<commit_before><?php
namespace StoragelessSessionTest\Http;

use Dflydev\FigCookies\Cookie;
use Dflydev\FigCookies\FigRequestCookies;
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Token;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use StoragelessSession\Http\SessionMiddleware;
use StoragelessSession\Session\Data;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;

final class SessionMiddlewareTest extends PHPUnit_Framework_TestCase
{
    public function testInjectsSessionDataEvenWithNoNextMiddleware()
    {
        self::markTestIncomplete();
    }

    public function testRequiresValidToken()
    {
        self::markTestIncomplete();
    }

    public function testRequiresTokenSignatureValidation()
    {
        self::markTestIncomplete();
    }

    public function testRequiresTokenExpirationValidation()
    {
        self::markTestIncomplete();
    }

    public function testAllowsModifyingHeaderDetails()
    {
        self::markTestIncomplete();
    }

    public function testRemovesSessionCookieOnEmptySessionContainer()
    {
        self::markTestIncomplete();
    }

    public function testExtractsSessionContainerFromEmptyRequest()
    {
        $checkingMiddleware = $this->getMock(\stdClass::class, ['__invoke']);

        $checkingMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->with(self::callback(function (ServerRequestInterface $request) {
                self::assertInstanceOf(Data::class, $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE));

                return true;
            }))
            ->willReturn(new Response());

        self::assertInstanceOf(
            ResponseInterface::class,
            $this->defaultMiddleware()(new ServerRequest(), new Response(), $checkingMiddleware)
        );
    }

    public function testInjectsSessionInResponseCookies()
    {
        $checkingMiddleware = $this->getMock(\stdClass::class, ['__invoke']);

        $checkingMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->willReturnCallback(function (ServerRequestInterface $request) {
                /* @var $data Data */
                $data = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

                $data->set('foo', 'bar');

                return new Response();
            });

        $response = $this->defaultMiddleware()(new ServerRequest(), new Response(), $checkingMiddleware);

        self::assertEmpty(FigResponseCookies::get($response, 'non-existing')->getValue());
        self::assertInstanceOf(
            Token::class,
            (new Parser())->parse(FigResponseCookies::get($response, 'cookie-name')->getValue())
        );
    }

    public function testSessionContainerCanBeReusedOverMultipleRequests()
    {
        $containerValue                = uniqid('', true);
        $containerPopulationMiddleware = $this->getMock(\stdClass::class, ['__invoke']);
        $containerCheckingMiddleware   = $this->getMock(\stdClass::class, ['__invoke']);

        $containerPopulationMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->with(self::callback(function (ServerRequestInterface $request) use ($containerValue) {
                /* @var $data Data */
                $data = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

                $data->set('foo', $containerValue);

                return true;
            }))
            ->willReturn(new Response());

        $containerCheckingMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->with(self::callback(function (ServerRequestInterface $request) use ($containerValue) {
                /* @var $data Data */
                $data = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

                self::assertSame($containerValue, $data->get('foo'));

                return true;
            }))
            ->willReturn(new Response());

        $response = $this->defaultMiddleware()(new ServerRequest(), new Response(), $containerPopulationMiddleware);

        $this->defaultMiddleware()(
            FigRequestCookies::set(
                new ServerRequest(),
                Cookie::create('cookie-name', FigResponseCookies::get($response, 'cookie-name')->getValue())
            ),
            new Response(),
            $containerCheckingMiddleware
        );
    }

    /**
     * @return SessionMiddleware
     */<commit_msg>Testing all possible middleware instantiations<commit_after><?php
namespace StoragelessSessionTest\Http;

use Dflydev\FigCookies\Cookie;
use Dflydev\FigCookies\FigRequestCookies;
use Dflydev\FigCookies\FigResponseCookies;
use Dflydev\FigCookies\SetCookie;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Token;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use StoragelessSession\Http\SessionMiddleware;
use StoragelessSession\Session\Data;
use Zend\Diactoros\Request;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;

final class SessionMiddlewareTest extends PHPUnit_Framework_TestCase
{
    public function testInjectsSessionDataEvenWithNoNextMiddleware()
    {
        self::markTestIncomplete();
    }

    public function testRequiresValidToken()
    {
        self::markTestIncomplete();
    }

    public function testRequiresTokenSignatureValidation()
    {
        self::markTestIncomplete();
    }

    public function testRequiresTokenExpirationValidation()
    {
        self::markTestIncomplete();
    }

    public function testAllowsModifyingHeaderDetails()
    {
        self::markTestIncomplete();
    }

    public function testRemovesSessionCookieOnEmptySessionContainer()
    {
        self::markTestIncomplete();
    }

    /**
     * @dataProvider validMiddlewaresProvider
     */
    public function testExtractsSessionContainerFromEmptyRequest(SessionMiddleware $middleware)
    {
        $checkingMiddleware = $this->getMock(\stdClass::class, ['__invoke']);

        $checkingMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->with(self::callback(function (ServerRequestInterface $request) {
                self::assertInstanceOf(Data::class, $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE));

                return true;
            }))
            ->willReturn(new Response());

        self::assertInstanceOf(
            ResponseInterface::class,
            $middleware(new ServerRequest(), new Response(), $checkingMiddleware)
        );
    }

    public function testInjectsSessionInResponseCookies()
    {
        $checkingMiddleware = $this->getMock(\stdClass::class, ['__invoke']);

        $checkingMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->willReturnCallback(function (ServerRequestInterface $request) {
                /* @var $data Data */
                $data = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

                $data->set('foo', 'bar');

                return new Response();
            });

        $response = $this->defaultMiddleware()(new ServerRequest(), new Response(), $checkingMiddleware);

        self::assertEmpty(FigResponseCookies::get($response, 'non-existing')->getValue());
        self::assertInstanceOf(
            Token::class,
            (new Parser())->parse(FigResponseCookies::get($response, 'cookie-name')->getValue())
        );
    }

    public function testSessionContainerCanBeReusedOverMultipleRequests()
    {
        $containerValue                = uniqid('', true);
        $containerPopulationMiddleware = $this->getMock(\stdClass::class, ['__invoke']);
        $containerCheckingMiddleware   = $this->getMock(\stdClass::class, ['__invoke']);

        $containerPopulationMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->with(self::callback(function (ServerRequestInterface $request) use ($containerValue) {
                /* @var $data Data */
                $data = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

                $data->set('foo', $containerValue);

                return true;
            }))
            ->willReturn(new Response());

        $containerCheckingMiddleware
            ->expects(self::once())
            ->method('__invoke')
            ->with(self::callback(function (ServerRequestInterface $request) use ($containerValue) {
                /* @var $data Data */
                $data = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE);

                self::assertSame($containerValue, $data->get('foo'));

                return true;
            }))
            ->willReturn(new Response());

        $response = $this->defaultMiddleware()(new ServerRequest(), new Response(), $containerPopulationMiddleware);

        $this->defaultMiddleware()(
            FigRequestCookies::set(
                new ServerRequest(),
                Cookie::create('cookie-name', FigResponseCookies::get($response, 'cookie-name')->getValue())
            ),
            new Response(),
            $containerCheckingMiddleware
        );
    }

    /**
     * @return SessionMiddleware[][]
     */
    public function validMiddlewaresProvider()
    {
        return [
            [$this->defaultMiddleware()],
            [SessionMiddleware::fromSymmetricKeyDefaults('not relevant', 100)],
            [SessionMiddleware::fromAsymmetricKey(
                file_get_contents(__DIR__ . '/../../keys/private_key.pem'),
                file_get_contents(__DIR__ . '/../../keys/public_key.pem'),
                200
            )],
        ];
    }

    /**
     * @return SessionMiddleware
     */
{'fraction_non_alphanumeric': 0.12375745526838966, 'fraction_numerical': 0.0011928429423459245, 'mean_word_length': 2.6799561082662766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, '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': '6437604', 'n_tokens_mistral': 2654, 'n_tokens_neox': 2559, 'n_words': 403}