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
11 changed files
with
363 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\LaravelDto\Tests\Fixtures; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class PostCreateFormRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'title' => 'string', | ||
'content' => ['nullable', 'string'], | ||
'author_id' => ['nullable', 'int'], | ||
'category_id' => ['nullable', Rule::exists('categories'. 'id')], | ||
'tags' => ['nullable', 'array'], | ||
'publish_at' => 'datetime', | ||
]; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\LaravelDto\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\OpenSoutheners\LaravelDto\Tests\Fixtures\Post> | ||
*/ | ||
class PostFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = Post::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'title' => $this->faker->title(), | ||
'status' => PostStatus::Published->value, | ||
]; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\LaravelDto\Tests\Fixtures; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class PostUpdateFormRequest extends FormRequest | ||
{ | ||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'title' => 'string', | ||
'content' => ['nullable', 'string'], | ||
'tags' => ['nullable', 'string'], | ||
'publish_at' => 'datetime', | ||
]; | ||
} | ||
} |
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 OpenSoutheners\LaravelDto\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class Tag 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', 'name', 'slug', | ||
]; | ||
|
||
public function post(): BelongsTo | ||
{ | ||
return $this->belongsTo(Post::class); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\LaravelDto\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Support\Str; | ||
|
||
/** | ||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\OpenSoutheners\LaravelDto\Tests\Fixtures\Tag> | ||
*/ | ||
class TagFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = Tag::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
$name = $this->faker->unique()->company(); | ||
|
||
return [ | ||
'name' => $name, | ||
'slug' => Str::slug($name), | ||
]; | ||
} | ||
} |
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 OpenSoutheners\LaravelDto\Tests\Fixtures; | ||
|
||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Collection; | ||
use OpenSoutheners\LaravelDto\Contracts\ValidatedDataTransferObject; | ||
use OpenSoutheners\LaravelDto\DataTransferObject; | ||
use stdClass; | ||
|
||
class UpdatePostWithRouteBindingData extends DataTransferObject implements ValidatedDataTransferObject | ||
{ | ||
/** | ||
* @param \Illuminate\Support\Collection<\OpenSoutheners\LaravelDto\Tests\Fixtures\Tag>|null $tags | ||
*/ | ||
public function __construct( | ||
public Post $post, | ||
public ?string $title = null, | ||
public ?stdClass $content = null, | ||
public ?PostStatus $postStatus = null, | ||
public ?Collection $tags = null, | ||
public ?Carbon $publishedAt = null, | ||
public ?Authenticatable $currentUser = null | ||
) { | ||
// | ||
} | ||
|
||
public static function request(): string | ||
{ | ||
return PostUpdateFormRequest::class; | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\LaravelDto\Tests\Integration; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use OpenSoutheners\LaravelDto\ServiceProvider; | ||
use OpenSoutheners\LaravelDto\Tests\Fixtures\Tag; | ||
|
||
class TestCase extends \Orchestra\Testbench\TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Factory::useNamespace(''); | ||
} | ||
|
||
/** | ||
* Define database migrations. | ||
* | ||
* @return void | ||
*/ | ||
protected function defineDatabaseMigrations() | ||
{ | ||
$this->loadMigrationsFrom(__DIR__.'/../database'); | ||
} | ||
|
||
/** | ||
* Define environment setup. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return void | ||
*/ | ||
protected function defineEnvironment($app) | ||
{ | ||
// Setup default database to use sqlite :memory: | ||
$app['config']->set('database.default', 'testing'); | ||
$app['config']->set('database.connections.testing', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
} | ||
|
||
/** | ||
* Get package providers. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return array<int, class-string> | ||
*/ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
ServiceProvider::class, | ||
]; | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
|
||
namespace OpenSoutheners\LaravelDto\Tests\Integration; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Route; | ||
use OpenSoutheners\LaravelDto\Tests\Fixtures\UpdatePostWithRouteBindingData; | ||
use OpenSoutheners\LaravelDto\Tests\Fixtures\Post; | ||
use OpenSoutheners\LaravelDto\Tests\Fixtures\Tag; | ||
|
||
class ValidatedDataTransferObjectTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->withoutExceptionHandling(); | ||
|
||
Route::patch('post/{post}', function (UpdatePostWithRouteBindingData $data) { | ||
return response()->json($data->toArray()); | ||
}); | ||
} | ||
|
||
public function testValidatedDataTransferObjectGetsRouteBoundModel() | ||
{ | ||
$post = Post::factory()->create(); | ||
|
||
$response = $this->patchJson('post/1', []); | ||
|
||
$response->assertJson([ | ||
'post' => $post->toArray(), | ||
], true); | ||
} | ||
|
||
public function testValidatedDataTransferObjectGetsValidatedOnlyParameters() | ||
{ | ||
Post::factory()->create(); | ||
|
||
$firstTag = Tag::factory()->create(); | ||
$secondTag = Tag::factory()->create(); | ||
|
||
$response = $this->patchJson('post/1', [ | ||
'tags' => '1,2', | ||
'post_status' => 'test_non_existing_status', | ||
]); | ||
|
||
$response->assertJson([ | ||
'tags' => [ | ||
[ | ||
'id' => $firstTag->getKey(), | ||
'name' => $firstTag->name, | ||
'slug' => $firstTag->slug, | ||
], | ||
[ | ||
'id' => $secondTag->getKey(), | ||
'name' => $secondTag->name, | ||
'slug' => $secondTag->slug, | ||
], | ||
], | ||
], true); | ||
} | ||
} |
Oops, something went wrong.