We will be off from 27/1 (Monday) to 31/1 (Friday) (GMT +7) for our Tet Holiday (Lunar New Year) in our country

Commit 0d96e0b9 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

Loader container

parent c4a4aaff
<?php
class EcomDev_PHPUnit_Model_Fixture_Loader_Container
{
/**
* List of loaders by code
*
* @var EcomDev_PHPUnit_Model_Fixture_LoaderInterface[]
*/
protected $_loaders = array();
/**
* Returns loader by code
*
* If loader does not exists, it returns false
*
* @param $code
* @return bool|EcomDev_PHPUnit_Model_Fixture_LoaderInterface
*/
public function get($code)
{
if (!$this->has($code)) {
return false;
}
return $this->_loaders[$code];
}
/**
* Adds a new loader to the container
*
* @param string $code
* @param EcomDev_PHPUnit_Model_Fixture_LoaderInterface $loader
* @return $this
*/
public function add($code, EcomDev_PHPUnit_Model_Fixture_LoaderInterface $loader)
{
$this->_loaders[$code] = $loader;
return $this;
}
/**
* Removes a loader by code from container
*
* @param string $code
* @return $this
*/
public function remove($code)
{
unset($this->_loaders[$code]);
return $this;
}
/**
* Checks existance of the loader by container
*
* @param string $code
* @return bool
*/
public function has($code)
{
return isset($this->_loaders[$code]);
}
}
\ No newline at end of file
<?php
/**
* The interface that will provide interface of the loader
* for a fixture processor
*
* This one should work like a single loader for each kind of data
*/
interface EcomDev_PHPUnit_Model_Fixture_LoaderInterface
{
/**
* Overrides the data in the system for specified data
*
* It should realize default functionality
* for a default fixture processors
*
* @param array $data
* @return $this
* @throws InvalidArgumentException
*/
public function override($data);
/**
* This one should realize possibility to merge data with existing Magento data
*
* @param $data
* @return mixed
*/
public function merge($data);
/**
* Flushes the data that was added before loading
*
* @return mixed
*/
public function flush();
/**
* Restores original data, that was modified by fixture flushing
*
* @return mixed
*/
public function restore();
}
<?php
use EcomDev_Utils_Reflection as ReflectionUtil;
class EcomDev_PHPUnitTest_Test_Model_Fixture_Loader_Container
extends PHPUnit_Framework_TestCase
{
/**
* @var EcomDev_PHPUnit_Model_Fixture_Loader_Container
*/
protected $_factory;
protected function setUp()
{
$this->_factory = new EcomDev_PHPUnit_Model_Fixture_Loader_Container();
}
public function testItHasLoadersProperty()
{
$this->assertObjectHasAttribute('_loaders', $this->_factory);
}
public function testItAddsMultipleLoaders()
{
$loaders = $this->_generateLoaders(3);
foreach ($loaders as $code => $loader) {
$this->_factory->add($code, $loader);
}
$this->assertAttributeEquals($loaders, '_loaders', $this->_factory);
}
public function testItRemovesLoaderByCode()
{
$loaders = $this->_stubLoaders(3);
$this->_factory->remove('my_loader_2');
$this->assertAttributeEquals(
array(
'my_loader_1' => $loaders['my_loader_1'],
'my_loader_3' => $loaders['my_loader_3']
),
'_loaders',
$this->_factory
);
}
public function testItChecksExistanceOfLoaderByCode()
{
$this->_stubLoaders(3);
$this->assertTrue($this->_factory->has('my_loader_1'));
$this->assertTrue($this->_factory->has('my_loader_2'));
$this->assertTrue($this->_factory->has('my_loader_3'));
$this->_factory->remove('my_loader_2');
$this->assertTrue($this->_factory->has('my_loader_1'));
$this->assertFalse($this->_factory->has('my_loader_2'));
$this->assertTrue($this->_factory->has('my_loader_3'));
}
public function testItReturnsLoaderByCode()
{
$loaders = $this->_stubLoaders(3);
$this->assertSame(
$loaders['my_loader_3'],
$this->_factory->get('my_loader_3')
);
}
public function testItReturnsFalseIfLoaderDoesNotExist()
{
$this->assertFalse(
$this->_factory->get('my_loader_1')
);
}
/**
* Generates loaders for tests
*
* @param int $count
* @return EcomDev_PHPUnit_Model_Fixture_Loader_Interface[]
*/
protected function _generateLoaders($count)
{
$loaders = array();
for ($i = 1; $i <= $count; $i ++) {
$loaders['my_loader_' . $i] = $this->getMockForAbstractClass(
'EcomDev_PHPUnit_Model_Fixture_LoaderInterface'
);
}
return $loaders;
}
/**
* Generates and stubs loaders for tests
*
* @param int $count
* @return EcomDev_PHPUnit_Model_Fixture_LoaderInterface[]
*/
protected function _stubLoaders($count)
{
$loaders = $this->_generateLoaders($count);
ReflectionUtil::setRestrictedPropertyValue(
$this->_factory, '_loaders', $loaders
);
return $loaders;
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment