ExceptionsTest.php 8.4 KB

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