<commit_before><?php

declare(strict_types=1);

use Behat\Behat\Context\Context;
use Behat\Behat\Tester\Exception\PendingException;

class FeatureContext implements Context
{
    /**
     * @Given I am an administrator user
     */
    public function iAmAnAdministratorUser()
    {
        throw new PendingException();
    }

    /**
     * @When I try to access administration functionalities
     */
    public function iTryToAccessAdministrationFunctionalities()
    {
        throw new PendingException();
    }

    /**
     * @Then I am granted access
     */
    public function iAmGrantedAccess()
    {
        throw new PendingException();
    }

    /**
     * @Given I am a non administrator user
     */
    public function iAmANonAdministratorUser()
    {
        throw new PendingException();
    }

    /**
     * @Then I am denied access
     */
    public function iAmDeniedAccess()
    {
        throw new PendingException();
    }

    /**
     * @Given I am a unknown user
     */
    public function iAmAUnknownUser()
    {
        throw new PendingException();
    }

}<commit_msg>implementation of authorization middleware behat tests<commit_after><?php

declare(strict_types=1);

use App\Domain\Entity\User;
use App\Domain\Service\Exception\UserNotFoundException;
use App\Domain\Service\UsersServiceInterface;
use App\Middleware\Authorization;
use Behat\Behat\Context\Context;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Middleware\JwtAuthentication;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Stratigility\Delegate\CallableDelegateDecorator;

class FeatureContext implements Context
{
    /**
     * @var User
     */
    private $user;

    /**
     * @var UsersServiceInterface
     */
    private $usersService;

    /**
     * @var ResponseInterface
     */
    private $response;

    /**
     * @var \Exception|null
     */
    private $exception = null;

    /** @BeforeScenario */
    public function beforeScenario()
    {
        $this->usersService = \Mockery::mock(UsersServiceInterface::class);
    }

    /**
     * @Given I am an administrator user
     */
    public function iAmAnAdministratorUser()
    {
        $this->user = User::newAdministrator('gigi', 'zucon');

        $this->usersService->shouldReceive('getByUsername')->with('gigi')->andReturn($this->user);
    }

    /**
     * @When I try to access administration functionalities
     */
    public function iTryToAccessAdministrationFunctionalities()
    {
        $authorizationMiddleware = new Authorization($this->usersService);

        $request = (new ServerRequest())->withAttribute(
            JwtAuthentication::class,
            json_decode(json_encode([
                'data' => [
                    'username' => $this->user->username()
                ]
            ]))
        );

        try {
            $this->response = $authorizationMiddleware->process(
                $request,
                new CallableDelegateDecorator(
                    function (ServerRequestInterface $request, ResponseInterface $response) {
                        return $response;
                    },
                    new Response()
                )
            );
        } catch (\Exception $e) {
            $this->exception = $e;
        }
    }

    /**
     * @Then I am granted access
     */
    public function iAmGrantedAccess()
    {
        Assert::assertSame(200, $this->response->getStatusCode());
    }

    /**
     * @Given I am a non administrator user
     */
    public function iAmANonAdministratorUser()
    {
        $this->user = User::new('gigi', 'zucon');

        $this->usersService->shouldReceive('getByUsername')->with('gigi')->andReturn($this->user);
    }

    /**
     * @Then I am denied access
     */
    public function iAmDeniedAccess()
    {
        Assert::assertTrue(
            $this->exception instanceof UserNotFoundException ||
            403 === $this->response->getStatusCode()
        );
    }

    /**
     * @Given I am a unknown user
     */
    public function iAmAUnknownUser()
    {
        $this->user = User::new('gigi', 'zucon');

        $this->usersService
            ->shouldReceive('getByUsername')
            ->with('gigi')
            ->andThrow(UserNotFoundException::fromUsername('gigi'));
    }

    /** @AfterScenario */
    public function afterScenario()
    {
        Mockery::close();
    }
}
{'fraction_non_alphanumeric': 0.11852346008450078, 'fraction_numerical': 0.00177896375361352, 'mean_word_length': 2.181046676096181, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '914721', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1215, 'n_words': 253}