From eefd3ac84771fb282d65964edb09b576f70539ed Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Sat, 15 Jul 2017 23:23:14 +0200 Subject: [PATCH 1/9] adapter for the getMock() depricated in phpunit --- lib/Cake/TestSuite/CakeTestCase.php | 63 +++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index 94d825c38..646da1691 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -715,6 +715,69 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { } // @codingStandardsIgnoreEnd +/** + * Returns a mock object for the specified class. + * + * @param string $originalClassName + * @param array $methods By default, all methods of the given class are replaced + * with a test double that just returns NULL unless a return value is configured + * using will($this->returnValue()), for instance. + * When the second (optional) parameter is provided, only the methods whose names + * are in the array are replaced with a configurable test double. The behavior + * of the other methods is not changed. Providing NULL as the parameter means + * that no methods will be replaced. + * @param array $arguments The third (optional) parameter may hold a parameter + * array that is passed to the original class' constructor (which is not replaced + * with a dummy implementation by default). + * @param string $mockClassName The fourth (optional) parameter can be used to + * specify a class name for the generated test double class. + * @param boolean $callOriginalConstructor The fifth (optional) parameter can be + * used to disable the call to the original class' constructor. + * @param boolean $callOriginalClone The sixth (optional) parameter can be used + * to disable the call to the original class' clone constructor. + * @param boolean $callAutoload The seventh (optional) parameter can be used to + * disable __autoload() during the generation of the test double class. + * @return object + * @throws InvalidArgumentException + * @deprecated Use `getMockBuilder()` or `createMock` in new unit tests. + * @see https://phpunit.de/manual/current/en/test-doubles.html + */ + public function getMock($originalClassName, $methods = array(), + array $arguments = array(), $mockClassName = '', + $callOriginalConstructor = true, $callOriginalClone = true, + $callAutoload = true, $cloneArguments = false, + $callOriginalMethods = false, $proxyTarget = null) { + $MockBuilder = $this->getMockBuilder($originalClassName); + if (!empty($methods)) { + $MockBuilder = $MockBuilder->setMethods($methods); + } + if (!empty($arguments)) { + $MockBuilder = $MockBuilder->setConstructorArgs($arguments); + } + if ($mockClassName != '') { + $MockBuilder = $MockBuilder->setMockClassName($mockClassName); + } + if ($callOriginalConstructor !== true) { + $MockBuilder= $MockBuilder->disableOriginalConstructor(); + } + if ($callOriginalClone !== true) { + $MockBuilder= $MockBuilder->disableOriginalClone(); + } + if ($callAutoload !== true) { + $MockBuilder= $MockBuilder->disableAutoload(); + } + if ($cloneArguments) { + throw new CakeException('$cloneArguments parameter is not supported'); + } + if ($callOriginalMethods) { + throw new CakeException('$callOriginalMethods parameter is not supported'); + } + if ($proxyTarget !== null) { + throw new CakeException('$proxyTarget parameter is not supported'); + } + return $MockBuilder->getMock(); + } + /** * Mock a model, maintain fixtures and table association * From 93696b65e444e9f374adb02394dcf0262a97b6d3 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Sun, 16 Jul 2017 00:09:36 +0200 Subject: [PATCH 2/9] Fixed indefinite loop in getMock(). --- lib/Cake/TestSuite/CakeTestCase.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index 646da1691..3f7d3ad51 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -737,6 +737,9 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { * to disable the call to the original class' clone constructor. * @param boolean $callAutoload The seventh (optional) parameter can be used to * disable __autoload() during the generation of the test double class. + * @param boolean $cloneArguments Not supported. + * @param boolean $callOriginalMethods Not supported. + * @param string $proxyTarget Not supported. * @return object * @throws InvalidArgumentException * @deprecated Use `getMockBuilder()` or `createMock` in new unit tests. @@ -747,6 +750,12 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = false, $callOriginalMethods = false, $proxyTarget = null) { + $phpUnitVersion = PHPUnit_Runner_Version::id(); + if ($phpUnitVersion < '5.7.0') { + return parent::getMock($originalClassName, $methods, $arguments, + $mockClassName, $callOriginalConstructor, $callOriginalClone, + $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget); + } $MockBuilder = $this->getMockBuilder($originalClassName); if (!empty($methods)) { $MockBuilder = $MockBuilder->setMethods($methods); From 7ba52d0c535cd732a71b9009d82e972742f36be9 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Sun, 16 Jul 2017 00:27:20 +0200 Subject: [PATCH 3/9] Fixed code style. --- lib/Cake/TestSuite/CakeTestCase.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index 3f7d3ad51..c8d01bf19 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -718,7 +718,7 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { /** * Returns a mock object for the specified class. * - * @param string $originalClassName + * @param string $originalClassName The class name of the object to be mocked. * @param array $methods By default, all methods of the given class are replaced * with a test double that just returns NULL unless a return value is configured * using will($this->returnValue()), for instance. @@ -731,18 +731,18 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { * with a dummy implementation by default). * @param string $mockClassName The fourth (optional) parameter can be used to * specify a class name for the generated test double class. - * @param boolean $callOriginalConstructor The fifth (optional) parameter can be + * @param bool $callOriginalConstructor The fifth (optional) parameter can be * used to disable the call to the original class' constructor. - * @param boolean $callOriginalClone The sixth (optional) parameter can be used + * @param bool $callOriginalClone The sixth (optional) parameter can be used * to disable the call to the original class' clone constructor. - * @param boolean $callAutoload The seventh (optional) parameter can be used to + * @param bool $callAutoload The seventh (optional) parameter can be used to * disable __autoload() during the generation of the test double class. - * @param boolean $cloneArguments Not supported. - * @param boolean $callOriginalMethods Not supported. + * @param bool $cloneArguments Not supported. + * @param bool $callOriginalMethods Not supported. * @param string $proxyTarget Not supported. * @return object - * @throws InvalidArgumentException - * @deprecated Use `getMockBuilder()` or `createMock` in new unit tests. + * @throws InvalidArgumentException When not supported parameters are set. + * @deprecated Use `getMockBuilder()` or `createMock()` in new unit tests. * @see https://phpunit.de/manual/current/en/test-doubles.html */ public function getMock($originalClassName, $methods = array(), @@ -767,22 +767,22 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { $MockBuilder = $MockBuilder->setMockClassName($mockClassName); } if ($callOriginalConstructor !== true) { - $MockBuilder= $MockBuilder->disableOriginalConstructor(); + $MockBuilder = $MockBuilder->disableOriginalConstructor(); } if ($callOriginalClone !== true) { - $MockBuilder= $MockBuilder->disableOriginalClone(); + $MockBuilder = $MockBuilder->disableOriginalClone(); } if ($callAutoload !== true) { - $MockBuilder= $MockBuilder->disableAutoload(); + $MockBuilder = $MockBuilder->disableAutoload(); } if ($cloneArguments) { - throw new CakeException('$cloneArguments parameter is not supported'); + throw new InvalidArgumentException('$cloneArguments parameter is not supported'); } if ($callOriginalMethods) { - throw new CakeException('$callOriginalMethods parameter is not supported'); + throw new InvalidArgumentException('$callOriginalMethods parameter is not supported'); } if ($proxyTarget !== null) { - throw new CakeException('$proxyTarget parameter is not supported'); + throw new InvalidArgumentException('$proxyTarget parameter is not supported'); } return $MockBuilder->getMock(); } From 15f0fe31b1beb213615891d5375cc4de68325cbe Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Sun, 16 Jul 2017 16:02:31 +0200 Subject: [PATCH 4/9] Fixed PHPUnit 5.7 warnings --- lib/Cake/Test/Case/Console/ShellTest.php | 16 +++------------- lib/Cake/Test/Case/Routing/DispatcherTest.php | 6 ------ .../Case/Routing/Filter/AssetDispatcherTest.php | 7 ------- 3 files changed, 3 insertions(+), 26 deletions(-) diff --git a/lib/Cake/Test/Case/Console/ShellTest.php b/lib/Cake/Test/Case/Console/ShellTest.php index c79ac8d97..e1349c4ba 100644 --- a/lib/Cake/Test/Case/Console/ShellTest.php +++ b/lib/Cake/Test/Case/Console/ShellTest.php @@ -749,13 +749,10 @@ class ShellTest extends CakeTestCase { public function testRunCommandBaseclassMethod() { $Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false); $Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false); - $Parser->expects($this->once())->method('help'); $Mock->expects($this->once())->method('getOptionParser') ->will($this->returnValue($Parser)); - $Mock->expects($this->never())->method('hr'); $Mock->expects($this->once())->method('out'); - $Mock->runCommand('hr', array()); } @@ -767,13 +764,10 @@ class ShellTest extends CakeTestCase { public function testRunCommandMissingMethod() { $Mock = $this->getMock('Shell', array('startup', 'getOptionParser', 'out'), array(), '', false); $Parser = $this->getMock('ConsoleOptionParser', array(), array(), '', false); - $Parser->expects($this->once())->method('help'); - $Mock->expects($this->never())->method('idontexist'); $Mock->expects($this->once())->method('getOptionParser') ->will($this->returnValue($Parser)); $Mock->expects($this->once())->method('out'); - $result = $Mock->runCommand('idontexist', array()); $this->assertFalse($result); } @@ -1003,31 +997,27 @@ TEXT; /** * Test that shell loggers do not get overridden in constructor if already configured - * + * * @return void */ public function testShellLoggersDoNotGetOverridden() { $shell = $this->getMock( "Shell", array( "_loggerIsConfigured", - "configureStdOutLogger", - "configureStdErrLogger", + "_configureStdOutLogger", + "_configureStdErrLogger", ), array(), "", false ); - $shell->expects($this->exactly(2)) ->method("_loggerIsConfigured") ->will($this->returnValue(true)); - $shell->expects($this->never()) ->method("_configureStdOutLogger"); - $shell->expects($this->never()) ->method("_configureStdErrLogger"); - $shell->__construct(); } } diff --git a/lib/Cake/Test/Case/Routing/DispatcherTest.php b/lib/Cake/Test/Case/Routing/DispatcherTest.php index e7d13b0ea..05c12c532 100644 --- a/lib/Cake/Test/Case/Routing/DispatcherTest.php +++ b/lib/Cake/Test/Case/Routing/DispatcherTest.php @@ -889,18 +889,12 @@ class DispatcherTest extends CakeTestCase { '_clearBuffer', '_flushBuffer' )); - - $response->expects($this->never()) - ->method('body'); - $response->expects($this->exactly(1)) ->method('_isActive') ->will($this->returnValue(true)); - ob_start(); $Dispatcher->dispatch($request, $response); $result = ob_get_clean(); - $this->assertEquals("/* this is the test asset css file */\n", $result); } diff --git a/lib/Cake/Test/Case/Routing/Filter/AssetDispatcherTest.php b/lib/Cake/Test/Case/Routing/Filter/AssetDispatcherTest.php index eb24861c1..222b39813 100644 --- a/lib/Cake/Test/Case/Routing/Filter/AssetDispatcherTest.php +++ b/lib/Cake/Test/Case/Routing/Filter/AssetDispatcherTest.php @@ -154,13 +154,10 @@ class AssetDispatcherTest extends CakeTestCase { $response = $this->getMock('CakeResponse', array('_sendHeader', 'checkNotModified')); $request = new CakeRequest('theme/test_theme/img/cake.power.gif'); - $response->expects($this->once())->method('checkNotModified') ->with($request) ->will($this->returnValue(true)); - $response->expects($this->never())->method('send'); $event = new CakeEvent('DispatcherTest', $this, compact('request', 'response')); - $this->assertSame($response, $filter->beforeDispatch($event)); $this->assertEquals($time->format('D, j M Y H:i:s') . ' GMT', $response->modified()); } @@ -193,13 +190,9 @@ class AssetDispatcherTest extends CakeTestCase { 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS), 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS) ), App::RESET); - $response = $this->getMock('CakeResponse', array('_sendHeader')); $request = new CakeRequest('theme/test_theme/../../../../../../VERSION.txt'); $event = new CakeEvent('Dispatcher.beforeRequest', $this, compact('request', 'response')); - - $response->expects($this->never())->method('send'); - $filter = new AssetDispatcher(); $this->assertNull($filter->beforeDispatch($event)); $this->assertFalse($event->isStopped()); From 5e92034ad878d8c0fe08df98cbcdfa3e27ea9676 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Tue, 18 Jul 2017 22:31:28 +0200 Subject: [PATCH 5/9] improved code style --- lib/Cake/TestSuite/CakeTestCase.php | 62 ++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/lib/Cake/TestSuite/CakeTestCase.php b/lib/Cake/TestSuite/CakeTestCase.php index c8d01bf19..24b2838f1 100644 --- a/lib/Cake/TestSuite/CakeTestCase.php +++ b/lib/Cake/TestSuite/CakeTestCase.php @@ -737,25 +737,14 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { * to disable the call to the original class' clone constructor. * @param bool $callAutoload The seventh (optional) parameter can be used to * disable __autoload() during the generation of the test double class. - * @param bool $cloneArguments Not supported. - * @param bool $callOriginalMethods Not supported. - * @param string $proxyTarget Not supported. * @return object - * @throws InvalidArgumentException When not supported parameters are set. * @deprecated Use `getMockBuilder()` or `createMock()` in new unit tests. * @see https://phpunit.de/manual/current/en/test-doubles.html */ - public function getMock($originalClassName, $methods = array(), + protected function _buildMock($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, - $callAutoload = true, $cloneArguments = false, - $callOriginalMethods = false, $proxyTarget = null) { - $phpUnitVersion = PHPUnit_Runner_Version::id(); - if ($phpUnitVersion < '5.7.0') { - return parent::getMock($originalClassName, $methods, $arguments, - $mockClassName, $callOriginalConstructor, $callOriginalClone, - $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget); - } + $callAutoload = true) { $MockBuilder = $this->getMockBuilder($originalClassName); if (!empty($methods)) { $MockBuilder = $MockBuilder->setMethods($methods); @@ -775,6 +764,50 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { if ($callAutoload !== true) { $MockBuilder = $MockBuilder->disableAutoload(); } + return $MockBuilder->getMock(); + } + +/** + * Returns a mock object for the specified class. + * + * @param string $originalClassName The class name of the object to be mocked. + * @param array $methods By default, all methods of the given class are replaced + * with a test double that just returns NULL unless a return value is configured + * using will($this->returnValue()), for instance. + * When the second (optional) parameter is provided, only the methods whose names + * are in the array are replaced with a configurable test double. The behavior + * of the other methods is not changed. Providing NULL as the parameter means + * that no methods will be replaced. + * @param array $arguments The third (optional) parameter may hold a parameter + * array that is passed to the original class' constructor (which is not replaced + * with a dummy implementation by default). + * @param string $mockClassName The fourth (optional) parameter can be used to + * specify a class name for the generated test double class. + * @param bool $callOriginalConstructor The fifth (optional) parameter can be + * used to disable the call to the original class' constructor. + * @param bool $callOriginalClone The sixth (optional) parameter can be used + * to disable the call to the original class' clone constructor. + * @param bool $callAutoload The seventh (optional) parameter can be used to + * disable __autoload() during the generation of the test double class. + * @param bool $cloneArguments Not supported. + * @param bool $callOriginalMethods Not supported. + * @param string $proxyTarget Not supported. + * @return object + * @throws InvalidArgumentException When not supported parameters are set. + * @deprecated Use `getMockBuilder()` or `createMock()` in new unit tests. + * @see https://phpunit.de/manual/current/en/test-doubles.html + */ + public function getMock($originalClassName, $methods = array(), + array $arguments = array(), $mockClassName = '', + $callOriginalConstructor = true, $callOriginalClone = true, + $callAutoload = true, $cloneArguments = false, + $callOriginalMethods = false, $proxyTarget = null) { + $phpUnitVersion = PHPUnit_Runner_Version::id(); + if (version_compare($phpUnitVersion, '5.7.0', '<')) { + return parent::getMock($originalClassName, $methods, $arguments, + $mockClassName, $callOriginalConstructor, $callOriginalClone, + $callAutoload, $cloneArguments, $callOriginalMethods, $proxyTarget); + } if ($cloneArguments) { throw new InvalidArgumentException('$cloneArguments parameter is not supported'); } @@ -784,7 +817,8 @@ abstract class CakeTestCase extends PHPUnit_Framework_TestCase { if ($proxyTarget !== null) { throw new InvalidArgumentException('$proxyTarget parameter is not supported'); } - return $MockBuilder->getMock(); + return $this->_buildMock($originalClassName, $methods, $arguments, + $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload); } /** From 31489a75d662cb2adc3f398ba08251fc6fecadf7 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Wed, 19 Jul 2017 23:51:27 +0200 Subject: [PATCH 6/9] Adjusted phpunit version in composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5b3a2624f..bfb2982ea 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-mcrypt": "*" }, "require-dev": { - "phpunit/phpunit": "3.7.*", + "phpunit/phpunit": "^5.0", "cakephp/cakephp-codesniffer": "^1.0.0" }, "config": { From 7dae9e24dac90ba5c55db17e96cabf9d1a80c1d0 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Thu, 20 Jul 2017 22:21:01 +0200 Subject: [PATCH 7/9] improved loading of composer autoload --- app/Config/bootstrap.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/Config/bootstrap.php b/app/Config/bootstrap.php index b4fb5290a..67cdf2057 100644 --- a/app/Config/bootstrap.php +++ b/app/Config/bootstrap.php @@ -22,6 +22,17 @@ * @license https://opensource.org/licenses/mit-license.php MIT License */ +/** + * Load Composer autoload. + * + * Remove and re-prepend CakePHP's autoloader as Composer thinks it is the most important. + * @see http://goo.gl/kKVJO7 + * @see http://book.cakephp.org/2.0/en/installation/advanced-installation.html#installing-cakephp-with-composer + */ +require ROOT . DS . 'vendors/autoload.php'; +spl_autoload_unregister(array('App', 'load')); +spl_autoload_register(array('App', 'load'), true, true); + // Setup a 'default' cache configuration for use in the application. Cache::config('default', array('engine' => 'File')); From 5765f4e72adb37da412af245296f53898733be5e Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Thu, 20 Jul 2017 22:25:56 +0200 Subject: [PATCH 8/9] adjusted phpunit version in composer config --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index bfb2982ea..38bc7dc42 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "ext-mcrypt": "*" }, "require-dev": { - "phpunit/phpunit": "^5.0", + "phpunit/phpunit": "<6.0.0", "cakephp/cakephp-codesniffer": "^1.0.0" }, "config": { From ddcca8cebca72f5b286441a24c961753c74602f2 Mon Sep 17 00:00:00 2001 From: Val Bancer Date: Thu, 27 Jul 2017 21:10:26 +0200 Subject: [PATCH 9/9] removed vendor autoload --- app/Config/bootstrap.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/app/Config/bootstrap.php b/app/Config/bootstrap.php index 67cdf2057..b4fb5290a 100644 --- a/app/Config/bootstrap.php +++ b/app/Config/bootstrap.php @@ -22,17 +22,6 @@ * @license https://opensource.org/licenses/mit-license.php MIT License */ -/** - * Load Composer autoload. - * - * Remove and re-prepend CakePHP's autoloader as Composer thinks it is the most important. - * @see http://goo.gl/kKVJO7 - * @see http://book.cakephp.org/2.0/en/installation/advanced-installation.html#installing-cakephp-with-composer - */ -require ROOT . DS . 'vendors/autoload.php'; -spl_autoload_unregister(array('App', 'load')); -spl_autoload_register(array('App', 'load'), true, true); - // Setup a 'default' cache configuration for use in the application. Cache::config('default', array('engine' => 'File'));