<commit_before><?php
namespace Ents24\Tests\Api;

use Ents24\Api\Client;
use Ents24\Api\Session;
use Guzzle\Common\Event;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Mockery as m;
use PHPUnit_Framework_TestCase;

class SessionTest extends PHPUnit_Framework_TestCase
{
    private $client;
    private $session;
    private $event;
    private $request;
    private $response;

    public function setUp()
    {
        parent::setUp();
        $this->client = m::mock(
            'Ents24\Api\Client',
            [
                'getId' => 'id1',
                'getSecret' => 'secret1',
            ]
        );
        $this->event = new Event;
        $this->session = new Session($this->client);
        $this->event['request'] = new Request(null, null, []);
        $this->event['response'] = new Response(null);
    }

    /**
     * @test
     */
    public function addsAuthorizationHeaderToRequest()
    {
        $this->session->setAccessToken('qwertyuiop');
        $this->session->onRequestBeforeSend($this->event);
        $this->assertEquals(
            'qwertyuiop',
            $this->event['request']->getHeader('Authorization')
        );
    }

    /**
     * @test
     */
    public function fetchesAccessTokenIfAbsent()
    {
        $tokenRequest = m::mock('Guzzle\Service\Command\OperationCommand');

        $this->client
            ->shouldReceive('getCommand')
            ->with(
                'RequestAccessToken',
                [
                    'client_id'     => 'id1',
                    'client_secret' => 'secret1',
                ]
            )
            ->andReturn($tokenRequest);

        $tokenRequest
            ->shouldReceive('execute')
            ->andReturn(['access_token' => '<KEY>']);

        $this->session->onRequestBeforeSend($this->event);
        $this->assertEquals(
            'asdfghjkl',
            $this->event['request']->getHeader('Authorization')
        );
    }
}<commit_msg>Add test for auth/token request infinite recursion bug<commit_after><?php
namespace Ents24\Tests\Api;

use Ents24\Api\Client;
use Ents24\Api\Session;
use Guzzle\Common\Event;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Message\Response;
use Mockery as m;
use PHPUnit_Framework_TestCase;

class SessionTest extends PHPUnit_Framework_TestCase
{
    private $client;
    private $session;
    private $event;
    private $request;
    private $response;

    public function setUp()
    {
        parent::setUp();
        $this->client = m::mock(
            'Ents24\Api\Client',
            [
                'getId' => 'id1',
                'getSecret' => 'secret1',
            ]
        );
        $this->event = new Event;
        $this->session = new Session($this->client);
        $this->event['request'] = new Request(null, null, []);
        $this->event['response'] = new Response(null);
    }

    /**
     * @test
     */
    public function addsAuthorizationHeaderToRequest()
    {
        $this->session->setAccessToken('qwertyuiop');
        $this->session->onRequestBeforeSend($this->event);
        $this->assertEquals(
            'qwertyuiop',
            $this->event['request']->getHeader('Authorization')
        );
    }

    /**
     * @test
     */
    public function fetchesAccessTokenIfAbsent()
    {
        $tokenRequest = m::mock('Guzzle\Service\Command\OperationCommand');

        $this->client
            ->shouldReceive('getCommand')
            ->with(
                'RequestAccessToken',
                [
                    'client_id'     => 'id1',
                    'client_secret' => 'secret1',
                ]
            )
            ->andReturn($tokenRequest);

        $tokenRequest
            ->shouldReceive('execute')
            ->andReturn(['access_token' => '<KEY>']);

        $this->session->onRequestBeforeSend($this->event);
        $this->assertEquals(
            'asdfghjkl',
            $this->event['request']->getHeader('Authorization')
        );
    }

    /**
     * @test
     */
    public function doesntFetchAccessTokenForAccessTokenRequest()
    {
        $this->event['request']->setPath('/auth/token');

        $this->client
            ->shouldReceive('getCommand')
            ->with('RequestAccessToken', m::any())
            ->times(0);

        $this->session->onRequestBeforeSend($this->event);
    }
}
{'fraction_non_alphanumeric': 0.14644160583941607, 'fraction_numerical': 0.005702554744525547, 'mean_word_length': 1.9528619528619529, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 76, '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': '731765', 'n_tokens_mistral': 1268, 'n_tokens_neox': 1224, 'n_words': 196}