Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/app/Controller/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Spiral\Testing\Tests\App\Controller;

use Spiral\Auth\AuthContextInterface;
use Spiral\Router\Annotation\Route;

class AuthController
{
public function __construct(
protected AuthContextInterface $auth,
) {}

#[Route(route: '/some-method', methods: 'GET')]
public function someMethod(): array
{
$actor = $this->auth->getActor();

return $actor === null ? [] : (array) $actor;
}
}
28 changes: 24 additions & 4 deletions tests/src/Http/FakeHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,26 +20,45 @@ 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);
}

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);
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading