<?php

namespace Tricks\LaravelTools\Traits;

trait EndpointSnapshotTestTrait
{
    public function endpointSnapshot(string $model)
    {
        expect(method_exists($model::factory(), 'testingState'))
            ->toBeTrue("The model $model needs to have a factory with a defined state named testingState");

        expect($model::apiUrl)->not()->toBeNull('models need have apiUrl-constant set for endpoint-tests');

        $model::factory()
            ->testingState()
            ->create();

        $test = $this->getJson($model::apiUrl);
        $test->assertSuccessful();
        $content = $test->content();
        $content = preg_replace(
            '/"http[^"]+"/',
            '"static"',
            $content
        );

        expect($content)->toMatchSnapshot();
    }
}
