<commit_before><?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use Faker\Factory as Faker;

use Tests\Helpers\Factory;

use App\Models\Collections\AgentType;
use App\Models\Collections\Agent;

abstract class ApiTestCase extends TestCase
{

    use Factory;

    protected $faker;


    public function setUp()
    {

        parent::setUp();

        config(['elasticsearch.defaultConnection' => 'testing']);

        \Artisan::call('migrate');

        if (get_class($this) != 'Tests\Unit\AgentTypeTest')
        {

            $agentTypeId = $this->make(AgentType::class, ['title' => 'Artist']);

            if (get_class($this) != 'Tests\Unit\AgentTest')
            {

                $this->times(1)->make(Agent::class, ['agent_type_citi_id' => $agentTypeId]);

            }

        }

    }

    protected function assertArrayHasKeys($resources = [], $keys = [], $arrayIsMultipleObjects = false)
    {

        foreach ($keys as $key)
        {

            if ($arrayIsMultipleObjects) {

                foreach ($resources as $resource)
                {

                    $this->assertArrayHasKey($key, $resource);

                }

            }
            else
            {

                $this->assertArrayHasKey($key, $resources);

            }

        }

    }

    public function it_fetches_all($class, $endpoint)
    {

        $this->times(5)->make($class);

        $response = $this->getJson('api/v1/' .$endpoint);
        $response->assertSuccessful();

        $resources = $response->json()['data'];
        $this->assertCount(5, $resources);

        foreach ($resources as $resource)
        {
            $this->assertArrayHasKeys($resource, ['id', 'title']);
        }

        return $resources;
    }

    public function it_fetches_a_single($class, $endpoint, $extraValue = '')
    {

        $id = $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'/' .$id .($extraValue ? '/' .$extraValue : ''));
        $response->assertSuccessful();

        $resource = $response->json()['data'];
        $this->assertArrayHasKeys($resource, ['id', 'title']);

        return $resource;
    }

    public function it_fetches_multiple($class, $endpoint)
    {

        $this->times(5)->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'?ids=' .implode(',',array_slice($this->ids, -3, 3)));
        $response->assertSuccessful();

        $resources = $response->json()['data'];
        $this->assertCount(3, $resources);

        foreach ($resources as $resource)
        {
            $this->assertArrayHasKeys($resource, ['id', 'title']);
        }

        return $resources;
    }

    public function it_400s($class, $endpoint)
    {

        $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'/fsdfdfs');

        $response->assertStatus(400);

    }

    public function it_403s($class, $endpoint)
    {

        $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'?limit=2000');

        $response->assertStatus(403);

    }

    public function it_404s($class, $endpoint, $useUuid = false)
    {

        $this->faker = Faker::create();

        $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'/' .$useUuid ? $this->faker->unique()->uuid : $this->faker->unique()->randomNumber(5));

        $response->assertStatus(404);

    }

    public function it_405s($class, $endpoint)
    {

        $this->make($class);

        $response = $this->postJson('api/v1/' .$endpoint);

        $response->assertStatus(405);

    }

}<commit_msg>Move assertArrayHasKeys to bottom of ApiTestCase<commit_after><?php

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

use Faker\Factory as Faker;

use Tests\Helpers\Factory;

use App\Models\Collections\AgentType;
use App\Models\Collections\Agent;

abstract class ApiTestCase extends TestCase
{

    use Factory;

    protected $faker;


    public function setUp()
    {

        parent::setUp();

        config(['elasticsearch.defaultConnection' => 'testing']);

        \Artisan::call('migrate');

        if (get_class($this) != 'Tests\Unit\AgentTypeTest')
        {

            $agentTypeId = $this->make(AgentType::class, ['title' => 'Artist']);

            if (get_class($this) != 'Tests\Unit\AgentTest')
            {

                $this->times(1)->make(Agent::class, ['agent_type_citi_id' => $agentTypeId]);

            }

        }

    }

    public function it_fetches_all($class, $endpoint)
    {

        $this->times(5)->make($class);

        $response = $this->getJson('api/v1/' .$endpoint);
        $response->assertSuccessful();

        $resources = $response->json()['data'];
        $this->assertCount(5, $resources);

        foreach ($resources as $resource)
        {
            $this->assertArrayHasKeys($resource, ['id', 'title']);
        }

        return $resources;
    }

    public function it_fetches_a_single($class, $endpoint, $extraValue = '')
    {

        $id = $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'/' .$id .($extraValue ? '/' .$extraValue : ''));
        $response->assertSuccessful();

        $resource = $response->json()['data'];
        $this->assertArrayHasKeys($resource, ['id', 'title']);

        return $resource;
    }

    public function it_fetches_multiple($class, $endpoint)
    {

        $this->times(5)->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'?ids=' .implode(',',array_slice($this->ids, -3, 3)));
        $response->assertSuccessful();

        $resources = $response->json()['data'];
        $this->assertCount(3, $resources);

        foreach ($resources as $resource)
        {
            $this->assertArrayHasKeys($resource, ['id', 'title']);
        }

        return $resources;
    }

    public function it_400s($class, $endpoint)
    {

        $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'/fsdfdfs');

        $response->assertStatus(400);

    }

    public function it_403s($class, $endpoint)
    {

        $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'?limit=2000');

        $response->assertStatus(403);

    }

    public function it_404s($class, $endpoint, $useUuid = false)
    {

        $this->faker = Faker::create();

        $this->make($class);

        $response = $this->getJson('api/v1/' .$endpoint .'/' .$useUuid ? $this->faker->unique()->uuid : $this->faker->unique()->randomNumber(5));

        $response->assertStatus(404);

    }

    public function it_405s($class, $endpoint)
    {

        $this->make($class);

        $response = $this->postJson('api/v1/' .$endpoint);

        $response->assertStatus(405);

    }

    protected function assertArrayHasKeys($resources = [], $keys = [], $arrayIsMultipleObjects = false)
    {

        foreach ($keys as $key)
        {

            if ($arrayIsMultipleObjects) {

                foreach ($resources as $resource)
                {

                    $this->assertArrayHasKey($key, $resource);

                }

            }
            else
            {

                $this->assertArrayHasKey($key, $resources);

            }

        }

    }

}
{'fraction_non_alphanumeric': 0.17033773861967694, 'fraction_numerical': 0.011480443198504872, 'mean_word_length': 2.2744755244755246, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 97, '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': '5419240', 'n_tokens_mistral': 2366, 'n_tokens_neox': 2193, 'n_words': 371}