From 13a9d7ad9c01f4030986adbf86e1a0230e44dfaf Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 4 Feb 2025 01:10:19 +0400 Subject: [PATCH] Add test FakeHttpTest::testWithActor --- tests/app/Controller/AuthController.php | 23 ++++++++++++++++++++ tests/src/Http/FakeHttpTest.php | 28 +++++++++++++++++++++---- tests/src/TestCase.php | 1 + 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/app/Controller/AuthController.php diff --git a/tests/app/Controller/AuthController.php b/tests/app/Controller/AuthController.php new file mode 100644 index 0000000..ec1394b --- /dev/null +++ b/tests/app/Controller/AuthController.php @@ -0,0 +1,23 @@ +auth->getActor(); + + return $actor === null ? [] : (array) $actor; + } +} diff --git a/tests/src/Http/FakeHttpTest.php b/tests/src/Http/FakeHttpTest.php index c6f32d0..014e040 100644 --- a/tests/src/Http/FakeHttpTest.php +++ b/tests/src/Http/FakeHttpTest.php @@ -5,6 +5,7 @@ namespace Spiral\Testing\Tests\Http; use PHPUnit\Framework\ExpectationFailedException; +use Spiral\Auth\Middleware\AuthMiddleware; use Spiral\Core\Internal\Introspector; use Spiral\Testing\Attribute\TestScope; use Spiral\Testing\Tests\App\Middleware\FailMiddleware; @@ -19,9 +20,25 @@ public function testGetBodySame(): void $response->assertBodySame('[]'); } + public function testWithActor(): void + { + $http = $this->fakeHttp(); + + $user = (object) ['id' => 42, 'name' => 'John Doe']; + + $response = $http + ->withMiddleware(AuthMiddleware::class) + ->withActor($user) + ->get('/some-method'); + + $response->assertOk(); + $response->assertBodySame('{"id":42,"name":"John Doe"}'); + } + public function testWithMiddleware(): void { - $response = $this->fakeHttp() + $response = $this + ->fakeHttp() ->withMiddleware(StaticResultMiddleware::class) ->get('/get/query-params'); $response->assertBodySame(StaticResultMiddleware::STATIC_RESULT); @@ -29,16 +46,19 @@ public function testWithMiddleware(): void public function testWithoutMiddleware(): void { - $this->fakeHttp() + $this + ->fakeHttp() ->get(FailMiddleware::ROUTE) ->assertBodySame(FailMiddleware::STATIC_RESULT); - $this->fakeHttp() + $this + ->fakeHttp() ->withoutMiddleware(FailMiddleware::class) ->get(FailMiddleware::ROUTE) ->assertNotFound(); - $this->fakeHttp() + $this + ->fakeHttp() ->get(FailMiddleware::ROUTE) ->assertBodySame(FailMiddleware::STATIC_RESULT); } diff --git a/tests/src/TestCase.php b/tests/src/TestCase.php index 100ef6e..3fdbe20 100644 --- a/tests/src/TestCase.php +++ b/tests/src/TestCase.php @@ -21,6 +21,7 @@ public function defineBootloaders(): array \Spiral\Tokenizer\Bootloader\TokenizerBootloader::class, \Spiral\SendIt\Bootloader\MailerBootloader::class, \Spiral\Bootloader\Http\HttpBootloader::class, + \Spiral\Bootloader\Auth\HttpAuthBootloader::class, \Spiral\Nyholm\Bootloader\NyholmBootloader::class, \Spiral\Bootloader\Security\EncrypterBootloader::class, \Spiral\Bootloader\Http\RouterBootloader::class,