Skip to content

Commit

Permalink
Dirty mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
rtripault committed Feb 25, 2015
1 parent 37b47f1 commit d17d557
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
73 changes: 73 additions & 0 deletions tests/Configuration/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,79 @@ public function testItemsLoadedFromModx($items)
$this->assertEquals($items, $config->getAll(), 'Items retrieved from modX should be available');
}

public function testSave()
{
$modx = $this->getMock('modX', array('getObject', 'newObject', 'getCacheManager', 'fromJSON', 'getOption', 'toJSON'));

$app = $this->prophesize('MODX\Shell\Application');
$app->getMODX()->willReturn($modx);

$config = new Component($app->reveal());
$config->set('dummy', array('service' => 'Fake'));

$setting = $this->getMock('modSystemSetting', array('set', 'save'));
$setting->expects($this->once())->method('save')->will($this->returnValue($setting));

$modx->expects($this->once())->method('getObject')->with('modSystemSetting', array(
'key' => 'console_commands'
))->will($this->returnValue($setting));

$cache = $this->getMock('modCacheManager', array('refresh'));
$cache->expects($this->once())->method('refresh')->will($this->returnValue(true));

$modx->expects($this->once())->method('getCacheManager')->will($this->returnValue($cache));

$this->assertTrue($config->save(), 'Saving components services is possible');
}

public function testSaveShouldCreateSystemSetting()
{
$modx = $this->getMock('modX', array('getObject', 'newObject', 'getCacheManager', 'fromJSON', 'getOption', 'toJSON'));

$app = $this->prophesize('MODX\Shell\Application');
$app->getMODX()->willReturn($modx);

$config = new Component($app->reveal());
$config->set('dummy', array('service' => 'Fake'));

$setting = $this->getMock('modSystemSetting', array('set', 'save'));
$setting->expects($this->once())->method('save')->will($this->returnValue($setting));

$modx->expects($this->once())->method('getObject')->with('modSystemSetting', array(
'key' => 'console_commands'
))->will($this->returnValue(null));

$modx->expects($this->once())->method('newObject')->with('modSystemSetting')->will($this->returnValue($setting));

$cache = $this->getMock('modCacheManager', array('refresh'));
$cache->expects($this->once())->method('refresh')->will($this->returnValue(true));

$modx->expects($this->once())->method('getCacheManager')->will($this->returnValue($cache));

$this->assertTrue($config->save(), 'Saving components services creates the appropriate system setting');
}

public function testSaveShouldFail()
{
$modx = $this->getMock('modX', array('getObject', 'newObject', 'getCacheManager', 'fromJSON', 'getOption', 'toJSON'));

$app = $this->prophesize('MODX\Shell\Application');
$app->getMODX()->willReturn($modx);

$config = new Component($app->reveal());
$config->set('dummy', array('service' => 'Fake'));

$setting = $this->getMock('modSystemSetting', array('set', 'save'));
$setting->expects($this->once())->method('save')->will($this->returnValue(false));

$modx->expects($this->once())->method('getObject')->with('modSystemSetting', array(
'key' => 'console_commands'
))->will($this->returnValue($setting));


$this->assertFalse($config->save(), 'Failing to save system setting should not trigger a cache refresh');
}

public function getData()
{
return array(
Expand Down
1 change: 0 additions & 1 deletion tests/Configuration/ExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class ExtensionTest extends \PHPUnit_Framework_TestCase
{

/**
* @param array $items
*
Expand Down

0 comments on commit d17d557

Please sign in to comment.