From b7dbae8ff2655a4b1b97a1bf33d6342299d396d3 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Thu, 19 Feb 2015 21:34:30 +0100 Subject: [PATCH 01/28] Abstract command tests --- .../MageTest/Command/AbstractCommandTest.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 tests/MageTest/Command/AbstractCommandTest.php diff --git a/tests/MageTest/Command/AbstractCommandTest.php b/tests/MageTest/Command/AbstractCommandTest.php new file mode 100644 index 0000000..4f7a2e1 --- /dev/null +++ b/tests/MageTest/Command/AbstractCommandTest.php @@ -0,0 +1,56 @@ +abstractCommand = $this->getMockForAbstractClass('Mage\Command\AbstractCommand'); + } + + /** + * @covers ::setConfig + */ + public function testSetConfig() + { + $configMock = $this->getMock('Mage\Config'); + $this->abstractCommand->setConfig($configMock); + + $configProperty = new \ReflectionProperty($this->abstractCommand, 'config'); + $configProperty->setAccessible(true); + $configValue = $configProperty->getValue($this->abstractCommand); + + $this->assertEquals($configMock, $configValue); + } + + /** + * @covers ::getConfig + */ + public function testGetConfig() + { + $configMock = $this->getMock('Mage\Config'); + + $configProperty = new \ReflectionProperty($this->abstractCommand, 'config'); + $configProperty->setAccessible(true); + $configProperty->setValue($this->abstractCommand, $configMock); + + $actual = $this->abstractCommand->getConfig(); + $this->assertEquals($configMock, $actual); + } +} From 10b8d7982eca5dc97dbce16c00b0c8855dae3063 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Fri, 20 Feb 2015 15:15:30 +0100 Subject: [PATCH 02/28] Update autoloading for MageTest namespace --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6f15d6c..e09b417 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "psr-4": { "Mage\\": "./Mage", "Task\\": [".mage/tasks", "../../../.mage/tasks"], - "Command\\": [".mage/tasks", "../../../.mage/commands"] + "Command\\": [".mage/tasks", "../../../.mage/commands"], + "MageTest\\": "./tests/MageTest" } }, "config": { From d8d90cb2ecfb3c121ab79811ab53b7e476c62dcf Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Fri, 20 Feb 2015 15:55:56 +0100 Subject: [PATCH 03/28] Add TestHelper\BaseTest with useful test methods --- tests/MageTest/TestHelper/BaseTest.php | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/MageTest/TestHelper/BaseTest.php diff --git a/tests/MageTest/TestHelper/BaseTest.php b/tests/MageTest/TestHelper/BaseTest.php new file mode 100644 index 0000000..a547e98 --- /dev/null +++ b/tests/MageTest/TestHelper/BaseTest.php @@ -0,0 +1,40 @@ + + */ +abstract class BaseTest extends \PHPUnit_Framework_TestCase +{ + /** + * Returns value of non-public property from given class + * + * @param string|object $object Object instance or class name + * @param string $propertyName Class' or object's property name + * @return mixed + */ + final protected function getPropertyValue($object, $propertyName) + { + $configProperty = new \ReflectionProperty($object, $propertyName); + $configProperty->setAccessible(true); + + return $configProperty->getValue($object); + } + + /** + * Sets value to given property and given object + * + * @param object $object Object instance + * @param string $propertyName Property name + * @param mixed $value Value to set + */ + final protected function setPropertyValue($object, $propertyName, $value) + { + $configProperty = new \ReflectionProperty($object, $propertyName); + $configProperty->setAccessible(true); + $configProperty->setValue($object, $value); + } +} From e35a8a7f3a1185104b73fa7cd1ef265f0ea40d05 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Fri, 20 Feb 2015 15:56:39 +0100 Subject: [PATCH 04/28] Apply BaseTest as parent of AbstractCommandTest class --- tests/MageTest/Command/AbstractCommandTest.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/MageTest/Command/AbstractCommandTest.php b/tests/MageTest/Command/AbstractCommandTest.php index 4f7a2e1..5f77a0e 100644 --- a/tests/MageTest/Command/AbstractCommandTest.php +++ b/tests/MageTest/Command/AbstractCommandTest.php @@ -2,6 +2,7 @@ namespace MageTest\Command; use Mage\Command\AbstractCommand; +use MageTest\TestHelper\BaseTest; use PHPUnit_Framework_MockObject_MockObject; /** @@ -9,7 +10,7 @@ use PHPUnit_Framework_MockObject_MockObject; * @package MageTest\Command * @coversDefaultClass Mage\Command\AbstractCommand */ -class AbstractCommandTest extends \PHPUnit_Framework_TestCase +class AbstractCommandTest extends BaseTest { /** * @var AbstractCommand|PHPUnit_Framework_MockObject_MockObject @@ -32,11 +33,8 @@ class AbstractCommandTest extends \PHPUnit_Framework_TestCase $configMock = $this->getMock('Mage\Config'); $this->abstractCommand->setConfig($configMock); - $configProperty = new \ReflectionProperty($this->abstractCommand, 'config'); - $configProperty->setAccessible(true); - $configValue = $configProperty->getValue($this->abstractCommand); - - $this->assertEquals($configMock, $configValue); + $actual = $this->getPropertyValue($this->abstractCommand, 'config'); + $this->assertEquals($configMock, $actual); } /** @@ -45,10 +43,7 @@ class AbstractCommandTest extends \PHPUnit_Framework_TestCase public function testGetConfig() { $configMock = $this->getMock('Mage\Config'); - - $configProperty = new \ReflectionProperty($this->abstractCommand, 'config'); - $configProperty->setAccessible(true); - $configProperty->setValue($this->abstractCommand, $configMock); + $this->setPropertyValue($this->abstractCommand, 'config', $configMock); $actual = $this->abstractCommand->getConfig(); $this->assertEquals($configMock, $actual); From cf89e2410648fb0695c2a48fc7479f8bb2d31f6d Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Fri, 20 Feb 2015 16:16:18 +0100 Subject: [PATCH 05/28] Add PHPDoc for BaseTest command --- tests/MageTest/Command/AbstractCommandTest.php | 1 + tests/MageTest/TestHelper/BaseTest.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/tests/MageTest/Command/AbstractCommandTest.php b/tests/MageTest/Command/AbstractCommandTest.php index 5f77a0e..95afc37 100644 --- a/tests/MageTest/Command/AbstractCommandTest.php +++ b/tests/MageTest/Command/AbstractCommandTest.php @@ -8,6 +8,7 @@ use PHPUnit_Framework_MockObject_MockObject; /** * Class AbstractCommandTest * @package MageTest\Command + * @author Jakub Turek * @coversDefaultClass Mage\Command\AbstractCommand */ class AbstractCommandTest extends BaseTest diff --git a/tests/MageTest/TestHelper/BaseTest.php b/tests/MageTest/TestHelper/BaseTest.php index a547e98..c0b974e 100644 --- a/tests/MageTest/TestHelper/BaseTest.php +++ b/tests/MageTest/TestHelper/BaseTest.php @@ -4,6 +4,11 @@ namespace MageTest\TestHelper; /** * Class BaseTest + * + * Class containing common methods useful for unit testing. + * Since Magallanes keeps compatibility with PHP 5.3, those methods can't be moved to a trait. + * This class extends \PHPUnit_Framework_TestCase so it can be used with any test class. + * * @package MageTest\TestHelper * @author Jakub Turek */ From b3587935fda7c848dec68c7f60872163ab209ec8 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 14:36:07 +0100 Subject: [PATCH 06/28] Add malkusch\phpmock to mock PHP built-in functions --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e09b417..d1a58cd 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ }, "require-dev": { "phpunit/phpunit": "4.3.5", - "satooshi/php-coveralls": ">=0.6.1" + "satooshi/php-coveralls": ">=0.6.1", + "malkusch/php-mock": "dev-php-5.3" }, "autoload": { "psr-4": { From ad6c8b24ac751c1ff649f223b502bd7d3f2e5e0c Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 14:38:53 +0100 Subject: [PATCH 07/28] Add ListCommand tests --- .../Command/BuiltIn/ListCommandTest.php | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 tests/MageTest/Command/BuiltIn/ListCommandTest.php diff --git a/tests/MageTest/Command/BuiltIn/ListCommandTest.php b/tests/MageTest/Command/BuiltIn/ListCommandTest.php new file mode 100644 index 0000000..53e598a --- /dev/null +++ b/tests/MageTest/Command/BuiltIn/ListCommandTest.php @@ -0,0 +1,148 @@ +listCommand = new ListCommand(); + + $this->scandirValueObj = new FixedValueFunction(); + $mockBuilder = new MockBuilder(); + $this->scandirMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn') + ->setName("scandir") + ->setCallableProvider($this->scandirValueObj) + ->build(); + $this->scandirMock->disable(); + $this->scandirMock->enable(); + } + + /** + * Disable logging to log file and turn off colors + * + * @before + */ + public function setUpConsoleStatics() + { + $consoleReflection = new \ReflectionClass('Mage\Console'); + $logEnableProperty = $consoleReflection->getProperty('logEnabled'); + $logEnableProperty->setAccessible(true); + $logEnableProperty->setValue(false); + + $configMock = $this->getMock('Mage\Config'); + $configMock->expects($this->atLeastOnce()) + ->method('getParameter') + ->with('no-color') + ->willReturn(true); + + $configProperty = $consoleReflection->getProperty('config'); + $configProperty->setAccessible(true); + $configProperty->setValue($configMock); + } + + /** + * @covers ::run + * @covers ::listEnvironments + */ + public function testListEnvironment() + { + $expectedOutput = <<expectOutputString($expectedOutput); + + $environmentsFiles = [ + 'rc.yml', + 'production.yml', + 'local.yml' + ]; + + $this->scandirValueObj->setValue($environmentsFiles); + + $configMock = $this->getMock('Mage\Config'); + $configMock->expects($this->once()) + ->method('getArgument') + ->with(1) + ->willReturn('environments'); + $this->listCommand->setConfig($configMock); + + $this->listCommand->run(); + } + + /** + * @covers ::run + * @covers ::listEnvironments + */ + public function testListEnvironmentWithNoEnvironments() + { + $expectedOutput = "\tYou don't have any environment configured.\n\n"; + $this->expectOutputString($expectedOutput); + + $this->scandirValueObj->setValue([]); + + $configMock = $this->getMock('Mage\Config'); + $configMock->expects($this->once()) + ->method('getArgument') + ->with(1) + ->willReturn('environments'); + $this->listCommand->setConfig($configMock); + $this->listCommand->run(); + } + + /** + * @covers ::run + */ + public function testRunWithInvalidCommand() + { + $expectedOutput = "\tThe Type of Elements to List is needed.\n\n"; + $this->expectOutputString($expectedOutput); + + $configMock = $this->getMock('Mage\Config'); + $configMock->expects($this->once()) + ->method('getArgument') + ->with(1) + ->willReturn('abc'); + $this->listCommand->setConfig($configMock); + $this->listCommand->run(); + } +} From 5944015b50041404fd49d3893dd09143ea2dd631 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 14:57:03 +0100 Subject: [PATCH 08/28] ListCommandTests refactor --- .../Command/BuiltIn/ListCommandTest.php | 89 ++++++++++--------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/tests/MageTest/Command/BuiltIn/ListCommandTest.php b/tests/MageTest/Command/BuiltIn/ListCommandTest.php index 53e598a..a6f7668 100644 --- a/tests/MageTest/Command/BuiltIn/ListCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/ListCommandTest.php @@ -76,56 +76,56 @@ class ListCommandTest extends BaseTest $configProperty->setValue($configMock); } - /** - * @covers ::run - * @covers ::listEnvironments - */ - public function testListEnvironment() + public function listEnvironmentsProvider() { - $expectedOutput = <<expectOutputString($expectedOutput); - - $environmentsFiles = [ - 'rc.yml', - 'production.yml', - 'local.yml' - ]; - - $this->scandirValueObj->setValue($environmentsFiles); - - $configMock = $this->getMock('Mage\Config'); - $configMock->expects($this->once()) - ->method('getArgument') - ->with(1) - ->willReturn('environments'); - $this->listCommand->setConfig($configMock); - - $this->listCommand->run(); + return array( + 'normal' => array( + 'environmentFiles' => array( + 'rc.yml', + 'production.yml', + 'local.yml' + ), + 'expectedOutput' => "\tThese are your configured environments:\n" + . "\t\t* local\n" + . "\t\t* production\n" + . "\t\t* rc\n" + . "\t\n" + ), + 'with_missing_yml_files' => array( + 'environmentFiles' => array( + 'rc', + 'production.yml' + ), + 'expectedOutput' => "\tThese are your configured environments:\n" + . "\t\t* production\n" + . "\t\n" + ), + 'with_no_yml_configs' => array( + 'environmentFiles' => array( + 'rc.ini', + 'production.txt' + ), + 'expectedOutput' => "\tYou don't have any environment configured.\n\n" + ), + 'with_no_configs' => array( + 'environmentFiles' => array(), + 'expectedOutput' => "\tYou don't have any environment configured.\n\n" + ) + ); } /** * @covers ::run * @covers ::listEnvironments + * @dataProvider listEnvironmentsProvider */ - public function testListEnvironmentWithNoEnvironments() + public function testListEnvironment($environmentFiles, $expectedOutput) { - $expectedOutput = "\tYou don't have any environment configured.\n\n"; $this->expectOutputString($expectedOutput); - $this->scandirValueObj->setValue([]); + $this->scandirValueObj->setValue($environmentFiles); + $this->mockInputArgument('environments'); - $configMock = $this->getMock('Mage\Config'); - $configMock->expects($this->once()) - ->method('getArgument') - ->with(1) - ->willReturn('environments'); - $this->listCommand->setConfig($configMock); $this->listCommand->run(); } @@ -137,12 +137,19 @@ OUTPUT; $expectedOutput = "\tThe Type of Elements to List is needed.\n\n"; $this->expectOutputString($expectedOutput); + $this->mockInputArgument('abc'); + + $this->listCommand->run(); + } + + private function mockInputArgument($argumentValue) + { $configMock = $this->getMock('Mage\Config'); $configMock->expects($this->once()) ->method('getArgument') ->with(1) - ->willReturn('abc'); + ->willReturn($argumentValue); + $this->listCommand->setConfig($configMock); - $this->listCommand->run(); } } From 881f0b2d871001fcbdb827a51f6bb3bebe14944f Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 14:57:25 +0100 Subject: [PATCH 09/28] Fix syntax error in ListCommand::run --- Mage/Command/BuiltIn/ListCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mage/Command/BuiltIn/ListCommand.php b/Mage/Command/BuiltIn/ListCommand.php index 0d27862..30714b2 100644 --- a/Mage/Command/BuiltIn/ListCommand.php +++ b/Mage/Command/BuiltIn/ListCommand.php @@ -40,7 +40,7 @@ class ListCommand extends AbstractCommand $exitCode = $this->listEnvironments(); break; - default; + default: throw new Exception('The Type of Elements to List is needed.'); break; } From e9ec99f3182f378f436e53aa7618d775bcf21b51 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 15:42:18 +0100 Subject: [PATCH 10/28] Add exit code assertions in ListCommandTest --- .../Command/BuiltIn/ListCommandTest.php | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/tests/MageTest/Command/BuiltIn/ListCommandTest.php b/tests/MageTest/Command/BuiltIn/ListCommandTest.php index a6f7668..06fc7fc 100644 --- a/tests/MageTest/Command/BuiltIn/ListCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/ListCommandTest.php @@ -89,7 +89,8 @@ class ListCommandTest extends BaseTest . "\t\t* local\n" . "\t\t* production\n" . "\t\t* rc\n" - . "\t\n" + . "\t\n", + 'expectedExitCode' => 0 ), 'with_missing_yml_files' => array( 'environmentFiles' => array( @@ -98,18 +99,21 @@ class ListCommandTest extends BaseTest ), 'expectedOutput' => "\tThese are your configured environments:\n" . "\t\t* production\n" - . "\t\n" + . "\t\n", + 'expectedExitCode' => 0 ), 'with_no_yml_configs' => array( 'environmentFiles' => array( 'rc.ini', 'production.txt' ), - 'expectedOutput' => "\tYou don't have any environment configured.\n\n" + 'expectedOutput' => "\tYou don't have any environment configured.\n\n", + 'expectedExitCode' => 220 ), 'with_no_configs' => array( 'environmentFiles' => array(), - 'expectedOutput' => "\tYou don't have any environment configured.\n\n" + 'expectedOutput' => "\tYou don't have any environment configured.\n\n", + 'expectedExitCode' => 220 ) ); } @@ -119,14 +123,15 @@ class ListCommandTest extends BaseTest * @covers ::listEnvironments * @dataProvider listEnvironmentsProvider */ - public function testListEnvironment($environmentFiles, $expectedOutput) + public function testListEnvironment($environmentFiles, $expectedOutput, $expectedExitCode) { $this->expectOutputString($expectedOutput); $this->scandirValueObj->setValue($environmentFiles); $this->mockInputArgument('environments'); - $this->listCommand->run(); + $actualExitCode = $this->listCommand->run(); + $this->assertEquals($expectedExitCode, $actualExitCode); } /** @@ -139,7 +144,9 @@ class ListCommandTest extends BaseTest $this->mockInputArgument('abc'); - $this->listCommand->run(); + $expectedExitCode = 221; + $actualExitCode = $this->listCommand->run(); + $this->assertEquals($expectedExitCode, $actualExitCode); } private function mockInputArgument($argumentValue) From d58e89c41db1c2cd38a3bad973c3f9fa9d7581e7 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 16:36:09 +0100 Subject: [PATCH 11/28] Set compiler by setter for better testability --- Mage/Command/BuiltIn/CompileCommand.php | 15 ++++++ .../Command/BuiltIn/CompileCommandTest.php | 51 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 tests/MageTest/Command/BuiltIn/CompileCommandTest.php diff --git a/Mage/Command/BuiltIn/CompileCommand.php b/Mage/Command/BuiltIn/CompileCommand.php index 9cd94b6..e7db44b 100644 --- a/Mage/Command/BuiltIn/CompileCommand.php +++ b/Mage/Command/BuiltIn/CompileCommand.php @@ -21,6 +21,21 @@ use Mage\Compiler; */ class CompileCommand extends AbstractCommand { + /** + * @var Compiler + */ + private $compiler; + + public function __construct() + { + $this->compiler = new Compiler(); + } + + public function setCompiler(Compiler $compiler) + { + $this->compiler = $compiler; + } + /** * @see \Mage\Compile::compile() */ diff --git a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php new file mode 100644 index 0000000..3780481 --- /dev/null +++ b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php @@ -0,0 +1,51 @@ +compileCommand = new CompileCommand(); + } + + /** + * @covers ::__construct + */ + public function testConstruct() + { + $compileCommand = new CompileCommand(); + + $compilerProperty = $this->getPropertyValue($compileCommand, 'compiler'); + $this->assertInstanceOf('Mage\Compiler', $compilerProperty); + } + + /** + * @covers ::setCompiler + */ + public function testSetCompiler() + { + $compilerMock = $this->getMock('Mage\Compiler'); + $this->compileCommand->setCompiler($compilerMock); + + $compilerProperty = $this->getPropertyValue($this->compileCommand, 'compiler'); + $this->assertEquals($compilerMock, $compilerProperty); + } +} From ee8bb3c2f98c1cc8a7ca04302165ab013cd5a987 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 16:45:26 +0100 Subject: [PATCH 12/28] Move "setUpConsoleStatics" into the BaseClass --- .../Command/BuiltIn/ListCommandTest.php | 23 +------------------ tests/MageTest/TestHelper/BaseTest.php | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/tests/MageTest/Command/BuiltIn/ListCommandTest.php b/tests/MageTest/Command/BuiltIn/ListCommandTest.php index 06fc7fc..19fc78f 100644 --- a/tests/MageTest/Command/BuiltIn/ListCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/ListCommandTest.php @@ -51,29 +51,8 @@ class ListCommandTest extends BaseTest ->build(); $this->scandirMock->disable(); $this->scandirMock->enable(); - } - /** - * Disable logging to log file and turn off colors - * - * @before - */ - public function setUpConsoleStatics() - { - $consoleReflection = new \ReflectionClass('Mage\Console'); - $logEnableProperty = $consoleReflection->getProperty('logEnabled'); - $logEnableProperty->setAccessible(true); - $logEnableProperty->setValue(false); - - $configMock = $this->getMock('Mage\Config'); - $configMock->expects($this->atLeastOnce()) - ->method('getParameter') - ->with('no-color') - ->willReturn(true); - - $configProperty = $consoleReflection->getProperty('config'); - $configProperty->setAccessible(true); - $configProperty->setValue($configMock); + $this->setUpConsoleStatics(); } public function listEnvironmentsProvider() diff --git a/tests/MageTest/TestHelper/BaseTest.php b/tests/MageTest/TestHelper/BaseTest.php index c0b974e..52dd372 100644 --- a/tests/MageTest/TestHelper/BaseTest.php +++ b/tests/MageTest/TestHelper/BaseTest.php @@ -42,4 +42,27 @@ abstract class BaseTest extends \PHPUnit_Framework_TestCase $configProperty->setAccessible(true); $configProperty->setValue($object, $value); } + + /** + * Disable logging to log file and turn off colors + * + * @before + */ + protected function setUpConsoleStatics() + { + $consoleReflection = new \ReflectionClass('Mage\Console'); + $logEnableProperty = $consoleReflection->getProperty('logEnabled'); + $logEnableProperty->setAccessible(true); + $logEnableProperty->setValue(false); + + $configMock = $this->getMock('Mage\Config'); + $configMock->expects($this->any()) + ->method('getParameter') + ->with('no-color') + ->willReturn(true); + + $configProperty = $consoleReflection->getProperty('config'); + $configProperty->setAccessible(true); + $configProperty->setValue($configMock); + } } From 5602bf83fedd897f3e7c20e9871f85c662725828 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 17:14:15 +0100 Subject: [PATCH 13/28] Add CompileCommand tests --- Mage/Command/BuiltIn/CompileCommand.php | 5 +- .../Command/BuiltIn/CompileCommandTest.php | 75 ++++++++++++++++++- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/Mage/Command/BuiltIn/CompileCommand.php b/Mage/Command/BuiltIn/CompileCommand.php index e7db44b..f03555b 100644 --- a/Mage/Command/BuiltIn/CompileCommand.php +++ b/Mage/Command/BuiltIn/CompileCommand.php @@ -28,7 +28,7 @@ class CompileCommand extends AbstractCommand public function __construct() { - $this->compiler = new Compiler(); + $this->setCompiler(new Compiler()); } public function setCompiler(Compiler $compiler) @@ -46,8 +46,7 @@ class CompileCommand extends AbstractCommand return 200; } - $compiler = new Compiler; - $compiler->compile(); + $this->compiler->compile(); Console::output('mage.phar compiled successfully', 0, 2); diff --git a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php index 3780481..ebada1f 100644 --- a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php @@ -4,12 +4,20 @@ namespace MageTest\Command\BuiltIn; use Mage\Command\BuiltIn\CompileCommand; use MageTest\TestHelper\BaseTest; +use malkusch\phpmock\FixedValueFunction; +use malkusch\phpmock\Mock; +use malkusch\phpmock\MockBuilder; /** * Class CompileCommandTest * @package MageTest\Command\BuiltIn * @coversDefaultClass Mage\Command\BuiltIn\CompileCommand * @uses Mage\Compiler + * @uses malkusch\phpmock\FixedValueFunction + * @uses malkusch\phpmock\Mock + * @uses malkusch\phpmock\MockBuilder + * @uses Mage\Console + * @uses Mage\Console\Colors */ class CompileCommandTest extends BaseTest { @@ -18,26 +26,48 @@ class CompileCommandTest extends BaseTest */ private $compileCommand; + /** + * @var FixedValueFunction + */ + private $iniGetValue; + + /** + * @var Mock + */ + private $iniGetMock; + + /** * @before */ public function before() { $this->compileCommand = new CompileCommand(); + + $this->iniGetValue = new FixedValueFunction(); + $mockBuilder = new MockBuilder(); + $this->iniGetMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn') + ->setName("ini_get") + ->setCallableProvider($this->iniGetValue) + ->build(); + $this->iniGetMock->disable(); + $this->iniGetMock->enable(); + + $this->setUpConsoleStatics(); } /** * @covers ::__construct + * @covers ::setCompiler */ public function testConstruct() { - $compileCommand = new CompileCommand(); - - $compilerProperty = $this->getPropertyValue($compileCommand, 'compiler'); + $compilerProperty = $this->getPropertyValue($this->compileCommand, 'compiler'); $this->assertInstanceOf('Mage\Compiler', $compilerProperty); } /** + * @covers ::__construct * @covers ::setCompiler */ public function testSetCompiler() @@ -48,4 +78,43 @@ class CompileCommandTest extends BaseTest $compilerProperty = $this->getPropertyValue($this->compileCommand, 'compiler'); $this->assertEquals($compilerMock, $compilerProperty); } + + /** + * @covers ::__construct + * @covers ::setCompiler + * @covers ::run + */ + public function testRun() + { + $expectedOutput = "mage.phar compiled successfully\n\n"; + $expectedExitCode = 0; + $this->expectOutputString($expectedOutput); + + $compilerMock = $this->getMock('Mage\Compiler'); + $compilerMock->expects($this->once()) + ->method('compile'); + + $this->iniGetValue->setValue(false); + $this->compileCommand->setCompiler($compilerMock); + $actualExitCode = $this->compileCommand->run(); + + $this->assertEquals($expectedExitCode, $actualExitCode); + } + + /** + * @covers ::__construct + * @covers ::setCompiler + * @covers ::run + */ + public function testRunWhenPharReadonlyEnabled() + { + $expectedOutput = "\tThe php.ini variable phar.readonly must be Off.\n\n"; + $expectedExitCode = 200; + $this->expectOutputString($expectedOutput); + $this->iniGetValue->setValue(true); + + $actualExitCode = $this->compileCommand->run(); + + $this->assertEquals($expectedExitCode, $actualExitCode); + } } From c06a9c7cf1b8cafb6a1477ec4f58c4c14c7c1c9e Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 17:18:03 +0100 Subject: [PATCH 14/28] Fix code style in CompileCommand according to PSR2 coding rules --- Mage/Command/BuiltIn/CompileCommand.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Mage/Command/BuiltIn/CompileCommand.php b/Mage/Command/BuiltIn/CompileCommand.php index f03555b..56e00bb 100644 --- a/Mage/Command/BuiltIn/CompileCommand.php +++ b/Mage/Command/BuiltIn/CompileCommand.php @@ -42,13 +42,23 @@ class CompileCommand extends AbstractCommand public function run() { if (ini_get('phar.readonly')) { - Console::output('The php.ini variable phar.readonly must be Off.', 1, 2); + Console::output( + 'The php.ini variable phar.readonly' + . ' must be Off.', + 1, + 2 + ); + return 200; } $this->compiler->compile(); - Console::output('mage.phar compiled successfully', 0, 2); + Console::output( + 'mage.phar compiled successfully', + 0, + 2 + ); return 0; } From b21b5f4c5e7c78807d5144e5392e3b1a8e231fb4 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 22:33:19 +0100 Subject: [PATCH 15/28] Add tests for LockCommand --- .../Command/BuiltIn/LockCommandTest.php | 199 ++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 tests/MageTest/Command/BuiltIn/LockCommandTest.php diff --git a/tests/MageTest/Command/BuiltIn/LockCommandTest.php b/tests/MageTest/Command/BuiltIn/LockCommandTest.php new file mode 100644 index 0000000..b814ed6 --- /dev/null +++ b/tests/MageTest/Command/BuiltIn/LockCommandTest.php @@ -0,0 +1,199 @@ +lockCommand = new LockCommand(); + + $mockBuilder = new MockBuilder(); + $fopenMock = $mockBuilder + ->setName('fopen') + ->setNamespace('Mage') + ->setFunction(function () { + return 'a'; + }) + ->build(); + + $this->fgetsValue = new FixedValueFunction(); + $fgetsMock = $mockBuilder + ->setNamespace('Mage') + ->setName('fgets') + ->setFunction( + function () { + switch (LockCommandTest::$fgetsCount) { + case 0: + LockCommandTest::$fgetsCount++; + return LockCommandTest::$mockName; + case 1: + LockCommandTest::$fgetsCount++; + return LockCommandTest::$mockEmail; + case 2: + LockCommandTest::$fgetsCount++; + return LockCommandTest::$mockDesc; + default: + throw new \Exception('"fgets" count limit exceed'); + } + } + ) + ->build(); + $getCwdMock = $mockBuilder + ->setNamespace('Mage\Command\Builtin') + ->setName('getcwd') + ->setFunction( + function () { + return ''; + } + ) + ->build(); + $fileGetContentsMock = $mockBuilder + ->setNamespace('Mage\Command\Builtin') + ->setName('file_put_contents') + ->setFunction( + function ($file, $contents) { + LockCommandTest::$filePutContentsFile = $file; + LockCommandTest::$filePutContentsResult = $contents; + } + ) + ->build(); + + $dateMock = $mockBuilder + ->setNamespace('Mage\Command\BuiltIn') + ->setName('date') + ->setFunction( + function () { + return '2015-01-01 12:00:00'; + } + ) + ->build(); + + $fopenMock->disable(); + $fgetsMock->disable(); + $getCwdMock->disable(); + $fileGetContentsMock->disable(); + $dateMock->disable(); + + $fopenMock->enable(); + $fgetsMock->enable(); + $getCwdMock->enable(); + $fileGetContentsMock->enable(); + $dateMock->enable(); + + $this->setUpConsoleStatics(); + } + + public function lockCommandProvider() + { + return array( + 'normal' => array( + 'name' => 'John Smith', + 'email' => 'john.smith@example.com', + 'description' => "There's a critical bug here!", + 'expectedLockFileContents' => "Locked environment at date: 2015-01-01 12:00:00\n" + . "Locked by John Smith (john.smith@example.com)\n" + . "There's a critical bug here!\n", + ), + 'with_no_name' => array( + 'name' => '', + 'email' => 'john.smith@example.com', + 'description' => "There's a critical bug here!", + 'expectedLockFileContents' => "Locked environment at date: 2015-01-01 12:00:00\n" + . "(john.smith@example.com)\n" + . "There's a critical bug here!\n", + ), + 'with_no_email' => array( + 'name' => 'John Smith', + 'email' => '', + 'description' => "There's a critical bug here!", + 'expectedLockFileContents' => "Locked environment at date: 2015-01-01 12:00:00\n" + . "Locked by John Smith \n" + . "There's a critical bug here!\n", + ), + 'with_no_name_nor_email' => array( + 'name' => '', + 'email' => '', + 'description' => "There's a critical bug here!", + 'expectedLockFileContents' => "Locked environment at date: 2015-01-01 12:00:00\n" + . "\n" + . "There's a critical bug here!\n", + ), + 'with_no_desciption' => array( + 'name' => 'John Smith', + 'email' => 'john.smith@example.com', + 'description' => '', + 'expectedLockFileContents' => "Locked environment at date: 2015-01-01 12:00:00\n" + . "Locked by John Smith (john.smith@example.com)" + ), + ); + } + + /** + * @covers ::run + * @dataProvider lockCommandProvider + */ + public function testRun($name, $email, $description, $expectedLockFileContents) + { + $expectedOutput = "Your name (enter to leave blank): " + . "Your email (enter to leave blank): " + . "Reason of lock (enter to leave blank): " + . "\tLocked deployment to production environment\n\n"; + $this->expectOutputString($expectedOutput); + $expectedLockFilePath = '/.mage/production.lock'; + $expectedExitCode = 0; + + self::$mockName = $name; + self::$mockEmail = $email; + self::$mockDesc = $description; + + $configMock = $this->getMock('Mage\Config'); + $configMock->expects($this->atLeastOnce()) + ->method('getEnvironment') + ->willReturn('production'); + $this->lockCommand->setConfig($configMock); + + $actualExitCode = $this->lockCommand->run(); + + $this->assertEquals($expectedExitCode, $actualExitCode); + $this->assertEquals($expectedLockFileContents, self::$filePutContentsResult); + $this->assertEquals($expectedLockFilePath, self::$filePutContentsFile); + } +} From 0bfe649aa5afa178b614beead0324adf055b4833 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sat, 21 Feb 2015 22:33:26 +0100 Subject: [PATCH 16/28] Fix code style for LockCommand --- Mage/Command/BuiltIn/LockCommand.php | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Mage/Command/BuiltIn/LockCommand.php b/Mage/Command/BuiltIn/LockCommand.php index 6c2a59e..e39001d 100644 --- a/Mage/Command/BuiltIn/LockCommand.php +++ b/Mage/Command/BuiltIn/LockCommand.php @@ -35,14 +35,26 @@ class LockCommand extends AbstractCommand implements RequiresEnvironment $reason = Console::readInput(); $lockmsg = PHP_EOL; - if ($name) $lockmsg .= 'Locked by ' . $name . ' '; - if ($email) $lockmsg .= '(' . $email . ')'; - if ($reason) $lockmsg .= PHP_EOL . $reason . PHP_EOL; + if ($name) { + $lockmsg .= 'Locked by ' . $name . ' '; + } + if ($email) { + $lockmsg .= '(' . $email . ')'; + } + if ($reason) { + $lockmsg .= PHP_EOL . $reason . PHP_EOL; + } $lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock'; file_put_contents($lockFile, 'Locked environment at date: ' . date('Y-m-d H:i:s') . $lockmsg); - Console::output('Locked deployment to ' . $this->getConfig()->getEnvironment() . ' environment', 1, 2); + Console::output( + 'Locked deployment to ' + . $this->getConfig()->getEnvironment() + . ' environment', + 1, + 2 + ); return 0; } From 04b270356a293207e23bb8a63b6d7b0fd9fb1edd Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sun, 22 Feb 2015 11:59:30 +0100 Subject: [PATCH 17/28] Reset fields in LockCommandTests for better reliability --- tests/MageTest/Command/BuiltIn/LockCommandTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/MageTest/Command/BuiltIn/LockCommandTest.php b/tests/MageTest/Command/BuiltIn/LockCommandTest.php index b814ed6..e56b63f 100644 --- a/tests/MageTest/Command/BuiltIn/LockCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/LockCommandTest.php @@ -43,6 +43,12 @@ class LockCommandTest extends BaseTest public function before() { self::$fgetsCount = 0; + self::$mockName = ''; + self::$mockEmail = ''; + self::$mockDesc = ''; + self::$filePutContentsResult = ''; + self::$filePutContentsFile = ''; + $this->lockCommand = new LockCommand(); $mockBuilder = new MockBuilder(); From cde8263d0630e28f7cdb5cc4ac4c3b7ecb34bb8e Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sun, 22 Feb 2015 12:52:54 +0100 Subject: [PATCH 18/28] Add UnlockCommand tests --- .../Command/BuiltIn/UnlockCommandTest.php | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/MageTest/Command/BuiltIn/UnlockCommandTest.php diff --git a/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php b/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php new file mode 100644 index 0000000..bd7ec91 --- /dev/null +++ b/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php @@ -0,0 +1,126 @@ +unlockCommand = new UnlockCommand(); + + self::$isUnlinkCalled = false; + self::$fileExistsResult = false; + self::$isFileExists = false; + + $mockBuilder = new MockBuilder(); + $fileExistsMock = $mockBuilder + ->setName('file_exists') + ->setNamespace('Mage\Command\BuiltIn') + ->setFunction( + function ($filePath) { + UnlockCommandTest::$fileExistsResult = $filePath; + return UnlockCommandTest::$isFileExists; + } + ) + ->build(); + $unlinkMock = $mockBuilder + ->setName('unlink') + ->setNamespace('Mage\Command\BuiltIn') + ->setFunction( + function () { + UnlockCommandTest::$isUnlinkCalled = true; + } + ) + ->build(); + $getCwdMock = $mockBuilder + ->setNamespace('Mage\Command\BuiltIn') + ->setName('getcwd') + ->setFunction( + function () { + return ''; + } + ) + ->build(); + + $fileExistsMock->disable(); + $unlinkMock->disable(); + $getCwdMock->disable(); + + + $fileExistsMock->enable(); + $unlinkMock->enable(); + $getCwdMock->enable(); + + $configMock = $this->getMock('Mage\Config'); + $configMock->expects($this->atLeastOnce()) + ->method('getEnvironment') + ->willReturn('production'); + $this->unlockCommand->setConfig($configMock); + + $this->setUpConsoleStatics(); + } + + /** + * @covers ::run + */ + public function testRun() + { + $expectedOutput = "\tUnlocked deployment to production environment\n\n"; + $this->expectOutputString($expectedOutput); + $expectedLockFilePath = '/.mage/production.lock'; + + self::$isFileExists = true; + + $actualExitCode = $this->unlockCommand->run(); + $expectedExitCode = 0; + + $this->assertTrue(self::$isUnlinkCalled); + $this->assertEquals($expectedExitCode, $actualExitCode); + $this->assertEquals($expectedLockFilePath, self::$fileExistsResult); + } + + /** + * @covers ::run + */ + public function testRunIfFileNotExist() + { + $expectedOutput = "\tUnlocked deployment to production environment\n\n"; + $this->expectOutputString($expectedOutput); + $expectedLockFilePath = '/.mage/production.lock'; + + self::$isFileExists = false; + + $actualExitCode = $this->unlockCommand->run(); + $expectedExitCode = 0; + + $this->assertFalse(self::$isUnlinkCalled); + $this->assertEquals($expectedExitCode, $actualExitCode); + $this->assertEquals($expectedLockFilePath, self::$fileExistsResult); + } +} From 3792f5fb8c7f123a2fee4c10099341547875a9d9 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sun, 22 Feb 2015 12:53:47 +0100 Subject: [PATCH 19/28] Fix code style in UnlockCommand --- Mage/Command/BuiltIn/UnlockCommand.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Mage/Command/BuiltIn/UnlockCommand.php b/Mage/Command/BuiltIn/UnlockCommand.php index 462349c..1666e67 100644 --- a/Mage/Command/BuiltIn/UnlockCommand.php +++ b/Mage/Command/BuiltIn/UnlockCommand.php @@ -19,8 +19,7 @@ use Mage\Console; * * @author Andrés Montañez */ -class UnlockCommand - extends AbstractCommand implements RequiresEnvironment +class UnlockCommand extends AbstractCommand implements RequiresEnvironment { /** * Unlocks an Environment @@ -33,7 +32,12 @@ class UnlockCommand @unlink($lockFile); } - Console::output('Unlocked deployment to ' . $this->getConfig()->getEnvironment() . ' environment', 1, 2); + Console::output( + 'Unlocked deployment to ' + . $this->getConfig()->getEnvironment() . ' environment', + 1, + 2 + ); return 0; } From a91060a16ffd159654614cc19a6dba73386cc7e7 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sun, 22 Feb 2015 18:14:53 +0100 Subject: [PATCH 20/28] General tests cleanup --- .../Command/BuiltIn/CompileCommandTest.php | 12 +++--------- .../MageTest/Command/BuiltIn/ListCommandTest.php | 16 ++++++++-------- .../Command/BuiltIn/UnlockCommandTest.php | 1 - 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php index ebada1f..9127833 100644 --- a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php @@ -31,12 +31,6 @@ class CompileCommandTest extends BaseTest */ private $iniGetValue; - /** - * @var Mock - */ - private $iniGetMock; - - /** * @before */ @@ -46,12 +40,12 @@ class CompileCommandTest extends BaseTest $this->iniGetValue = new FixedValueFunction(); $mockBuilder = new MockBuilder(); - $this->iniGetMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn') + $iniGetMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn') ->setName("ini_get") ->setCallableProvider($this->iniGetValue) ->build(); - $this->iniGetMock->disable(); - $this->iniGetMock->enable(); + $iniGetMock->disable(); + $iniGetMock->enable(); $this->setUpConsoleStatics(); } diff --git a/tests/MageTest/Command/BuiltIn/ListCommandTest.php b/tests/MageTest/Command/BuiltIn/ListCommandTest.php index 19fc78f..4d193e1 100644 --- a/tests/MageTest/Command/BuiltIn/ListCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/ListCommandTest.php @@ -26,11 +26,6 @@ class ListCommandTest extends BaseTest */ private $listCommand; - /** - * @var Mock - */ - private $scandirMock; - /** * @var FixedValueFunction */ @@ -45,12 +40,12 @@ class ListCommandTest extends BaseTest $this->scandirValueObj = new FixedValueFunction(); $mockBuilder = new MockBuilder(); - $this->scandirMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn') + $scandirMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn') ->setName("scandir") ->setCallableProvider($this->scandirValueObj) ->build(); - $this->scandirMock->disable(); - $this->scandirMock->enable(); + $scandirMock->disable(); + $scandirMock->enable(); $this->setUpConsoleStatics(); } @@ -128,6 +123,11 @@ class ListCommandTest extends BaseTest $this->assertEquals($expectedExitCode, $actualExitCode); } + /** + * Stub Config::getArgument to return desired value + * + * @param String $argumentValue Input argument + */ private function mockInputArgument($argumentValue) { $configMock = $this->getMock('Mage\Config'); diff --git a/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php b/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php index bd7ec91..8d92ced 100644 --- a/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php @@ -72,7 +72,6 @@ class UnlockCommandTest extends BaseTest $unlinkMock->disable(); $getCwdMock->disable(); - $fileExistsMock->enable(); $unlinkMock->enable(); $getCwdMock->enable(); From 5e51b79857fb2af1a24ef0a2755c0f7d0891abbd Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Thu, 5 Mar 2015 20:24:39 +0100 Subject: [PATCH 21/28] Resolve conflicts on composer.lock --- composer.lock | 54 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/composer.lock b/composer.lock index 312d535..44ef421 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "d82ccc62c52f99a0819bea9b247d4f86", + "hash": "6b9e925b25d502eb4834a26e4ce49e06", "packages": [], "packages-dev": [ { @@ -153,6 +153,54 @@ ], "time": "2014-08-11 04:32:36" }, + { + "name": "malkusch/php-mock", + "version": "dev-php-5.3", + "source": { + "type": "git", + "url": "https://github.com/malkusch/php-mock.git", + "reference": "37b301b4b479601232f3919920451c6e777c3264" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/malkusch/php-mock/zipball/37b301b4b479601232f3919920451c6e777c3264", + "reference": "37b301b4b479601232f3919920451c6e777c3264", + "shasum": "" + }, + "require": { + "php": ">=5.3" + }, + "require-dev": { + "phpunit/phpunit": ">=4" + }, + "type": "library", + "autoload": { + "psr-4": { + "malkusch\\phpmock\\": "classes/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "WTFPL-2.0" + ], + "authors": [ + { + "name": "Markus Malkusch", + "email": "markus@malkusch.de", + "homepage": "http://markus.malkusch.de", + "role": "Developer" + } + ], + "description": "Mock non deterministic built-in PHP functions (e.g. time() or rand())", + "homepage": "https://github.com/malkusch/php-mock", + "keywords": [ + "function", + "mock", + "stub", + "test" + ], + "time": "2014-12-01 18:01:18" + }, { "name": "phpunit/php-code-coverage", "version": "2.0.13", @@ -1208,7 +1256,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "malkusch/php-mock": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { From 3b326227ae426c12b0c598cdc8ba2d56f7cd51b9 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Thu, 5 Mar 2015 20:52:05 +0100 Subject: [PATCH 22/28] Remove setter from CompileCommand Also, let the Compiler be constructed with DI or by itself --- Mage/Command/BuiltIn/CompileCommand.php | 9 ++-- .../Command/BuiltIn/CompileCommandTest.php | 42 +++++++++---------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/Mage/Command/BuiltIn/CompileCommand.php b/Mage/Command/BuiltIn/CompileCommand.php index 56e00bb..ab92fd2 100644 --- a/Mage/Command/BuiltIn/CompileCommand.php +++ b/Mage/Command/BuiltIn/CompileCommand.php @@ -26,13 +26,12 @@ class CompileCommand extends AbstractCommand */ private $compiler; - public function __construct() + public function __construct(Compiler $compiler = null) { - $this->setCompiler(new Compiler()); - } + if ($compiler === null) { + $compiler = new Compiler(); + } - public function setCompiler(Compiler $compiler) - { $this->compiler = $compiler; } diff --git a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php index 9127833..f19dafa 100644 --- a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php @@ -5,14 +5,12 @@ namespace MageTest\Command\BuiltIn; use Mage\Command\BuiltIn\CompileCommand; use MageTest\TestHelper\BaseTest; use malkusch\phpmock\FixedValueFunction; -use malkusch\phpmock\Mock; use malkusch\phpmock\MockBuilder; /** * Class CompileCommandTest * @package MageTest\Command\BuiltIn * @coversDefaultClass Mage\Command\BuiltIn\CompileCommand - * @uses Mage\Compiler * @uses malkusch\phpmock\FixedValueFunction * @uses malkusch\phpmock\Mock * @uses malkusch\phpmock\MockBuilder @@ -21,11 +19,6 @@ use malkusch\phpmock\MockBuilder; */ class CompileCommandTest extends BaseTest { - /** - * @var CompileCommand - */ - private $compileCommand; - /** * @var FixedValueFunction */ @@ -36,8 +29,6 @@ class CompileCommandTest extends BaseTest */ public function before() { - $this->compileCommand = new CompileCommand(); - $this->iniGetValue = new FixedValueFunction(); $mockBuilder = new MockBuilder(); $iniGetMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn') @@ -52,30 +43,28 @@ class CompileCommandTest extends BaseTest /** * @covers ::__construct - * @covers ::setCompiler */ public function testConstruct() { - $compilerProperty = $this->getPropertyValue($this->compileCommand, 'compiler'); + $compileCommand = $this->getRawCompileCommand(); + $compilerProperty = $this->getPropertyValue($compileCommand, 'compiler'); + $this->assertInstanceOf('Mage\Compiler', $compilerProperty); } /** * @covers ::__construct - * @covers ::setCompiler */ - public function testSetCompiler() + public function testConstructWithNoParams() { - $compilerMock = $this->getMock('Mage\Compiler'); - $this->compileCommand->setCompiler($compilerMock); + $compileCommand = new CompileCommand(); + $compilerProperty = $this->getPropertyValue($compileCommand, 'compiler'); - $compilerProperty = $this->getPropertyValue($this->compileCommand, 'compiler'); - $this->assertEquals($compilerMock, $compilerProperty); + $this->assertInstanceOf('Mage\Compiler', $compilerProperty); } /** * @covers ::__construct - * @covers ::setCompiler * @covers ::run */ public function testRun() @@ -84,20 +73,20 @@ class CompileCommandTest extends BaseTest $expectedExitCode = 0; $this->expectOutputString($expectedOutput); + $this->iniGetValue->setValue(false); + $compilerMock = $this->getMock('Mage\Compiler'); $compilerMock->expects($this->once()) ->method('compile'); + $compileCommand = new CompileCommand($compilerMock); - $this->iniGetValue->setValue(false); - $this->compileCommand->setCompiler($compilerMock); - $actualExitCode = $this->compileCommand->run(); + $actualExitCode = $compileCommand->run(); $this->assertEquals($expectedExitCode, $actualExitCode); } /** * @covers ::__construct - * @covers ::setCompiler * @covers ::run */ public function testRunWhenPharReadonlyEnabled() @@ -107,8 +96,15 @@ class CompileCommandTest extends BaseTest $this->expectOutputString($expectedOutput); $this->iniGetValue->setValue(true); - $actualExitCode = $this->compileCommand->run(); + $compileCommand = $this->getRawCompileCommand(); + $actualExitCode = $compileCommand->run(); $this->assertEquals($expectedExitCode, $actualExitCode); } + + private function getRawCompileCommand() + { + $compilerMock = $this->getMock('Mage\Compiler'); + return new CompileCommand($compilerMock); + } } From 477705f4be623abda48495da7a342f20dafc411a Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Thu, 5 Mar 2015 21:03:29 +0100 Subject: [PATCH 23/28] Change autoloading to dev for tests classes --- composer.json | 6 +++++- composer.lock | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index d1a58cd..a8b9d75 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,11 @@ "psr-4": { "Mage\\": "./Mage", "Task\\": [".mage/tasks", "../../../.mage/tasks"], - "Command\\": [".mage/tasks", "../../../.mage/commands"], + "Command\\": [".mage/tasks", "../../../.mage/commands"] + } + }, + "autoload-dev": { + "psr-4": { "MageTest\\": "./tests/MageTest" } }, diff --git a/composer.lock b/composer.lock index 44ef421..7d89680 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "6b9e925b25d502eb4834a26e4ce49e06", + "hash": "caa09089c7b57461ed42e97a4449f2c6", "packages": [], "packages-dev": [ { From 937355ce8bef527f8e6b9cee04c62edb6a687c6f Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Thu, 5 Mar 2015 21:04:59 +0100 Subject: [PATCH 24/28] Change assignment assertion in accessors' tests to more restrict --- tests/MageTest/Command/AbstractCommandTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/MageTest/Command/AbstractCommandTest.php b/tests/MageTest/Command/AbstractCommandTest.php index 95afc37..28021d7 100644 --- a/tests/MageTest/Command/AbstractCommandTest.php +++ b/tests/MageTest/Command/AbstractCommandTest.php @@ -1,6 +1,7 @@ abstractCommand->setConfig($configMock); $actual = $this->getPropertyValue($this->abstractCommand, 'config'); - $this->assertEquals($configMock, $actual); + $this->assertSame($configMock, $actual); } /** @@ -47,6 +48,6 @@ class AbstractCommandTest extends BaseTest $this->setPropertyValue($this->abstractCommand, 'config', $configMock); $actual = $this->abstractCommand->getConfig(); - $this->assertEquals($configMock, $actual); + $this->assertSame($configMock, $actual); } } From c6e0d6762ca0f044935da81f734f8e0e54e9e48b Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Thu, 5 Mar 2015 21:11:31 +0100 Subject: [PATCH 25/28] Merge run test scenarios in UnlockCommandTests Expectations and setting properties should be in dataProvider --- .../Command/BuiltIn/UnlockCommandTest.php | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php b/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php index 8d92ced..1732811 100644 --- a/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/UnlockCommandTest.php @@ -27,6 +27,18 @@ class UnlockCommandTest extends BaseTest public static $fileExistsResult; public static $isFileExists; + public function runProvider() + { + return array( + 'happy_path' => array( + 'file_exists' => true, + ), + 'file_not_exists' => array( + 'file_exsits' => false + ) + ); + } + /** * @before */ @@ -87,38 +99,20 @@ class UnlockCommandTest extends BaseTest /** * @covers ::run + * @dataProvider runProvider */ - public function testRun() + public function testRun($fileExists) { $expectedOutput = "\tUnlocked deployment to production environment\n\n"; $this->expectOutputString($expectedOutput); $expectedLockFilePath = '/.mage/production.lock'; - self::$isFileExists = true; + self::$isFileExists = $fileExists; $actualExitCode = $this->unlockCommand->run(); $expectedExitCode = 0; - $this->assertTrue(self::$isUnlinkCalled); - $this->assertEquals($expectedExitCode, $actualExitCode); - $this->assertEquals($expectedLockFilePath, self::$fileExistsResult); - } - - /** - * @covers ::run - */ - public function testRunIfFileNotExist() - { - $expectedOutput = "\tUnlocked deployment to production environment\n\n"; - $this->expectOutputString($expectedOutput); - $expectedLockFilePath = '/.mage/production.lock'; - - self::$isFileExists = false; - - $actualExitCode = $this->unlockCommand->run(); - $expectedExitCode = 0; - - $this->assertFalse(self::$isUnlinkCalled); + $this->assertEquals(self::$isUnlinkCalled, $fileExists); $this->assertEquals($expectedExitCode, $actualExitCode); $this->assertEquals($expectedLockFilePath, self::$fileExistsResult); } From e7c0b87ccd40b150930f46247ef1cca85597c6e6 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Thu, 5 Mar 2015 21:29:20 +0100 Subject: [PATCH 26/28] Move testing setters and getters to BaseTest --- Mage/Command/AbstractCommand.php | 2 +- .../MageTest/Command/AbstractCommandTest.php | 10 +--- tests/MageTest/TestHelper/BaseTest.php | 55 +++++++++++++++++++ 3 files changed, 58 insertions(+), 9 deletions(-) diff --git a/Mage/Command/AbstractCommand.php b/Mage/Command/AbstractCommand.php index 3ed5a9d..f69b0ef 100644 --- a/Mage/Command/AbstractCommand.php +++ b/Mage/Command/AbstractCommand.php @@ -31,7 +31,7 @@ abstract class AbstractCommand * @return integer exit code * @throws \Exception */ - public abstract function run(); + abstract public function run(); /** * Sets the Loaded Configuration. diff --git a/tests/MageTest/Command/AbstractCommandTest.php b/tests/MageTest/Command/AbstractCommandTest.php index 28021d7..fed7df5 100644 --- a/tests/MageTest/Command/AbstractCommandTest.php +++ b/tests/MageTest/Command/AbstractCommandTest.php @@ -33,10 +33,7 @@ class AbstractCommandTest extends BaseTest public function testSetConfig() { $configMock = $this->getMock('Mage\Config'); - $this->abstractCommand->setConfig($configMock); - - $actual = $this->getPropertyValue($this->abstractCommand, 'config'); - $this->assertSame($configMock, $actual); + $this->doTestSetter($this->abstractCommand, 'config', $configMock); } /** @@ -45,9 +42,6 @@ class AbstractCommandTest extends BaseTest public function testGetConfig() { $configMock = $this->getMock('Mage\Config'); - $this->setPropertyValue($this->abstractCommand, 'config', $configMock); - - $actual = $this->abstractCommand->getConfig(); - $this->assertSame($configMock, $actual); + $this->doTestGetter($this->abstractCommand, 'config', $configMock); } } diff --git a/tests/MageTest/TestHelper/BaseTest.php b/tests/MageTest/TestHelper/BaseTest.php index 52dd372..de7bc17 100644 --- a/tests/MageTest/TestHelper/BaseTest.php +++ b/tests/MageTest/TestHelper/BaseTest.php @@ -65,4 +65,59 @@ abstract class BaseTest extends \PHPUnit_Framework_TestCase $configProperty->setAccessible(true); $configProperty->setValue($configMock); } + + /** + * Tests getter of given object for given property name and example value + * + * @param object $object Object instance + * @param string $propertyName Property name + * @param mixed $propertyValue Value to set + */ + final protected function doTestGetter($object, $propertyName, $propertyValue) + { + $this->setPropertyValue($object, $propertyName, $propertyValue); + $getterName = $this->getGetterName($propertyName); + + $actual = $object->$getterName(); + + $this->assertSame($propertyValue, $actual); + } + + /** + * Tests setter of given object for given property name and example value + * + * @param object $object Object instance + * @param string $propertyName Property name + * @param mixed $propertyValue Value to set + */ + final protected function doTestSetter($object, $propertyName, $propertyValue) + { + $setterName = $this->getSetterName($propertyName); + $object->$setterName($propertyValue); + + $actual = $this->getPropertyValue($object, $propertyName); + $this->assertSame($propertyValue, $actual); + } + + /** + * Returns the conventional getter name for given property name + * + * @param string $propertyName Property name + * @return string Getter method name + */ + final protected function getGetterName($propertyName) + { + return 'get' . ucfirst($propertyName); + } + + /** + * Returns the conventional setter name for given property name + * + * @param string $propertyName Property name + * @return string Getter method name + */ + final protected function getSetterName($propertyName) + { + return 'set' . ucfirst($propertyName); + } } From 3f02ebcfe4a2878d46103d7e19ee11a2b756675e Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sun, 8 Mar 2015 21:22:12 +0100 Subject: [PATCH 27/28] Add more restrictly tests for constructor --- .../Command/BuiltIn/CompileCommandTest.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php index f19dafa..44b4486 100644 --- a/tests/MageTest/Command/BuiltIn/CompileCommandTest.php +++ b/tests/MageTest/Command/BuiltIn/CompileCommandTest.php @@ -46,10 +46,13 @@ class CompileCommandTest extends BaseTest */ public function testConstruct() { - $compileCommand = $this->getRawCompileCommand(); + $compilerMock = $this->getMock('Mage\Compiler'); + $compileCommand = new CompileCommand($compilerMock); + $compilerProperty = $this->getPropertyValue($compileCommand, 'compiler'); $this->assertInstanceOf('Mage\Compiler', $compilerProperty); + $this->assertSame($compilerMock, $compilerProperty); } /** @@ -96,15 +99,10 @@ class CompileCommandTest extends BaseTest $this->expectOutputString($expectedOutput); $this->iniGetValue->setValue(true); - $compileCommand = $this->getRawCompileCommand(); + $compilerMock = $this->getMock('Mage\Compiler'); + $compileCommand = new CompileCommand($compilerMock); $actualExitCode = $compileCommand->run(); $this->assertEquals($expectedExitCode, $actualExitCode); } - - private function getRawCompileCommand() - { - $compilerMock = $this->getMock('Mage\Compiler'); - return new CompileCommand($compilerMock); - } } From c690ea3efebd870a1cd9ca5ec43f71caeb5ab400 Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Sun, 8 Mar 2015 21:43:11 +0100 Subject: [PATCH 28/28] Change scope of getGetterName and getSetterName methods --- tests/MageTest/TestHelper/BaseTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/MageTest/TestHelper/BaseTest.php b/tests/MageTest/TestHelper/BaseTest.php index de7bc17..d8db4dc 100644 --- a/tests/MageTest/TestHelper/BaseTest.php +++ b/tests/MageTest/TestHelper/BaseTest.php @@ -105,7 +105,7 @@ abstract class BaseTest extends \PHPUnit_Framework_TestCase * @param string $propertyName Property name * @return string Getter method name */ - final protected function getGetterName($propertyName) + private function getGetterName($propertyName) { return 'get' . ucfirst($propertyName); } @@ -116,7 +116,7 @@ abstract class BaseTest extends \PHPUnit_Framework_TestCase * @param string $propertyName Property name * @return string Getter method name */ - final protected function getSetterName($propertyName) + private function getSetterName($propertyName) { return 'set' . ucfirst($propertyName); }