diff --git a/src/PHPCR/Util/Console/Command/BaseCommand.php b/src/PHPCR/Util/Console/Command/BaseCommand.php index f399b80..eeadd0b 100644 --- a/src/PHPCR/Util/Console/Command/BaseCommand.php +++ b/src/PHPCR/Util/Console/Command/BaseCommand.php @@ -10,6 +10,8 @@ abstract class BaseCommand extends Command { + protected $phpcrCliHelper; + /** * @return PHPCR\SessionInterface */ @@ -23,8 +25,19 @@ protected function getPhpcrSession() */ protected function getPhpcrCliHelper() { - $phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession()); - return $phpcrCliHelper; + if (!$this->phpcrCliHelper) { + $this->phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession()); + } + + return $this->phpcrCliHelper; + } + + /** + * Hack to enable overriding for unit tests. + */ + public function setPhpcrCliHelper(PhpcrCliHelper $helper) + { + $this->phpcrCliHelper = $helper; } public function configureNodeManipulationInput() diff --git a/src/PHPCR/Util/Console/Command/NodeTouchCommand.php b/src/PHPCR/Util/Console/Command/NodeTouchCommand.php index b7dc4b9..b2270fb 100644 --- a/src/PHPCR/Util/Console/Command/NodeTouchCommand.php +++ b/src/PHPCR/Util/Console/Command/NodeTouchCommand.php @@ -143,8 +143,8 @@ protected function execute(InputInterface $input, OutputInterface $output) } $helper->processNode($output, $node, array( - 'setProps' => $setProp, - 'removeProps' => $removeProp, + 'setProp' => $setProp, + 'removeProp' => $removeProp, 'addMixins' => $addMixins, 'removeMixins' => $removeMixins, 'dump' => $dump, diff --git a/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php b/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php index b5d9ece..e63f4aa 100644 --- a/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php +++ b/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php @@ -18,18 +18,35 @@ abstract class BaseCommandTest extends \PHPUnit_Framework_TestCase { - /** @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $session; - /** @var WorkspaceInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $workspace; - /** @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $repository; - /** @var PhpcrConsoleDumperHelper|\PHPUnit_Framework_MockObject_MockObject */ - protected $dumperHelper; - /** @var HelperSet */ - protected $helperSet; - /** @var Application */ - protected $application; + /** + * @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject + * */ + public $session; + + /** + * @var WorkspaceInterface|\PHPUnit_Framework_MockObject_MockObject + */ + public $workspace; + + /** + * @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject + */ + public $repository; + + /** + * @var PhpcrConsoleDumperHelper|\PHPUnit_Framework_MockObject_MockObject + */ + public $dumperHelper; + + /** + * @var HelperSet + */ + public $helperSet; + + /** + * @var Application + */ + public $application; public function setUp() { @@ -50,6 +67,10 @@ public function setUp() 'phpcr_console_dumper' => $this->dumperHelper, )); + $this->phpcrCliHelper = $this->getMockBuilder('PHPCR\Util\Console\Helper\PhpcrCliHelper') + ->disableOriginalConstructor() + ->getMock(); + $this->session->expects($this->any()) ->method('getWorkspace') ->will($this->returnValue($this->workspace)); diff --git a/tests/PHPCR/Tests/Util/Console/Command/NodeTouchCommandTest.php b/tests/PHPCR/Tests/Util/Console/Command/NodeTouchCommandTest.php index 45bf737..9b2ba7d 100644 --- a/tests/PHPCR/Tests/Util/Console/Command/NodeTouchCommandTest.php +++ b/tests/PHPCR/Tests/Util/Console/Command/NodeTouchCommandTest.php @@ -13,7 +13,11 @@ class NodeTouchCommandTest extends BaseCommandTest public function setUp() { parent::setUp(); - $this->application->add(new NodeTouchCommand()); + $this->command = new NodeTouchCommand; + + // override default concrete instance with mock + $this->command->setPhpcrCliHelper($this->phpcrCliHelper); + $this->application->add($this->command); $this->nodeType = $this->getMock('PHPCR\NodeType\NodeTypeInterface'); } @@ -38,8 +42,47 @@ public function testTouch() $this->session->expects($this->once()) ->method('save'); + + $ct = $this->executeCommand('phpcr:node:touch', array( + 'path' => '/cms', + )); + } + + public function testUpdate() + { + $this->session->expects($this->exactly(1)) + ->method('getNode') + ->with('/cms') + ->will($this->returnValue($this->node1)); + $this->node1->expects($this->once()) + ->method('getPrimaryNodeType') + ->will($this->returnValue($this->nodeType)); + $this->nodeType->expects($this->once()) + ->method('getName') + ->will($this->returnValue('nt:unstructured')); + + + $me = $this; + $this->phpcrCliHelper->expects($this->once()) + ->method('processNode') + ->will($this->returnCallback(function ($output, $node, $options) use ($me) { + $me->assertEquals($me->node1, $node); + $me->assertEquals(array( + 'setProp' => array('foo=bar'), + 'removeProp' => array('bar'), + 'addMixins' => array('foo:bar'), + 'removeMixins' => array('bar:foo'), + 'dump' => true, + ), $options); + })); + $ct = $this->executeCommand('phpcr:node:touch', array( 'path' => '/cms', + '--set-prop' => array('foo=bar'), + '--remove-prop' => array('bar'), + '--add-mixin' => array('foo:bar'), + '--remove-mixin' => array('bar:foo'), + '--dump' => true, )); } }