generated from open-southeners/php-package-template
-
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
Showing
13 changed files
with
214 additions
and
22 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
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 |
---|---|---|
|
@@ -13,9 +13,12 @@ | |
use Workbench\App\DataTransferObjects\UpdatePostData; | ||
use Workbench\App\DataTransferObjects\UpdatePostWithDefaultData; | ||
use Workbench\App\Enums\PostStatus; | ||
use Workbench\App\Models\Film; | ||
use Workbench\App\Models\Post; | ||
use Workbench\App\Models\User; | ||
use Workbench\Database\Factories\FilmFactory; | ||
use Workbench\Database\Factories\PostFactory; | ||
use Workbench\Database\Factories\TagFactory; | ||
|
||
class DataTransferObjectTest extends TestCase | ||
{ | ||
|
@@ -197,6 +200,69 @@ public function testDataTransferObjectWithDefaultValueAttributeGetsBoundWhenOneI | |
]); | ||
} | ||
|
||
public function testDataTransferObjectWithMorphsGetsModelsBoundOfEachTypeSent() | ||
{ | ||
$user = User::create([ | ||
'email' => '[email protected]', | ||
'password' => '1234', | ||
'name' => 'Ruben', | ||
]); | ||
|
||
$this->actingAs($user); | ||
|
||
$horrorTag = TagFactory::new()->create([ | ||
'name' => 'Horror', | ||
'slug' => 'horror', | ||
]); | ||
|
||
$fooBarPost = PostFactory::new()->create([ | ||
'title' => 'Foo bar', | ||
'slug' => 'foo-bar', | ||
]); | ||
|
||
$helloWorldPost = PostFactory::new()->create([ | ||
'title' => 'Hello world', | ||
'slug' => 'hello-world', | ||
]); | ||
|
||
$myFilm = FilmFactory::new()->create([ | ||
'title' => 'My Film', | ||
'slug' => 'my-film', | ||
'year' => 1997 | ||
]); | ||
|
||
$response = $this->patchJson('tags/1', [ | ||
'name' => 'Scary', | ||
'taggable' => '1, 1, 2', | ||
'taggable_type' => 'film, post', | ||
]); | ||
|
||
$response->assertSuccessful(); | ||
|
||
$response->assertJsonCount(3, 'data.taggable'); | ||
|
||
$response->assertJsonFragment([ | ||
"id" => 1, | ||
"title" => "My Film", | ||
"year" => "1997", | ||
"about" => null | ||
]); | ||
|
||
$response->assertJsonFragment([ | ||
"id" => 1, | ||
"title" => "Foo bar", | ||
"slug" => "foo-bar", | ||
"status" => "published" | ||
]); | ||
|
||
$response->assertJsonFragment([ | ||
"id" => 2, | ||
"title" => "Hello world", | ||
"slug" => "hello-world", | ||
"status" => "published" | ||
]); | ||
} | ||
|
||
public function testNestedDataTransferObjectsGetsTheNestedAsObjectInstance() | ||
{ | ||
$this->markTestIncomplete('Need to create nested actions/DTOs'); | ||
|
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
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,33 @@ | ||
<?php | ||
|
||
namespace Workbench\App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\MorphToMany; | ||
|
||
class Film extends Model | ||
{ | ||
use HasFactory; | ||
|
||
/** | ||
* The attributes that aren't mass assignable. | ||
* | ||
* @var array<string>|bool | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* The attributes that should be visible in serialization. | ||
* | ||
* @var array<string> | ||
*/ | ||
protected $visible = [ | ||
'id', 'title', 'year', 'about', | ||
]; | ||
|
||
public function tags(): MorphToMany | ||
{ | ||
return $this->morphToMany(Tag::class, 'taggable'); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Workbench\Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Str; | ||
use Workbench\App\Models\Film; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\Workbench\App\Models\Film> | ||
*/ | ||
class FilmFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = Film::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
$name = $this->faker->unique()->words(2, true); | ||
|
||
return [ | ||
'title' => $name, | ||
'slug' => Str::slug($name), | ||
'year' => $this->faker->year(), | ||
]; | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
workbench/database/migrations/2022_05_32_163139_create_films_table.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,36 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('films', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('title'); | ||
$table->string('slug')->unique(); | ||
$table->string('year')->index(); | ||
$table->text('about')->nullable(); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('films'); | ||
} | ||
}; |
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