ExceptionsTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. }
  66. /**
  67. * Tests PersistenceFailedException works.
  68. *
  69. * @return void
  70. */
  71. public function testPersistenceFailedException()
  72. {
  73. $previous = new Exception();
  74. $entity = new Entity();
  75. $exception = new PersistenceFailedException($entity, 'message', 100, $previous);
  76. $this->assertSame('message', $exception->getMessage());
  77. $this->assertSame(100, $exception->getCode());
  78. $this->assertSame($previous, $exception->getPrevious());
  79. $this->assertSame($entity, $exception->getEntity());
  80. }
  81. /**
  82. * Test the template exceptions
  83. *
  84. * @return void
  85. */
  86. public function testMissingTemplateExceptions()
  87. {
  88. $previous = new Exception();
  89. $error = new MissingTemplateException('view.ctp', ['path/a/', 'path/b/'], 100, $previous);
  90. $this->assertStringContainsString('Template file `view.ctp` could not be found', $error->getMessage());
  91. $this->assertStringContainsString('- `path/a/view.ctp`', $error->getMessage());
  92. $this->assertSame($previous, $error->getPrevious());
  93. $this->assertSame(100, $error->getCode());
  94. $attributes = $error->getAttributes();
  95. $this->assertArrayHasKey('file', $attributes);
  96. $this->assertArrayHasKey('paths', $attributes);
  97. $error = new MissingLayoutException('default.ctp', ['path/a/', 'path/b/'], 100, $previous);
  98. $this->assertStringContainsString('Layout file `default.ctp` could not be found', $error->getMessage());
  99. $this->assertStringContainsString('- `path/a/default.ctp`', $error->getMessage());
  100. $this->assertSame($previous, $error->getPrevious());
  101. $this->assertSame(100, $error->getCode());
  102. $error = new MissingElementException('view.ctp', ['path/a/', 'path/b/'], 100, $previous);
  103. $this->assertStringContainsString('Element file `view.ctp` could not be found', $error->getMessage());
  104. $this->assertStringContainsString('- `path/a/view.ctp`', $error->getMessage());
  105. $this->assertSame($previous, $error->getPrevious());
  106. $this->assertSame(100, $error->getCode());
  107. $error = new MissingCellTemplateException('Articles', 'view.ctp', ['path/a/', 'path/b/'], 100, $previous);
  108. $this->assertStringContainsString('Cell template file `view.ctp` could not be found', $error->getMessage());
  109. $this->assertStringContainsString('- `path/a/view.ctp`', $error->getMessage());
  110. $this->assertSame($previous, $error->getPrevious());
  111. $this->assertSame(100, $error->getCode());
  112. $attributes = $error->getAttributes();
  113. $this->assertArrayHasKey('name', $attributes);
  114. $this->assertArrayHasKey('file', $attributes);
  115. $this->assertArrayHasKey('paths', $attributes);
  116. }
  117. /**
  118. * Provides pairs of exception name and default code.
  119. *
  120. * @return array
  121. */
  122. public function exceptionProvider()
  123. {
  124. return [
  125. ['Cake\Console\Exception\ConsoleException', 1],
  126. ['Cake\Console\Exception\MissingHelperException', 1],
  127. ['Cake\Console\Exception\MissingShellException', 1],
  128. ['Cake\Console\Exception\MissingShellMethodException', 1],
  129. ['Cake\Console\Exception\MissingTaskException', 1],
  130. ['Cake\Console\Exception\StopException', 1],
  131. ['Cake\Controller\Exception\AuthSecurityException', 400],
  132. ['Cake\Controller\Exception\MissingActionException', 0],
  133. ['Cake\Controller\Exception\MissingComponentException', 0],
  134. ['Cake\Controller\Exception\SecurityException', 400],
  135. ['Cake\Core\Exception\Exception', 0],
  136. ['Cake\Core\Exception\MissingPluginException', 0],
  137. ['Cake\Database\Exception', 0],
  138. ['Cake\Database\Exception\MissingConnectionException', 0],
  139. ['Cake\Database\Exception\MissingDriverException', 0],
  140. ['Cake\Database\Exception\MissingExtensionException', 0],
  141. ['Cake\Database\Exception\NestedTransactionRollbackException', 0],
  142. ['Cake\Datasource\Exception\InvalidPrimaryKeyException', 0],
  143. ['Cake\Datasource\Exception\MissingDatasourceConfigException', 0],
  144. ['Cake\Datasource\Exception\MissingDatasourceException', 0],
  145. ['Cake\Datasource\Exception\MissingModelException', 0],
  146. ['Cake\Datasource\Exception\PageOutOfBoundsException', 0],
  147. ['Cake\Datasource\Exception\RecordNotFoundException', 0],
  148. ['Cake\Mailer\Exception\MissingActionException', 0],
  149. ['Cake\Mailer\Exception\MissingMailerException', 0],
  150. ['Cake\Http\Exception\BadRequestException', 400],
  151. ['Cake\Http\Exception\ConflictException', 409],
  152. ['Cake\Http\Exception\ForbiddenException', 403],
  153. ['Cake\Http\Exception\GoneException', 410],
  154. ['Cake\Http\Exception\HttpException', 500],
  155. ['Cake\Http\Exception\InternalErrorException', 500],
  156. ['Cake\Http\Exception\InvalidCsrfTokenException', 403],
  157. ['Cake\Http\Exception\MethodNotAllowedException', 405],
  158. ['Cake\Http\Exception\MissingControllerException', 404],
  159. ['Cake\Http\Exception\NotAcceptableException', 406],
  160. ['Cake\Http\Exception\NotFoundException', 404],
  161. ['Cake\Http\Exception\NotImplementedException', 501],
  162. ['Cake\Http\Exception\ServiceUnavailableException', 503],
  163. ['Cake\Http\Exception\UnauthorizedException', 401],
  164. ['Cake\Http\Exception\UnavailableForLegalReasonsException', 451],
  165. ['Cake\Network\Exception\SocketException', 0],
  166. ['Cake\ORM\Exception\MissingBehaviorException', 0],
  167. ['Cake\ORM\Exception\MissingEntityException', 0],
  168. ['Cake\ORM\Exception\MissingTableClassException', 0],
  169. ['Cake\ORM\Exception\RolledbackTransactionException', 0],
  170. ['Cake\Routing\Exception\DuplicateNamedRouteException', 0],
  171. ['Cake\Routing\Exception\MissingDispatcherFilterException', 0],
  172. ['Cake\Routing\Exception\MissingRouteException', 0],
  173. ['Cake\Routing\Exception\RedirectException', 302],
  174. ['Cake\Utility\Exception\XmlException', 0],
  175. ['Cake\View\Exception\MissingCellException', 0],
  176. ['Cake\View\Exception\MissingHelperException', 0],
  177. ['Cake\View\Exception\MissingViewException', 0],
  178. ];
  179. }
  180. }