diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index 954028c7269..9d86fcdfa5b 100644 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -182,7 +182,7 @@ protected static function _buildEngine($name) if ($config['fallback'] === $name) { throw new InvalidArgumentException( - sprintf('"%s" cache configuration cannot fallback to itself.', $name) + sprintf('"%s" cache configuration cannot fallback to itself.', $name), null, $e ); } diff --git a/src/Controller/Component/PaginatorComponent.php b/src/Controller/Component/PaginatorComponent.php index 3c1bfefd61f..c61b59a331a 100644 --- a/src/Controller/Component/PaginatorComponent.php +++ b/src/Controller/Component/PaginatorComponent.php @@ -205,7 +205,7 @@ public function paginate($object, array $settings = []) } catch (PageOutOfBoundsException $e) { $this->_setPagingParams(); - throw new NotFoundException(); + throw new NotFoundException(null, null, $e); } return $results; diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 95c31c51489..94ea84f6656 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -224,7 +224,7 @@ public function connect() try { return $this->_driver->connect(); } catch (\Exception $e) { - throw new MissingConnectionException(['reason' => $e->getMessage()]); + throw new MissingConnectionException(['reason' => $e->getMessage()], null, $e); } } diff --git a/src/Mailer/Transport/SmtpTransport.php b/src/Mailer/Transport/SmtpTransport.php index a72ff9cdaf0..ed0a36ef663 100644 --- a/src/Mailer/Transport/SmtpTransport.php +++ b/src/Mailer/Transport/SmtpTransport.php @@ -228,12 +228,12 @@ protected function _connect() } } catch (SocketException $e) { if ($config['tls']) { - throw new SocketException('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'); + throw new SocketException('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.', null, $e); } try { $this->_smtpSend("HELO {$host}", '250'); } catch (SocketException $e2) { - throw new SocketException('SMTP server did not accept the connection.'); + throw new SocketException('SMTP server did not accept the connection.', null, $e2); } } } @@ -252,12 +252,12 @@ protected function _auth() try { $this->_smtpSend(base64_encode($this->_config['username']), '334'); } catch (SocketException $e) { - throw new SocketException('SMTP server did not accept the username.'); + throw new SocketException('SMTP server did not accept the username.', null, $e); } try { $this->_smtpSend(base64_encode($this->_config['password']), '235'); } catch (SocketException $e) { - throw new SocketException('SMTP server did not accept the password.'); + throw new SocketException('SMTP server did not accept the password.', null, $e); } } elseif ($replyCode === '504') { throw new SocketException('SMTP authentication method not allowed, check if SMTP server requires TLS.'); diff --git a/src/Network/Socket.php b/src/Network/Socket.php index ca954d668eb..7c369f21356 100644 --- a/src/Network/Socket.php +++ b/src/Network/Socket.php @@ -435,7 +435,7 @@ public function enableCrypto($type, $clientOrServer = 'client', $enable = true) $enableCryptoResult = stream_socket_enable_crypto($this->connection, $enable, $this->_encryptMethods[$type . '_' . $clientOrServer]); } catch (Exception $e) { $this->setLastError(null, $e->getMessage()); - throw new SocketException($e->getMessage()); + throw new SocketException($e->getMessage(), null, $e); } if ($enableCryptoResult === true) { $this->encrypted = $enable; diff --git a/src/TestSuite/Fixture/FixtureManager.php b/src/TestSuite/Fixture/FixtureManager.php index 61836d793bd..88a786ad8e8 100644 --- a/src/TestSuite/Fixture/FixtureManager.php +++ b/src/TestSuite/Fixture/FixtureManager.php @@ -303,7 +303,7 @@ public function load($test) get_class($test), $e->getMessage() ); - throw new Exception($msg); + throw new Exception($msg, null, $e); } } } @@ -326,7 +326,7 @@ public function load($test) get_class($test), $e->getMessage() ); - throw new Exception($msg); + throw new Exception($msg, null, $e); } } }; @@ -344,7 +344,7 @@ public function load($test) get_class($test), $e->getMessage() ); - throw new Exception($msg); + throw new Exception($msg, null, $e); } } }; @@ -355,7 +355,7 @@ public function load($test) get_class($test), $e->getMessage() ); - throw new Exception($msg); + throw new Exception($msg, null, $e); } } diff --git a/src/View/Cell.php b/src/View/Cell.php index ba104442eb2..4c2be8a286e 100644 --- a/src/View/Cell.php +++ b/src/View/Cell.php @@ -215,7 +215,7 @@ public function render($template = null) try { return $this->View->render($template); } catch (MissingTemplateException $e) { - throw new MissingCellViewException(['file' => $template, 'name' => $name]); + throw new MissingCellViewException(['file' => $template, 'name' => $name], null, $e); } }; diff --git a/tests/TestCase/Cache/CacheTest.php b/tests/TestCase/Cache/CacheTest.php index dfb7affb6a5..d0f984f7a71 100644 --- a/tests/TestCase/Cache/CacheTest.php +++ b/tests/TestCase/Cache/CacheTest.php @@ -106,9 +106,6 @@ public function testCacheEngineFallbackToSelf() { $filename = tempnam(TMP, 'tmp_'); - $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('cannot fallback to itself'); - Cache::setConfig('tests', [ 'engine' => 'File', 'path' => $filename, @@ -116,10 +113,18 @@ public function testCacheEngineFallbackToSelf() 'fallback' => 'tests' ]); - Cache::engine('tests'); + $e = null; + try { + Cache::engine('tests'); + } catch (InvalidArgumentException $e) { + } Cache::drop('tests'); unlink($filename); + + $this->assertNotNull($e); + $this->assertStringEndsWith('cannot fallback to itself.', $e->getMessage()); + $this->assertInstanceOf('RunTimeException', $e->getPrevious()); } /** diff --git a/tests/TestCase/Controller/Component/PaginatorComponentTest.php b/tests/TestCase/Controller/Component/PaginatorComponentTest.php index dcb60c9cbc1..4bb7a2ecf5e 100644 --- a/tests/TestCase/Controller/Component/PaginatorComponentTest.php +++ b/tests/TestCase/Controller/Component/PaginatorComponentTest.php @@ -19,6 +19,7 @@ use Cake\Controller\Controller; use Cake\Datasource\ConnectionManager; use Cake\Datasource\EntityInterface; +use Cake\Datasource\Exception\PageOutOfBoundsException; use Cake\Datasource\Paginator; use Cake\Http\ServerRequest; use Cake\Network\Exception\NotFoundException; @@ -720,16 +721,21 @@ public function testOutOfRangePageNumberGetsClamped() $this->request->query['page'] = 3000; $table = TableRegistry::get('PaginatorPosts'); + + $e = null; try { $this->Paginator->paginate($table); - $this->fail('No exception raised'); } catch (NotFoundException $e) { - $this->assertEquals( - 1, - $this->request->params['paging']['PaginatorPosts']['page'], - 'Page number should not be 0' - ); } + + $this->assertEquals( + 1, + $this->request->params['paging']['PaginatorPosts']['page'], + 'Page number should not be 0' + ); + + $this->assertNotNull($e); + $this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious()); } /** @@ -744,16 +750,21 @@ public function testOutOfRangePageNumberStillProvidesPageCount() $this->request->query['page'] = 4; $table = TableRegistry::get('PaginatorPosts'); + + $e = null; try { $this->Paginator->paginate($table); - $this->fail('No exception raised'); } catch (NotFoundException $e) { - $this->assertEquals( - 3, - $this->request->params['paging']['PaginatorPosts']['pageCount'], - 'Page count number should not be 0' - ); } + + $this->assertEquals( + 3, + $this->request->params['paging']['PaginatorPosts']['pageCount'], + 'Page count number should not be 0' + ); + + $this->assertNotNull($e); + $this->assertInstanceOf(PageOutOfBoundsException::class, $e->getPrevious()); } /** diff --git a/tests/TestCase/Database/ConnectionTest.php b/tests/TestCase/Database/ConnectionTest.php index b4e76bdd737..43ac47811fe 100644 --- a/tests/TestCase/Database/ConnectionTest.php +++ b/tests/TestCase/Database/ConnectionTest.php @@ -16,6 +16,7 @@ use Cake\Database\Connection; use Cake\Database\Driver\Mysql; +use Cake\Database\Exception\MissingConnectionException; use Cake\Database\Exception\NestedTransactionRollbackException; use Cake\Database\Log\LoggingStatement; use Cake\Database\Log\QueryLogger; @@ -161,7 +162,6 @@ public function testDriverOptionClassNameSupport() /** * Tests that connecting with invalid credentials or database name throws an exception * - * @expectedException \Cake\Database\Exception\MissingConnectionException * @return void */ public function testWrongCredentials() @@ -169,7 +169,16 @@ public function testWrongCredentials() $config = ConnectionManager::getConfig('test'); $this->skipIf(isset($config['url']), 'Datasource has dsn, skipping.'); $connection = new Connection(['database' => '/dev/nonexistent'] + ConnectionManager::getConfig('test')); - $connection->connect(); + + $e = null; + try { + $connection->connect(); + } catch (MissingConnectionException $e) { + } + + $this->assertNotNull($e); + $this->assertStringStartsWith('Connection to database could not be established:', $e->getMessage()); + $this->assertInstanceOf('PDOException', $e->getPrevious()); } /** diff --git a/tests/TestCase/Mailer/Transport/SmtpTransportTest.php b/tests/TestCase/Mailer/Transport/SmtpTransportTest.php index dcd4d403c17..c7507f38100 100644 --- a/tests/TestCase/Mailer/Transport/SmtpTransportTest.php +++ b/tests/TestCase/Mailer/Transport/SmtpTransportTest.php @@ -16,6 +16,7 @@ use Cake\Mailer\Email; use Cake\Mailer\Transport\SmtpTransport; +use Cake\Network\Exception\SocketException; use Cake\Network\Socket; use Cake\TestSuite\TestCase; @@ -124,8 +125,6 @@ public function testConnectEhloTls() /** * testConnectEhloTlsOnNonTlsServer method * - * @expectedException \Cake\Network\Exception\SocketException - * @expectedExceptionMessage SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS. * @return void */ public function testConnectEhloTlsOnNonTlsServer() @@ -138,7 +137,17 @@ public function testConnectEhloTlsOnNonTlsServer() $this->socket->expects($this->at(4))->method('write')->with("STARTTLS\r\n"); $this->socket->expects($this->at(5))->method('read') ->will($this->returnValue("500 5.3.3 Unrecognized command\r\n")); - $this->SmtpTransport->connect(); + + $e = null; + try { + $this->SmtpTransport->connect(); + } catch (SocketException $e) { + } + + $this->assertNotNull($e); + $this->assertEquals('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.', $e->getMessage()); + $this->assertInstanceOf(SocketException::class, $e->getPrevious()); + $this->assertContains('500 5.3.3 Unrecognized command', $e->getPrevious()->getMessage()); } /** @@ -180,8 +189,6 @@ public function testConnectHelo() /** * testConnectFail method * - * @expectedException \Cake\Network\Exception\SocketException - * @expectedExceptionMessage SMTP server did not accept the connection. * @return void */ public function testConnectFail() @@ -192,7 +199,17 @@ public function testConnectFail() $this->socket->expects($this->at(3))->method('read')->will($this->returnValue("200 Not Accepted\r\n")); $this->socket->expects($this->at(4))->method('write')->with("HELO localhost\r\n"); $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("200 Not Accepted\r\n")); - $this->SmtpTransport->connect(); + + $e = null; + try { + $this->SmtpTransport->connect(); + } catch (SocketException $e) { + } + + $this->assertNotNull($e); + $this->assertEquals('SMTP server did not accept the connection.', $e->getMessage()); + $this->assertInstanceOf(SocketException::class, $e->getPrevious()); + $this->assertContains('200 Not Accepted', $e->getPrevious()->getMessage()); } /** @@ -263,8 +280,6 @@ public function testAuthBadSequence() /** * testAuthBadUsername method * - * @expectedException \Cake\Network\Exception\SocketException - * @expectedExceptionMessage SMTP server did not accept the username. * @return void */ public function testAuthBadUsername() @@ -275,14 +290,22 @@ public function testAuthBadUsername() $this->socket->expects($this->at(3))->method('read') ->will($this->returnValue("535 5.7.8 Authentication failed\r\n")); $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']); - $this->SmtpTransport->auth(); + + $e = null; + try { + $this->SmtpTransport->auth(); + } catch (SocketException $e) { + } + + $this->assertNotNull($e); + $this->assertEquals('SMTP server did not accept the username.', $e->getMessage()); + $this->assertInstanceOf(SocketException::class, $e->getPrevious()); + $this->assertContains('535 5.7.8 Authentication failed', $e->getPrevious()->getMessage()); } /** * testAuthBadPassword method * - * @expectedException \Cake\Network\Exception\SocketException - * @expectedExceptionMessage SMTP server did not accept the password. * @return void */ public function testAuthBadPassword() @@ -294,7 +317,17 @@ public function testAuthBadPassword() $this->socket->expects($this->at(4))->method('write')->with("c3Rvcnk=\r\n"); $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n")); $this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']); - $this->SmtpTransport->auth(); + + $e = null; + try { + $this->SmtpTransport->auth(); + } catch (SocketException $e) { + } + + $this->assertNotNull($e); + $this->assertEquals('SMTP server did not accept the password.', $e->getMessage()); + $this->assertInstanceOf(SocketException::class, $e->getPrevious()); + $this->assertContains('535 5.7.8 Authentication failed', $e->getPrevious()->getMessage()); } /** diff --git a/tests/TestCase/Network/SocketTest.php b/tests/TestCase/Network/SocketTest.php index 3eead81e52d..7be67557514 100644 --- a/tests/TestCase/Network/SocketTest.php +++ b/tests/TestCase/Network/SocketTest.php @@ -277,7 +277,6 @@ public function testEnableCryptoSocketExceptionNoSsl() /** * testEnableCryptoSocketExceptionNoTls * - * @expectedException \Cake\Network\Exception\SocketException * @return void */ public function testEnableCryptoSocketExceptionNoTls() @@ -287,7 +286,15 @@ public function testEnableCryptoSocketExceptionNoTls() // testing exception on no ssl socket server for ssl and tls methods $this->Socket = new Socket($configNoSslOrTls); $this->Socket->connect(); - $this->Socket->enableCrypto('tls', 'client'); + + $e = null; + try { + $this->Socket->enableCrypto('tls', 'client'); + } catch (SocketException $e) { + } + + $this->assertNotNull($e); + $this->assertInstanceOf('Exception', $e->getPrevious()); } /** diff --git a/tests/TestCase/TestSuite/FixtureManagerTest.php b/tests/TestCase/TestSuite/FixtureManagerTest.php index 37af362fc09..d4e1da85fd9 100644 --- a/tests/TestCase/TestSuite/FixtureManagerTest.php +++ b/tests/TestCase/TestSuite/FixtureManagerTest.php @@ -14,6 +14,7 @@ */ namespace Cake\Test\TestSuite; +use Cake\Core\Exception\Exception as CakeException; use Cake\Core\Plugin; use Cake\Database\Schema\Table; use Cake\Datasource\ConnectionManager; @@ -22,6 +23,7 @@ use Cake\TestSuite\Fixture\FixtureManager; use Cake\TestSuite\Stub\ConsoleOutput; use Cake\TestSuite\TestCase; +use PDOException; /** * Fixture manager test case. @@ -387,4 +389,105 @@ public function testLoadSingle() $this->assertCount(4, $results); $this->manager->unload($test); } + + /** + * Test exception on load + * + * @return void + */ + public function testExceptionOnLoad() + { + $test = $this->getMockBuilder('Cake\TestSuite\TestCase')->getMock(); + $test->fixtures = ['core.products']; + + $manager = $this->getMockBuilder(FixtureManager::class) + ->setMethods(['_runOperation']) + ->getMock(); + $manager->expects($this->any()) + ->method('_runOperation') + ->will($this->returnCallback(function () { + throw new PDOException('message'); + })); + + $manager->fixturize($test); + + $e = null; + try { + $manager->load($test); + } catch (CakeException $e) { + } + + $this->assertNotNull($e); + $this->assertRegExp('/^Unable to insert fixtures for "Mock_TestCase_\w+" test case. message$/D', $e->getMessage()); + $this->assertInstanceOf('PDOException', $e->getPrevious()); + } + + /** + * Test exception on load fixture + * + * @dataProvider loadErrorMessageProvider + * @return void + */ + public function testExceptionOnLoadFixture($method, $expectedMessage) + { + $fixture = $this->getMockBuilder('Cake\Test\Fixture\ProductsFixture') + ->setMethods([$method]) + ->getMock(); + $fixture->expects($this->once()) + ->method($method) + ->will($this->returnCallback(function () { + throw new PDOException('message'); + })); + + $fixtures = [ + 'core.products' => $fixture, + ]; + + $test = $this->getMockBuilder('Cake\TestSuite\TestCase')->getMock(); + $test->fixtures = array_keys($fixtures); + + $manager = $this->getMockBuilder(FixtureManager::class) + ->setMethods(['_fixtureConnections']) + ->getMock(); + $manager->expects($this->any()) + ->method('_fixtureConnections') + ->will($this->returnValue([ + 'test' => $fixtures, + ])); + $manager->fixturize($test); + $manager->loadSingle('Products'); + + $e = null; + try { + $manager->load($test); + } catch (CakeException $e) { + } + + $this->assertNotNull($e); + $this->assertRegExp($expectedMessage, $e->getMessage()); + $this->assertInstanceOf('PDOException', $e->getPrevious()); + } + + /** + * Data provider for testExceptionOnLoadFixture + * + * @return array + */ + public function loadErrorMessageProvider() + { + return [ + [ + 'createConstraints', + '/^Unable to create constraints for fixture "Mock_ProductsFixture_\w+" in "Mock_TestCase_\w+" test case: \nmessage$/D', + ], + [ + 'dropConstraints', + '/^Unable to drop constraints for fixture "Mock_ProductsFixture_\w+" in "Mock_TestCase_\w+" test case: \nmessage$/D', + ], + [ + 'insert', + '/^Unable to insert fixture "Mock_ProductsFixture_\w+" in "Mock_TestCase_\w+" test case: \nmessage$/D', + ], + ]; + } } diff --git a/tests/TestCase/View/CellTest.php b/tests/TestCase/View/CellTest.php index a0fa8b9aa4b..a5a25e198a9 100644 --- a/tests/TestCase/View/CellTest.php +++ b/tests/TestCase/View/CellTest.php @@ -18,6 +18,8 @@ use Cake\Core\Plugin; use Cake\TestSuite\TestCase; use Cake\View\View; +use Cake\View\Exception\MissingCellViewException; +use Cake\View\Exception\MissingTemplateException; use TestApp\Controller\CellTraitTestController; use TestApp\View\CustomJsonView; @@ -192,13 +194,21 @@ public function testCellManualRender() /** * Tests manual render() invocation with error * - * @expectedException \Cake\View\Exception\MissingCellViewException * @return void */ public function testCellManualRenderError() { $cell = $this->View->cell('Articles'); - $cell->render('derp'); + + $e = null; + try { + $cell->render('derp'); + } catch (MissingCellViewException $e) { + } + + $this->assertNotNull($e); + $this->assertEquals('Cell view file "derp" is missing.', $e->getMessage()); + $this->assertInstanceOf(MissingTemplateException::class, $e->getPrevious()); } /**