ExceptionsTest.php 9.4 KB

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