<commit_before><?php
namespace JsonApi\Test\TestCase\View;

use Cake\Controller\Controller;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Neomerx\JsonApi\Schema\Link;

class JsonApiViewTest extends TestCase
{
    public $fixtures = [
        'core.articles',
        'core.authors'
    ];

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

    protected function _getView($viewVars = [])
    {
        $Request = new Request();
        $Response = new Response();
        $Controller = new Controller($Request, $Response);

        $builder = $Controller->viewBuilder();
        $builder->className('JsonApi\View\JsonApiView');

        if ($viewVars) {
            $Controller->set($viewVars);
        }

        return $Controller->createView();
    }

    /**
     * Test Render
     * @return [type] [description]
     */
    public function testRenderUsingBaseSchema()
    {
        $records = TableRegistry::get('Articles')->find()->all();

        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Article'
            ],
            '_serialize' => $records
        ]);

        $this->assertJsonStringEqualsJsonFile(
            ROOT . DS . 'tests' . DS . 'Fixture' . DS . 'articles.json',
            $view->render()
        );
    }

    /**
     * Test Render
     * @return [type] [description]
     */
    public function testRenderUsingCustomSchema()
    {
        $records = TableRegistry::get('Authors')->find()
            ->contain(['Articles'])
            ->all();


        $view = $this->_getView([
            '_serialize' => $records,
            '_url' => 'http://localhost',
            '_entities' => [
                'Author',
                'Article'
            ]
        ]);

        $this->assertJsonStringEqualsJsonFile(
            ROOT . DS . 'tests' . DS . 'Fixture' . DS . 'authors.json',
            $view->render()
        );
    }

    public function testViewResponse()
    {
        $records = TableRegistry::get('Articles')->find()->all();

        $view = $this->_getView([
            '_entities' => [ 'Article' ],
            '_serialize' => $records
        ]);

        $output = $view->render();

        $this->assertSame('application/vnd.api+json', $view->response->type());
    }

    /**
     * Test Render
     * @return [type] [description]
     */
    public function testEncodeWithIncludeAndFieldSet()
    {
        $records = TableRegistry::get('Authors')->find()
            ->contain(['Articles'])
            ->all();

        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Author',
                'Article'
            ],
            '_serialize' => $records,
            '_include' => [ 'articles' ],
            '_fieldsets' => [ 'articles' => [ 'title' ] ]
        ]);

        $output = $view->render();
        $output = json_decode($output, true);

        $expectedSubset = [
            'included' => [
                [
                    'type' => 'articles',
                    'id' => '1',
                    'attributes' => [
                        'title' => 'First Article'
                    ]
                ]
            ]
        ];

        $this->assertArraySubset($expectedSubset, $output);
    }

    public function testOnlyMetaData()
    {
        $meta = [ 'meta' => 'data' ];
        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Article'
            ],
            '_meta' => $meta
        ]);

        $output = $view->render();
        $this->assertEquals(['meta' => $meta ], json_decode($output, true));
    }


    public function testResponseWithLinks()
    {
        $records = TableRegistry::get('Articles')->find()->all();

        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Article'
            ],
            '_serialize' => $records,
            '_links' => [
                Link::FIRST => new Link('/authors?page=1'),
                Link::LAST => new Link('/authors?page=4'),
                Link::NEXT => new Link('/authors?page=6'),
                Link::LAST => new Link('/authors?page=9', [
                    'meta' => 'data'
                ])
            ]
        ]);

        $output = $view->render();
        $output = json_decode($output, true);

        $expected = [
            'first' => 'http://localhost/authors?page=1',
            'last' => [
                'href' => 'http://localhost/authors?page=9',
                'meta' => [
                    'meta' => 'data'
                ]
            ],
            'next' => 'http://localhost/authors?page=6'
        ];

        $this->assertArraySubset(['links' => $expected], $output);
    }

    public function testJsonOptions()
    {
        $view = $this->_getView([
            '_jsonOptions' => JSON_HEX_QUOT
        ]);

        $view->render();
        $this->assertEquals(8, $view->viewVars['_jsonOptions']);

        $view = $this->_getView([
            '_jsonOptions' => false
        ]);

        $view->render();
        $this->assertEquals(0, $view->viewVars['_jsonOptions']);
    }

    public function testEmptyView()
    {
        $view = $this->_getView();
        $output = $view->render();

        $this->assertEquals(['data' => null], json_decode($output, true));
    }<commit_msg>Improve testcase for metadata<commit_after><?php
namespace JsonApi\Test\TestCase\View;

use Cake\Controller\Controller;
use Cake\Network\Request;
use Cake\Network\Response;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use Neomerx\JsonApi\Schema\Link;

class JsonApiViewTest extends TestCase
{
    public $fixtures = [
        'core.articles',
        'core.authors'
    ];

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

    protected function _getView($viewVars = [])
    {
        $Request = new Request();
        $Response = new Response();
        $Controller = new Controller($Request, $Response);

        $builder = $Controller->viewBuilder();
        $builder->className('JsonApi\View\JsonApiView');

        if ($viewVars) {
            $Controller->set($viewVars);
        }

        return $Controller->createView();
    }

    /**
     * Test Render
     * @return [type] [description]
     */
    public function testRenderUsingBaseSchema()
    {
        $records = TableRegistry::get('Articles')->find()->all();

        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Article'
            ],
            '_serialize' => $records
        ]);

        $this->assertJsonStringEqualsJsonFile(
            ROOT . DS . 'tests' . DS . 'Fixture' . DS . 'articles.json',
            $view->render()
        );
    }

    /**
     * Test Render
     * @return [type] [description]
     */
    public function testRenderUsingCustomSchema()
    {
        $records = TableRegistry::get('Authors')->find()
            ->contain(['Articles'])
            ->all();


        $view = $this->_getView([
            '_serialize' => $records,
            '_url' => 'http://localhost',
            '_entities' => [
                'Author',
                'Article'
            ]
        ]);

        $this->assertJsonStringEqualsJsonFile(
            ROOT . DS . 'tests' . DS . 'Fixture' . DS . 'authors.json',
            $view->render()
        );
    }

    public function testViewResponse()
    {
        $records = TableRegistry::get('Articles')->find()->all();

        $view = $this->_getView([
            '_entities' => [ 'Article' ],
            '_serialize' => $records
        ]);

        $output = $view->render();

        $this->assertSame('application/vnd.api+json', $view->response->type());
    }

    /**
     * Test Render
     * @return [type] [description]
     */
    public function testEncodeWithIncludeAndFieldSet()
    {
        $records = TableRegistry::get('Authors')->find()
            ->contain(['Articles'])
            ->all();

        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Author',
                'Article'
            ],
            '_serialize' => $records,
            '_include' => [ 'articles' ],
            '_fieldsets' => [ 'articles' => [ 'title' ] ]
        ]);

        $output = $view->render();
        $output = json_decode($output, true);

        $expectedSubset = [
            'included' => [
                [
                    'type' => 'articles',
                    'id' => '1',
                    'attributes' => [
                        'title' => 'First Article'
                    ]
                ]
            ]
        ];

        $this->assertArraySubset($expectedSubset, $output);
    }

    public function testOnlyMetaData()
    {
        $meta = [ 'meta' => 'data' ];
        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Article'
            ],
            '_meta' => $meta
        ]);

        $output = $view->render();
        $this->assertEquals(['meta' => $meta ], json_decode($output, true));
    }


    public function testResponseWithLinksAndMeta()
    {
        $records = TableRegistry::get('Articles')->find()->all();

        $expectedMeta = [
            'meta' => 'data'
        ];

        $view = $this->_getView([
            '_url' => 'http://localhost',
            '_entities' => [
                'Article'
            ],
            '_serialize' => $records,
            '_meta' => $expectedMeta,
            '_links' => [
                Link::FIRST => new Link('/authors?page=1'),
                Link::LAST => new Link('/authors?page=4'),
                Link::NEXT => new Link('/authors?page=6'),
                Link::LAST => new Link('/authors?page=9', [
                    'meta' => 'data'
                ])
            ]
        ]);

        $output = $view->render();
        $output = json_decode($output, true);

        $expectedLinks = [
            'first' => 'http://localhost/authors?page=1',
            'last' => [
                'href' => 'http://localhost/authors?page=9',
                'meta' => [
                    'meta' => 'data'
                ]
            ],
            'next' => 'http://localhost/authors?page=6'
        ];

        $this->assertArraySubset(['meta' => $expectedMeta], $output);
        $this->assertArraySubset(['links' => $expectedLinks], $output);
    }

    public function testJsonOptions()
    {
        $view = $this->_getView([
            '_jsonOptions' => JSON_HEX_QUOT
        ]);

        $view->render();
        $this->assertEquals(8, $view->viewVars['_jsonOptions']);

        $view = $this->_getView([
            '_jsonOptions' => false
        ]);

        $view->render();
        $this->assertEquals(0, $view->viewVars['_jsonOptions']);
    }

    public function testEmptyView()
    {
        $view = $this->_getView();
        $output = $view->render();

        $this->assertEquals(['data' => null], json_decode($output, true));
    }
{'fraction_non_alphanumeric': 0.16890934844192634, 'fraction_numerical': 0.0017705382436260624, 'mean_word_length': 1.5756953944368446, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 187, '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': '4028246', 'n_tokens_mistral': 3199, 'n_tokens_neox': 3047, 'n_words': 527}