-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
615c2fa
commit b1f43af
Showing
9 changed files
with
308 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
tests/Feature/Http/Resources/ResourceCollectionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) | ||
); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Instances/Http/Resources/SampleWithCollectionDefaultExtractCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/Instances/Http/Resources/SampleWithCollectionDefaultExtractResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/Instances/Http/Resources/SampleWithDefaultExtractCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
tests/Instances/Http/Resources/SampleWithHookCollection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?' | ||
] ); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters