ExceptionsTest.php 8.4 KB

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