count(fake()->randomDigitNotNull()) ->create(); $response = get(route('api.videoscript.index')); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query()) ->response() ->getData() ), true ) ); }); test('paginated', function () { VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->create(); $response = get(route('api.videoscript.index')); $response->assertJsonStructure([ ScriptCollection::$wrap, 'links', 'meta', ]); }); test('allowed include paths', function () { $schema = new ScriptSchema(); $allowedIncludes = collect($schema->allowedIncludes()); $selectedIncludes = $allowedIncludes->random(fake()->numberBetween(1, $allowedIncludes->count())); $includedPaths = $selectedIncludes->map(fn (AllowedInclude $include) => $include->path()); $parameters = [ IncludeParser::param() => $includedPaths->join(','), ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for(Video::factory()) ->create(); $scripts = VideoScript::with($includedPaths->all())->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('sparse fieldsets', function () { $schema = new ScriptSchema(); $fields = collect($schema->fields()); $includedFields = $fields->random(fake()->numberBetween(1, $fields->count())); $parameters = [ FieldParser::param() => [ ScriptResource::$wrap => $includedFields->map(fn (Field $field) => $field->getKey())->join(','), ], ]; $scripts = VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->create(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('sorts', function () { $schema = new ScriptSchema(); /** @var Sort $sort */ $sort = collect($schema->fields()) ->filter(fn (Field $field) => $field instanceof SortableField) ->map(fn (SortableField $field) => $field->getSort()) ->random(); $parameters = [ SortParser::param() => $sort->format(Arr::random(Direction::cases())), ]; $query = new Query($parameters); VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->create(); $response = get(route('api.videoscript.index', $parameters)); $scripts = $this->sort(VideoScript::query(), $query, $schema)->get(); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, $query) ->response() ->getData() ), true ) ); }); test('created at filter', function () { $createdFilter = fake()->date(); $excludedDate = fake()->date(); $parameters = [ FilterParser::param() => [ BaseModel::ATTRIBUTE_CREATED_AT => $createdFilter, ], PagingParser::param() => [ OffsetCriteria::SIZE_PARAM => Criteria::MAX_RESULTS, ], ]; Carbon::withTestNow($createdFilter, function () { VideoScript::factory()->count(fake()->randomDigitNotNull())->create(); }); Carbon::withTestNow($excludedDate, function () { VideoScript::factory()->count(fake()->randomDigitNotNull())->create(); }); $script = VideoScript::query()->where(BaseModel::ATTRIBUTE_CREATED_AT, $createdFilter)->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($script, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('updated at filter', function () { $updatedFilter = fake()->date(); $excludedDate = fake()->date(); $parameters = [ FilterParser::param() => [ BaseModel::ATTRIBUTE_UPDATED_AT => $updatedFilter, ], PagingParser::param() => [ OffsetCriteria::SIZE_PARAM => Criteria::MAX_RESULTS, ], ]; Carbon::withTestNow($updatedFilter, function () { VideoScript::factory()->count(fake()->randomDigitNotNull())->create(); }); Carbon::withTestNow($excludedDate, function () { VideoScript::factory()->count(fake()->randomDigitNotNull())->create(); }); $script = VideoScript::query()->where(BaseModel::ATTRIBUTE_UPDATED_AT, $updatedFilter)->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($script, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('without trashed filter', function () { $parameters = [ FilterParser::param() => [ TrashedCriteria::PARAM_VALUE => TrashedStatus::WITHOUT->value, ], PagingParser::param() => [ OffsetCriteria::SIZE_PARAM => Criteria::MAX_RESULTS, ], ]; VideoScript::factory()->count(fake()->randomDigitNotNull())->create(); VideoScript::factory()->trashed()->count(fake()->randomDigitNotNull())->create(); $script = VideoScript::withoutTrashed()->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($script, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('with trashed filter', function () { $parameters = [ FilterParser::param() => [ TrashedCriteria::PARAM_VALUE => TrashedStatus::WITH->value, ], PagingParser::param() => [ OffsetCriteria::SIZE_PARAM => Criteria::MAX_RESULTS, ], ]; VideoScript::factory()->count(fake()->randomDigitNotNull())->create(); VideoScript::factory()->trashed()->count(fake()->randomDigitNotNull())->create(); $script = VideoScript::withTrashed()->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($script, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('only trashed filter', function () { $parameters = [ FilterParser::param() => [ TrashedCriteria::PARAM_VALUE => TrashedStatus::ONLY->value, ], PagingParser::param() => [ OffsetCriteria::SIZE_PARAM => Criteria::MAX_RESULTS, ], ]; VideoScript::factory()->count(fake()->randomDigitNotNull())->create(); VideoScript::factory()->trashed()->count(fake()->randomDigitNotNull())->create(); $script = VideoScript::onlyTrashed()->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($script, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('deleted at filter', function () { $deletedFilter = fake()->date(); $excludedDate = fake()->date(); $parameters = [ FilterParser::param() => [ ModelConstants::ATTRIBUTE_DELETED_AT => $deletedFilter, TrashedCriteria::PARAM_VALUE => TrashedStatus::WITH->value, ], PagingParser::param() => [ OffsetCriteria::SIZE_PARAM => Criteria::MAX_RESULTS, ], ]; Carbon::withTestNow($deletedFilter, function () { VideoScript::factory()->trashed()->count(fake()->randomDigitNotNull())->create(); }); Carbon::withTestNow($excludedDate, function () { VideoScript::factory()->trashed()->count(fake()->randomDigitNotNull())->create(); }); $script = VideoScript::withTrashed()->where(ModelConstants::ATTRIBUTE_DELETED_AT, $deletedFilter)->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($script, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('videos by lyrics', function () { $lyricsFilter = fake()->boolean(); $parameters = [ FilterParser::param() => [ Video::ATTRIBUTE_LYRICS => $lyricsFilter, ], IncludeParser::param() => VideoScript::RELATION_VIDEO, ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for(Video::factory()) ->create(); $scripts = VideoScript::with([ VideoScript::RELATION_VIDEO => function (BelongsTo $query) use ($lyricsFilter) { $query->where(Video::ATTRIBUTE_LYRICS, $lyricsFilter); }, ]) ->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('videos by nc', function () { $ncFilter = fake()->boolean(); $parameters = [ FilterParser::param() => [ Video::ATTRIBUTE_NC => $ncFilter, ], IncludeParser::param() => VideoScript::RELATION_VIDEO, ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for(Video::factory()) ->create(); $scripts = VideoScript::with([ VideoScript::RELATION_VIDEO => function (BelongsTo $query) use ($ncFilter) { $query->where(Video::ATTRIBUTE_NC, $ncFilter); }, ]) ->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('videos by overlap', function () { $overlapFilter = Arr::random(VideoOverlap::cases()); $parameters = [ FilterParser::param() => [ Video::ATTRIBUTE_OVERLAP => $overlapFilter->localize(), ], IncludeParser::param() => VideoScript::RELATION_VIDEO, ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for(Video::factory()) ->create(); $scripts = VideoScript::with([ VideoScript::RELATION_VIDEO => function (BelongsTo $query) use ($overlapFilter) { $query->where(Video::ATTRIBUTE_OVERLAP, $overlapFilter->value); }, ]) ->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('videos by resolution', function () { $resolutionFilter = fake()->randomNumber(); $excludedResolution = $resolutionFilter + 1; $parameters = [ FilterParser::param() => [ Video::ATTRIBUTE_RESOLUTION => $resolutionFilter, ], IncludeParser::param() => VideoScript::RELATION_VIDEO, ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for( Video::factory()->state([ Video::ATTRIBUTE_RESOLUTION => fake()->boolean() ? $resolutionFilter : $excludedResolution, ]) ) ->create(); $scripts = VideoScript::with([ VideoScript::RELATION_VIDEO => function (BelongsTo $query) use ($resolutionFilter) { $query->where(Video::ATTRIBUTE_RESOLUTION, $resolutionFilter); }, ]) ->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('videos by source', function () { $sourceFilter = Arr::random(VideoSource::cases()); $parameters = [ FilterParser::param() => [ Video::ATTRIBUTE_SOURCE => $sourceFilter->localize(), ], IncludeParser::param() => VideoScript::RELATION_VIDEO, ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for(Video::factory()) ->create(); $scripts = VideoScript::with([ VideoScript::RELATION_VIDEO => function (BelongsTo $query) use ($sourceFilter) { $query->where(Video::ATTRIBUTE_SOURCE, $sourceFilter->value); }, ]) ->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('videos by subbed', function () { $subbedFilter = fake()->boolean(); $parameters = [ FilterParser::param() => [ Video::ATTRIBUTE_SUBBED => $subbedFilter, ], IncludeParser::param() => VideoScript::RELATION_VIDEO, ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for(Video::factory()) ->create(); $scripts = VideoScript::with([ VideoScript::RELATION_VIDEO => function (BelongsTo $query) use ($subbedFilter) { $query->where(Video::ATTRIBUTE_SUBBED, $subbedFilter); }, ]) ->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); }); test('videos by uncen', function () { $uncenFilter = fake()->boolean(); $parameters = [ FilterParser::param() => [ Video::ATTRIBUTE_UNCEN => $uncenFilter, ], IncludeParser::param() => VideoScript::RELATION_VIDEO, ]; VideoScript::factory() ->count(fake()->randomDigitNotNull()) ->for(Video::factory()) ->create(); $scripts = VideoScript::with([ VideoScript::RELATION_VIDEO => function (BelongsTo $query) use ($uncenFilter) { $query->where(Video::ATTRIBUTE_UNCEN, $uncenFilter); }, ]) ->get(); $response = get(route('api.videoscript.index', $parameters)); $response->assertJson( json_decode( json_encode( new ScriptCollection($scripts, new Query($parameters)) ->response() ->getData() ), true ) ); });