assertSame('message', $exception->getMessage()); $this->assertSame(100, $exception->getCode()); $this->assertSame($previous, $exception->getPrevious()); $exception = new $class('message', null, $previous); $this->assertSame('message', $exception->getMessage()); $this->assertSame($defaultCode, $exception->getCode()); $this->assertSame($previous, $exception->getPrevious()); } /** * Tests FatalErrorException works. */ public function testFatalErrorException(): void { $previous = new Exception(); $exception = new FatalErrorException('message', 100, __FILE__, 1, $previous); $this->assertSame('message', $exception->getMessage()); $this->assertSame(100, $exception->getCode()); $this->assertSame(__FILE__, $exception->getFile()); $this->assertSame(1, $exception->getLine()); $this->assertSame($previous, $exception->getPrevious()); } /** * Tests PersistenceFailedException works. */ public function testPersistenceFailedException(): void { $previous = new Exception(); $entity = new Entity(); $exception = new PersistenceFailedException($entity, 'message', 100, $previous); $this->assertSame('message', $exception->getMessage()); $this->assertSame(100, $exception->getCode()); $this->assertSame($previous, $exception->getPrevious()); $this->assertSame($entity, $exception->getEntity()); } /** * Test the template exceptions */ public function testMissingTemplateExceptions(): void { $previous = new Exception(); $error = new MissingTemplateException('view.ctp', ['path/a/', 'path/b/'], 100, $previous); $this->assertStringContainsString('Template file `view.ctp` could not be found', $error->getMessage()); $this->assertStringContainsString('- `path/a/view.ctp`', $error->getMessage()); $this->assertSame($previous, $error->getPrevious()); $this->assertSame(100, $error->getCode()); $attributes = $error->getAttributes(); $this->assertArrayHasKey('file', $attributes); $this->assertArrayHasKey('paths', $attributes); $error = new MissingLayoutException('default.ctp', ['path/a/', 'path/b/'], 100, $previous); $this->assertStringContainsString('Layout file `default.ctp` could not be found', $error->getMessage()); $this->assertStringContainsString('- `path/a/default.ctp`', $error->getMessage()); $this->assertSame($previous, $error->getPrevious()); $this->assertSame(100, $error->getCode()); $error = new MissingElementException('view.ctp', ['path/a/', 'path/b/'], 100, $previous); $this->assertStringContainsString('Element file `view.ctp` could not be found', $error->getMessage()); $this->assertStringContainsString('- `path/a/view.ctp`', $error->getMessage()); $this->assertSame($previous, $error->getPrevious()); $this->assertSame(100, $error->getCode()); $error = new MissingCellTemplateException('Articles', 'view.ctp', ['path/a/', 'path/b/'], 100, $previous); $this->assertStringContainsString('Cell template file `view.ctp` could not be found', $error->getMessage()); $this->assertStringContainsString('- `path/a/view.ctp`', $error->getMessage()); $this->assertSame($previous, $error->getPrevious()); $this->assertSame(100, $error->getCode()); $attributes = $error->getAttributes(); $this->assertArrayHasKey('name', $attributes); $this->assertArrayHasKey('file', $attributes); $this->assertArrayHasKey('paths', $attributes); } /** * Provides pairs of exception name and default code. * * @return array */ public static function exceptionProvider(): array { return [ [ConsoleException::class, 1], [MissingHelperException::class, 1], [StopException::class, 1], [AuthSecurityException::class, 400], [MissingActionException::class, 0], [MissingComponentException::class, 0], [SecurityException::class, 400], [CakeException::class, 0], [MissingPluginException::class, 0], [DatabaseException::class, 0], [MissingConnectionException::class, 0], [MissingDriverException::class, 0], [MissingExtensionException::class, 0], [NestedTransactionRollbackException::class, 0], [InvalidPrimaryKeyException::class, 0], [MissingDatasourceConfigException::class, 0], [MissingDatasourceException::class, 0], [MissingModelException::class, 0], [RecordNotFoundException::class, 0], [PageOutOfBoundsException::class, 0], [MailerMissingActionException::class, 0], [MissingMailerException::class, 0], [BadRequestException::class, 400], [ConflictException::class, 409], [ForbiddenException::class, 403], [GoneException::class, 410], [HttpException::class, 500], [InternalErrorException::class, 500], [InvalidCsrfTokenException::class, 403], [MethodNotAllowedException::class, 405], [MissingControllerException::class, 404], [NotAcceptableException::class, 406], [NotFoundException::class, 404], [NotImplementedException::class, 501], [ServiceUnavailableException::class, 503], [UnauthorizedException::class, 401], [UnavailableForLegalReasonsException::class, 451], [SocketException::class, 0], [MissingBehaviorException::class, 0], [MissingEntityException::class, 0], [MissingTableClassException::class, 0], [RolledbackTransactionException::class, 0], [DuplicateNamedRouteException::class, 0], [MissingRouteException::class, 0], [XmlException::class, 0], [MissingCellException::class, 0], [ViewMissingHelperException::class, 0], [MissingViewException::class, 0], ]; } }