Skip to content

4.x - Exceptions not testable using disableErrorHandlerMiddleware() #14237

@Schlaefer

Description

@Schlaefer

This is a (multiple allowed):

  • bug

  • enhancement

  • feature-discussion (RFC)

  • CakePHP Version: 4.0.3

  • Platform and Target: PHP 7.2, apache 2.4, MySQL 5.7,

What you did

  1. Create new project with composer create-project --prefer-dist cakephp/app
  2. Create a DB with table "mytable"
  3. Bake controller bin/cake bake controller Mytable
  4. Throw an exception in controller
class MytableController extends AppController
{
     public function index()
    {
        throw new \Exception();
  1. Check for the exception in the test created by bake
class MytableControllerTest extends TestCase
{
     public function testIndex(): void
    {
        $this->disableErrorHandlerMiddleware();
        $this->expectException(\Exception::class);
        $this->get('/mytable/index');
  1. Run test
vendor/bin/phpunit tests/TestCase/Controller/MytableControllerTest.php --filter testIndex

What happened

There was 1 failure:

1) App\Test\TestCase\Controller\MytableControllerTest::testIndex
Failed asserting that exception of type "Exception" is thrown.

What you expected to happen

The test should not fail but detect the thrown exception.

Using disableErrorHandlerMiddleware() a exception was testable in Cake 3, but this isn't the case in CakePHP 4 (at the moment)?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions