<commit_before><?php

namespace Tests\App\Http\Controllers;

use App\Http\Controllers\PostController;
use App\Http\JsonApi;
use App\Post;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Mockery;
use Mockery\Mock;
use Psr\Log\LoggerInterface;
use Tests\TestCase;

class PostControllerTest extends TestCase
{
    /**
     * @var PostController
     */
    private $postController;
    /**
     * @var Gate | Mock;
     */
    private $gateMock;
    /**
     * @var Response | Mock
     */
    private $responseMock;
    /**
     * @var LoggerInterface | Mock
     */
    private $loggerMock;
    /**
     * @var Post | Mock
     */
    private $postMock;
    /**
     * @var JsonApi | Mock;
     */
    protected $jsonApiMock;

    public function setUp()
    {
        parent::setUp();
        $this->jsonApiMock = $this->jsonApiMock = Mockery::mock(JsonApi::class);
        $this->gateMock = Mockery::mock(Gate::class);
        $this->loggerMock = Mockery::mock(LoggerInterface::class);
        $this->responseMock = Mockery::mock(Response::class);
        $this->postMock = Mockery::mock(Post::class);
        $this->postController = new PostController($this->jsonApiMock, $this->gateMock, $this->loggerMock);
    }

    public function tearDown()
    {
        parent::tearDown();
    }

    public function test_index_responds_with_resources()
    {
        $paginationMock = Mockery::mock(LengthAwarePaginator::class);

        $request = Request::create('v1/posts', 'GET', ['size' => 10, 'page' => 2]);
        $this->postMock
            ->shouldReceive('where')
            ->once()
            ->withArgs(['status', 'draft'])
            ->andReturn($this->postMock);

        $this->postMock
            ->shouldReceive('orderBy')
            ->once()
            ->withArgs(['created_at', 'desc'])
            ->andReturn($this->postMock);

        $this->postMock
            ->shouldReceive('paginate')
            ->once()
            ->withArgs([10, null, 'page', 2])
            ->andReturn($paginationMock);

        $this->jsonApiMock
            ->shouldReceive('respondResourcesFound')
            ->once()
            ->withArgs([$this->responseMock, $paginationMock])
            ->andReturn($this->responseMock);

        $actualResult = $this->postController->index($request, $this->responseMock, $this->postMock);
        $expectedResult = $this->responseMock;

        $this->assertThat($actualResult, $this->equalTo($expectedResult));
    }

    public function test_store_authorization()
    {
        $this->gateMock
            ->shouldReceive('denies')
            ->once()
            ->withArgs(['store', $this->postMock])
            ->andReturn(true);

        $this->jsonApiMock
            ->shouldReceive('respondUnauthorized')
            ->once()
            ->andReturn($this->responseMock);

        $request = Request::create('v1/posts', 'POST');<commit_msg>Added tests for PostController show method<commit_after><?php

namespace Tests\App\Http\Controllers;

use App\Http\Controllers\PostController;
use App\Http\JsonApi;
use App\Post;
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Mockery;
use Mockery\Mock;
use Psr\Log\LoggerInterface;
use Tests\TestCase;

class PostControllerTest extends TestCase
{
    /**
     * @var PostController
     */
    private $postController;
    /**
     * @var Gate | Mock;
     */
    private $gateMock;
    /**
     * @var Response | Mock
     */
    private $responseMock;
    /**
     * @var LoggerInterface | Mock
     */
    private $loggerMock;
    /**
     * @var Post | Mock
     */
    private $postMock;
    /**
     * @var JsonApi | Mock;
     */
    protected $jsonApiMock;

    public function setUp()
    {
        parent::setUp();
        $this->jsonApiMock = $this->jsonApiMock = Mockery::mock(JsonApi::class);
        $this->gateMock = Mockery::mock(Gate::class);
        $this->loggerMock = Mockery::mock(LoggerInterface::class);
        $this->responseMock = Mockery::mock(Response::class);
        $this->postMock = Mockery::mock(Post::class);
        $this->postController = new PostController($this->jsonApiMock, $this->gateMock, $this->loggerMock);
    }

    public function tearDown()
    {
        parent::tearDown();
    }

    public function test_index_responds_with_resources()
    {
        $paginationMock = Mockery::mock(LengthAwarePaginator::class);

        $request = Request::create('v1/posts', 'GET', ['size' => 10, 'page' => 2]);
        $this->postMock
            ->shouldReceive('where')
            ->once()
            ->withArgs(['status', 'draft'])
            ->andReturn($this->postMock);

        $this->postMock
            ->shouldReceive('orderBy')
            ->once()
            ->withArgs(['created_at', 'desc'])
            ->andReturn($this->postMock);

        $this->postMock
            ->shouldReceive('paginate')
            ->once()
            ->withArgs([10, null, 'page', 2])
            ->andReturn($paginationMock);

        $this->jsonApiMock
            ->shouldReceive('respondResourcesFound')
            ->once()
            ->withArgs([$this->responseMock, $paginationMock])
            ->andReturn($this->responseMock);

        $actualResult = $this->postController->index($request, $this->responseMock, $this->postMock);
        $expectedResult = $this->responseMock;

        $this->assertThat($actualResult, $this->equalTo($expectedResult));
    }

    public function test_show_responds_with_a_resource_when_one_exists()
    {
        $this->postMock
            ->shouldReceive('find')
            ->once()
            ->withArgs(['a-slug'])
            ->andReturn($this->postMock);

        $this->jsonApiMock
            ->shouldReceive('respondResourceFound')
            ->once()
            ->withArgs([$this->responseMock, $this->postMock])
            ->andReturn($this->responseMock);

        $actualResult = $this->postController->show($this->responseMock, $this->postMock, 'a-slug');
        $expectedResult = $this->responseMock;

        $this->assertThat($actualResult, $this->equalTo($expectedResult));
    }

    public function test_show_responds_not_found_when_no_resource_exists()
    {
        $this->postMock
            ->shouldReceive('find')
            ->once()
            ->withArgs(['a-slug'])
            ->andReturn(null);

        $this->jsonApiMock
            ->shouldReceive('respondResourceNotFound')
            ->once()
            ->withArgs([$this->responseMock])
            ->andReturn($this->responseMock);

        $actualResult = $this->postController->show($this->responseMock, $this->postMock, 'a-slug');
        $expectedResult = $this->responseMock;

        $this->assertThat($actualResult, $this->equalTo($expectedResult));
    }

    public function test_store_authorization()
    {
        $this->gateMock
            ->shouldReceive('denies')
            ->once()
            ->withArgs(['store', $this->postMock])
            ->andReturn(true);

        $this->jsonApiMock
            ->shouldReceive('respondUnauthorized')
            ->once()
            ->andReturn($this->responseMock);

        $request = Request::create('v1/posts', 'POST');
{'fraction_non_alphanumeric': 0.1550838290968091, 'fraction_numerical': 0.002163331530557058, 'mean_word_length': 2.413474850023073, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 151, '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': '6195964', 'n_tokens_mistral': 2126, 'n_tokens_neox': 2099, 'n_words': 335}