2016-05-27 14:33:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* FuzeWorks.
|
|
|
|
*
|
|
|
|
* The FuzeWorks MVC PHP FrameWork
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 TechFuze
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @author TechFuze
|
|
|
|
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
|
|
|
|
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
|
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
|
|
|
|
*
|
2016-07-23 15:06:19 +00:00
|
|
|
* @link http://techfuze.net/fuzeworks
|
2016-05-27 14:33:12 +00:00
|
|
|
* @since Version 0.0.1
|
|
|
|
*
|
2016-07-23 15:06:19 +00:00
|
|
|
* @version Version 1.0.0
|
2016-05-27 14:33:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
use FuzeWorks\Factory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FactoryTest.
|
|
|
|
*
|
|
|
|
* Will test the FuzeWorks Factory.
|
|
|
|
*/
|
|
|
|
class factoryTest extends CoreTestAbstract
|
|
|
|
{
|
|
|
|
public function testCanLoadFactory()
|
|
|
|
{
|
|
|
|
$this->assertInstanceOf('FuzeWorks\Factory', Factory::getInstance());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCanLoadFactory
|
|
|
|
*/
|
|
|
|
public function testLoadSameInstance()
|
|
|
|
{
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertSame(Factory::getInstance(), Factory::getInstance());
|
2016-05-27 14:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCanLoadFactory
|
|
|
|
*/
|
|
|
|
public function testLoadDifferentInstance()
|
|
|
|
{
|
|
|
|
// First a situation where one is the shared instance and one is a cloned instance
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertNotSame(Factory::getInstance(), Factory::getInstance(true));
|
2016-05-27 14:33:12 +00:00
|
|
|
|
|
|
|
// And a situation where both are cloned instances
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertNotSame(Factory::getInstance(true), Factory::getInstance(true));
|
2016-05-27 14:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testCanLoadFactory
|
|
|
|
*/
|
|
|
|
public function testObjectsSameInstance()
|
|
|
|
{
|
|
|
|
// Create mock
|
|
|
|
$mock = $this->getMock('MockInstance');
|
|
|
|
|
|
|
|
// Test not set
|
|
|
|
$this->assertNull(Factory::getInstance()->mock);
|
|
|
|
|
|
|
|
// Same instance factories
|
|
|
|
$factory1 = Factory::getInstance()->setInstance('Mock', $mock);
|
|
|
|
$factory2 = Factory::getInstance()->setInstance('Mock', $mock);
|
|
|
|
|
|
|
|
// Return the mocks
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertSame($factory1->mock, $factory2->mock);
|
2016-05-27 14:33:12 +00:00
|
|
|
|
|
|
|
// Different instance factories
|
|
|
|
$factory3 = Factory::getInstance(true)->setInstance('Mock', $mock);
|
|
|
|
$factory4 = Factory::getInstance(true)->setInstance('Mock', $mock);
|
|
|
|
|
|
|
|
// Return the mocks
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertSame($factory3->mock, $factory4->mock);
|
2016-05-27 14:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testObjectsSameInstance
|
|
|
|
*/
|
|
|
|
public function testObjectsDifferentInstance()
|
|
|
|
{
|
|
|
|
// Create mock
|
|
|
|
$mock = $this->getMock('MockInstance');
|
|
|
|
|
|
|
|
// Test not set
|
|
|
|
$this->assertNull(Factory::getInstance()->mock);
|
|
|
|
|
|
|
|
// Same instance factories
|
|
|
|
$factory1 = Factory::getInstance()->setInstance('Mock', $mock);
|
|
|
|
$factory2 = Factory::getInstance()->setInstance('Mock', $mock);
|
|
|
|
|
|
|
|
// Clone the instance in factory2
|
|
|
|
$factory2->cloneInstance('Mock');
|
|
|
|
|
|
|
|
// Should be true, since both Factories use the same Mock instance
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertSame($factory1->mock, $factory2->mock);
|
2016-05-27 14:33:12 +00:00
|
|
|
|
|
|
|
// Different instance factories
|
|
|
|
$factory3 = Factory::getInstance(true)->setInstance('Mock', $mock);
|
|
|
|
$factory4 = Factory::getInstance(true)->setInstance('Mock', $mock);
|
|
|
|
|
|
|
|
// Clone the instance in factory4
|
|
|
|
$factory4->cloneInstance('Mock');
|
|
|
|
|
|
|
|
// Should be false, since both Factories use a different Mock instance
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertNotSame($factory3->mock, $factory4->mock);
|
2016-05-27 14:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGlobalCloneInstance()
|
|
|
|
{
|
|
|
|
// First test without global cloning
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertSame(Factory::getInstance(), Factory::getInstance());
|
2016-05-27 14:33:12 +00:00
|
|
|
|
|
|
|
// Now enable global cloning
|
|
|
|
Factory::enableCloneInstances();
|
|
|
|
|
|
|
|
// Now test without global cloning
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertNotSame(Factory::getInstance(), Factory::getInstance());
|
2016-05-27 14:33:12 +00:00
|
|
|
|
|
|
|
// Disable global cloning
|
|
|
|
Factory::disableCloneInstances();
|
|
|
|
|
|
|
|
// And test again without global cloning
|
2016-06-02 11:57:32 +00:00
|
|
|
$this->assertSame(Factory::getInstance(), Factory::getInstance());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewFactoryInstance()
|
|
|
|
{
|
|
|
|
// Load the different factories
|
|
|
|
$factory = new Factory();
|
|
|
|
$factory2 = Factory::getInstance();
|
|
|
|
|
|
|
|
// Test if the objects are different factory instances
|
|
|
|
$this->assertNotSame($factory, $factory2);
|
|
|
|
|
|
|
|
// Fetch the instances
|
|
|
|
$instances1 = $factory->getClassInstances();
|
|
|
|
$instances2 = $factory2->getClassInstances();
|
|
|
|
|
|
|
|
// And test if all ClassInstances are the same
|
|
|
|
foreach ($instances1 as $className => $object) {
|
|
|
|
$this->assertSame($object, $instances2[$className]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// And test when changing one classInstance
|
|
|
|
$factory->newInstance('Layout');
|
2016-06-02 18:12:22 +00:00
|
|
|
$this->assertNotSame($factory->layout, $factory2->layout);
|
2016-05-27 14:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
Factory::disableCloneInstances();
|
2016-06-02 11:57:32 +00:00
|
|
|
|
|
|
|
$factory = Factory::getInstance();
|
|
|
|
if (isset($factory->Mock))
|
|
|
|
{
|
|
|
|
$factory->removeInstance('Mock');
|
|
|
|
}
|
2016-05-27 14:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|