ExceptionsTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * ExceptionsTest file
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  13. * @link https://cakephp.org CakePHP(tm) Project
  14. * @since 3.5.0
  15. * @license https://opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase;
  18. use Cake\Error\FatalErrorException;
  19. use Cake\Error\PHP7ErrorException;
  20. use Cake\ORM\Entity;
  21. use Cake\ORM\Exception\PersistenceFailedException;
  22. use Cake\TestSuite\TestCase;
  23. use Error;
  24. use Exception;
  25. class ExceptionsTest extends TestCase
  26. {
  27. /**
  28. * Tests simple exceptions work.
  29. *
  30. * @dataProvider exceptionProvider
  31. * @param $class The exception class name
  32. * @param $defaultCode The default exception code
  33. * @return void
  34. */
  35. public function testSimpleException($class, $defaultCode)
  36. {
  37. $previous = new Exception();
  38. $exception = new $class('message', 100, $previous);
  39. $this->assertSame('message', $exception->getMessage());
  40. $this->assertSame(100, $exception->getCode());
  41. $this->assertSame($previous, $exception->getPrevious());
  42. $exception = new $class('message', null, $previous);
  43. $this->assertSame('message', $exception->getMessage());
  44. $this->assertSame($defaultCode, $exception->getCode());
  45. $this->assertSame($previous, $exception->getPrevious());
  46. }
  47. /**
  48. * Tests FatalErrorException works.
  49. *
  50. * @return void
  51. */
  52. public function testFatalErrorException()
  53. {
  54. $previous = new Exception();
  55. $exception = new FatalErrorException('message', 100, __FILE__, 1, $previous);
  56. $this->assertSame('message', $exception->getMessage());
  57. $this->assertSame(100, $exception->getCode());
  58. $this->assertSame(__FILE__, $exception->getFile());
  59. $this->assertSame(1, $exception->getLine());
  60. $this->assertSame($previous, $exception->getPrevious());
  61. $exception = new FatalErrorException('message', null, __FILE__, 1, $previous);
  62. $this->assertSame('message', $exception->getMessage());
  63. $this->assertSame(500, $exception->getCode());
  64. $this->assertSame(__FILE__, $exception->getFile());
  65. $this->assertSame(1, $exception->getLine());
  66. $this->assertSame($previous, $exception->getPrevious());
  67. }
  68. /**
  69. * Tests PHP7ErrorException works.
  70. *
  71. * @return void
  72. */
  73. public function testPHP7ErrorException()
  74. {
  75. $this->skipIf(version_compare(PHP_VERSION, '7.0.0', '<'));
  76. $previous = new Exception();
  77. $error = new Error('message', 100, $previous);
  78. $line = __LINE__ - 1;
  79. $exception = new PHP7ErrorException($error);
  80. $this->assertSame(100, $exception->getCode());
  81. $this->assertSame(__FILE__, $exception->getFile());
  82. $this->assertSame($line, $exception->getLine());
  83. $this->assertSame($previous, $exception->getPrevious());
  84. }
  85. /**
  86. * Tests PersistenceFailedException works.
  87. *
  88. * @return void
  89. */
  90. public function testPersistenceFailedException()
  91. {
  92. $previous = new Exception();
  93. $entity = new Entity();
  94. $exception = new PersistenceFailedException($entity, 'message', 100, $previous);
  95. $this->assertSame('message', $exception->getMessage());
  96. $this->assertSame(100, $exception->getCode());
  97. $this->assertSame($previous, $exception->getPrevious());
  98. $this->assertSame($entity, $exception->getEntity());
  99. $exception = new PersistenceFailedException(new Entity, 'message', null, $previous);
  100. $this->assertSame('message', $exception->getMessage());
  101. $this->assertSame(500, $exception->getCode());
  102. $this->assertSame($previous, $exception->getPrevious());
  103. }
  104. /**
  105. * Provides pairs of exception name and default code.
  106. *
  107. * @return array
  108. */
  109. public function exceptionProvider()
  110. {
  111. return [
  112. ['Cake\Console\Exception\ConsoleException', 500],
  113. ['Cake\Console\Exception\MissingHelperException', 500],
  114. ['Cake\Console\Exception\MissingShellException', 500],
  115. ['Cake\Console\Exception\MissingShellMethodException', 500],
  116. ['Cake\Console\Exception\MissingTaskException', 500],
  117. ['Cake\Console\Exception\StopException', 500],
  118. ['Cake\Controller\Exception\AuthSecurityException', 400],
  119. ['Cake\Controller\Exception\MissingActionException', 404],
  120. ['Cake\Controller\Exception\MissingComponentException', 500],
  121. ['Cake\Controller\Exception\SecurityException', 400],
  122. ['Cake\Core\Exception\Exception', 500],
  123. ['Cake\Core\Exception\MissingPluginException', 500],
  124. ['Cake\Database\Exception', 500],
  125. ['Cake\Database\Exception\MissingConnectionException', 500],
  126. ['Cake\Database\Exception\MissingDriverException', 500],
  127. ['Cake\Database\Exception\MissingExtensionException', 500],
  128. ['Cake\Database\Exception\NestedTransactionRollbackException', 500],
  129. ['Cake\Datasource\Exception\InvalidPrimaryKeyException', 404],
  130. ['Cake\Datasource\Exception\MissingDatasourceConfigException', 500],
  131. ['Cake\Datasource\Exception\MissingDatasourceException', 500],
  132. ['Cake\Datasource\Exception\MissingModelException', 500],
  133. ['Cake\Datasource\Exception\PageOutOfBoundsException', 404],
  134. ['Cake\Datasource\Exception\RecordNotFoundException', 404],
  135. ['Cake\Mailer\Exception\MissingActionException', 404],
  136. ['Cake\Mailer\Exception\MissingMailerException', 500],
  137. ['Cake\Network\Exception\BadRequestException', 400],
  138. ['Cake\Network\Exception\ConflictException', 409],
  139. ['Cake\Network\Exception\ForbiddenException', 403],
  140. ['Cake\Network\Exception\GoneException', 410],
  141. ['Cake\Network\Exception\HttpException', 500],
  142. ['Cake\Network\Exception\InternalErrorException', 500],
  143. ['Cake\Network\Exception\InvalidCsrfTokenException', 403],
  144. ['Cake\Network\Exception\MethodNotAllowedException', 405],
  145. ['Cake\Network\Exception\NotAcceptableException', 406],
  146. ['Cake\Network\Exception\NotFoundException', 404],
  147. ['Cake\Network\Exception\NotImplementedException', 501],
  148. ['Cake\Network\Exception\ServiceUnavailableException', 503],
  149. ['Cake\Network\Exception\SocketException', 0],
  150. ['Cake\Network\Exception\UnauthorizedException', 401],
  151. ['Cake\Network\Exception\UnavailableForLegalReasonsException', 451],
  152. ['Cake\ORM\Exception\MissingBehaviorException', 500],
  153. ['Cake\ORM\Exception\MissingEntityException', 500],
  154. ['Cake\ORM\Exception\MissingTableClassException', 500],
  155. ['Cake\ORM\Exception\RolledbackTransactionException', 500],
  156. ['Cake\Routing\Exception\DuplicateNamedRouteException', 500],
  157. ['Cake\Routing\Exception\MissingControllerException', 500],
  158. ['Cake\Routing\Exception\MissingDispatcherFilterException', 500],
  159. ['Cake\Routing\Exception\MissingRouteException', 500],
  160. ['Cake\Routing\Exception\RedirectException', 302],
  161. ['Cake\Utility\Exception\XmlException', 0],
  162. ['Cake\View\Exception\MissingCellException', 500],
  163. ['Cake\View\Exception\MissingCellViewException', 500],
  164. ['Cake\View\Exception\MissingElementException', 500],
  165. ['Cake\View\Exception\MissingHelperException', 500],
  166. ['Cake\View\Exception\MissingLayoutException', 500],
  167. ['Cake\View\Exception\MissingTemplateException', 500],
  168. ['Cake\View\Exception\MissingViewException', 500],
  169. ];
  170. }
  171. }