Skip to content

Commit

Permalink
ResourceCollectionTest completed;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed May 4, 2023
1 parent 615c2fa commit b1f43af
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Http/Resources/Contracts/BaseResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class BaseResourceCollection extends BaseJsonResource implements Counta
*/
public function __construct( $resource ) {
parent::__construct( $resource );

$resource ??= [];
$this->resource = $this->collectResource( $resource );
}

Expand Down Expand Up @@ -90,12 +90,12 @@ public function toArray( $request ): array {
->mergeQueriedDataInto( $data );
}

$this->loaded( $data );

if ( ! empty( $item->getExtra() ) ) {
$data = array_merge( $data, [ 'extra' => $item->getExtra() ] );
}

$this->loaded( $data );

return $data;
} );

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Http/Resources/JsonResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Hans\Tests\Valravn\Instances\Http\Resources\SampleResource;
use Hans\Tests\Valravn\Instances\Http\Resources\SampleWithDefaultExtractResource;
use Hans\Tests\Valravn\Instances\Http\Resources\SampleWithLoadedResource;
use Hans\Tests\Valravn\Instances\Http\Resources\SampleWithHookResource;
use Hans\Tests\Valravn\Instances\Http\Resources\SampleWithTypeOverrideResource;
use Hans\Tests\Valravn\TestCase;
use Hans\Valravn\Models\BaseModel;
Expand Down Expand Up @@ -232,7 +232,7 @@ public function makeAsTypeOverride(): void {
* @return void
*/
public function loaded(): void {
$resource = SampleWithLoadedResource::make( $this->model );
$resource = SampleWithHookResource::make( $this->model );
self::assertEquals(
[
'data' => [
Expand Down
158 changes: 158 additions & 0 deletions tests/Feature/Http/Resources/ResourceCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace Hans\Tests\Valravn\Feature\Http\Resources;

use Hans\Tests\Valravn\Instances\Http\Resources\SampleCollection;
use Hans\Tests\Valravn\Instances\Http\Resources\SampleWithCollectionDefaultExtractCollection;
use Hans\Tests\Valravn\Instances\Http\Resources\SampleWithDefaultExtractCollection;
use Hans\Tests\Valravn\Instances\Http\Resources\SampleWithHookCollection;
use Hans\Tests\Valravn\TestCase;
use Hans\Valravn\Models\BaseModel;
use Illuminate\Support\Collection;

class ResourceCollectionTest extends TestCase {

private Collection $models;

protected function setUp(): void {
parent::setUp();
$this->models = collect();
$object = new class extends BaseModel {
protected $fillable = [ 'name' ];
};
foreach ( range( 1, 5 ) as $counter ) {
$this->models->push(
clone $object->forceFill( [ 'id' => rand( 1, 999 ), 'name' => fake()->name() ] )
);
}
}

/**
* @test
*
* @return void
*/
public function make(): void {
$collection = SampleCollection::make( $this->models );
self::assertEquals(
[
'data' => $this->models->map(
fn( $model ) => [
'type' => 'samples',
'id' => $model->id,
]
)
->toArray(),
'type' => 'samples'
],
$this->resourceToJson( $collection )
);
}

/**
* @test
*
* @return void
*/
public function toArrayAsNull(): void {
$collection = SampleCollection::make( null );
self::assertEquals(
[
'data' => [],
'type' => 'samples'
],
$this->resourceToJson( $collection )
);
}

/**
* @test
*
* @return void
*/
public function toArrayAsNullModel(): void {
$resource = SampleCollection::make( collect( [ new class extends BaseModel { } ] ) );
self::assertEquals(
[
'data' => [
[
'type' => 'samples',
'id' => null,
]
],
'type' => 'samples'
],
$this->resourceToJson( $resource )
);
}

/**
* @test
*
* @return void
*/
public function makeAsDefaultExtract(): void {
$resource = SampleWithDefaultExtractCollection::make( $this->models );
self::assertEquals(
[
'data' => $this->models->map(
fn( $model ) => [
'type' => 'samples',
...$model->toArray()
]
)
->toArray(),
'type' => 'samples'
],
$this->resourceToJson( $resource )
);
}
/**
* @test
*
* @return void
*/
public function makeAsDefaultExtractInCollection(): void {
$resource = SampleWithCollectionDefaultExtractCollection::make( $this->models );
self::assertEquals(
[
'data' => $this->models->map(
fn( $model ) => [
'type' => 'samples',
'id' => $model->id,
'name' => $model->name,
'extract' => 'not default on resource'
]
)
->toArray(),
'type' => 'samples'
],
$this->resourceToJson( $resource )
);
}

/**
* @test
*
* @return void
*/
public function allLoaded(): void {
$resource = SampleWithHookCollection::make( $this->models );
self::assertEquals(
[
'data' => $this->models->map(
fn( $model ) => [
'type' => 'samples',
'id' => $model->id,
]
)
->toArray(),
'type' => 'samples',
'all-loaded' => 'will you still love me when i no longer young and beautiful?'
],
$this->resourceToJson( $resource )
);
}


}
27 changes: 27 additions & 0 deletions tests/Instances/Http/Resources/SampleCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Hans\Tests\Valravn\Instances\Http\Resources;

use Hans\Valravn\Http\Resources\Contracts\BaseResourceCollection;
use Illuminate\Database\Eloquent\Model;

class SampleCollection extends BaseResourceCollection {

/**
* @param Model $model
*
* @return array|null
*/
public function extract( Model $model ): ?array {
return [
'id' => $model->id,
];
}

/**
* @return string
*/
public function type(): string {
return 'samples';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Hans\Tests\Valravn\Instances\Http\Resources;

use Hans\Valravn\Http\Resources\Contracts\BaseResourceCollection;
use Illuminate\Database\Eloquent\Model;

class SampleWithCollectionDefaultExtractCollection extends BaseResourceCollection {

/**
* @param Model $model
*
* @return array|null
*/
public function extract( Model $model ): ?array {
return null;
}

/**
* @return string
*/
public function type(): string {
return 'samples';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Hans\Tests\Valravn\Instances\Http\Resources;

use Hans\Valravn\Http\Resources\Contracts\BaseJsonResource;
use Illuminate\Database\Eloquent\Model;

class SampleWithCollectionDefaultExtractResource extends BaseJsonResource {

/**
* @param Model $model
*
* @return array|null
*/
public function extract( Model $model ): ?array {
return [
'id' => $model->id,
'name' => $model->name,
'extract' => 'not default on resource'
];
}

/**
* @return string
*/
public function type(): string {
return 'samples';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Hans\Tests\Valravn\Instances\Http\Resources;

use Hans\Valravn\Http\Resources\Contracts\BaseResourceCollection;
use Illuminate\Database\Eloquent\Model;

class SampleWithDefaultExtractCollection extends BaseResourceCollection {

/**
* @param Model $model
*
* @return array|null
*/
public function extract( Model $model ): ?array {
return null;
}

/**
* @return string
*/
public function type(): string {
return 'samples';
}
}
38 changes: 38 additions & 0 deletions tests/Instances/Http/Resources/SampleWithHookCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Hans\Tests\Valravn\Instances\Http\Resources;

use Hans\Valravn\Http\Resources\Contracts\BaseResourceCollection;
use Illuminate\Database\Eloquent\Model;

class SampleWithHookCollection extends BaseResourceCollection {

/**
* @param Model $model
*
* @return array|null
*/
public function extract( Model $model ): ?array {
return [
'id' => $model->id,
];
}

/**
* @return string
*/
public function type(): string {
return 'samples';
}

/**
* @return void
*/
protected function allLoaded(): void {
$this->addAdditional( [
'all-loaded' => 'will you still love me when i no longer young and beautiful?'
] );
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Hans\Valravn\Http\Resources\Contracts\BaseJsonResource;
use Illuminate\Database\Eloquent\Model;

class SampleWithLoadedResource extends BaseJsonResource {
class SampleWithHookResource extends BaseJsonResource {

public function extract( Model $model ): ?array {
return [
Expand Down

0 comments on commit b1f43af

Please sign in to comment.