Skip to content

Commit

Permalink
improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Sep 12, 2023
1 parent 38842da commit 42bd343
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 28 deletions.
18 changes: 18 additions & 0 deletions tests/Fixtures/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@

namespace OpenSoutheners\LaravelDto\Tests\Fixtures;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Post 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', 'status',
];

public function tags(): HasMany
{
return $this->hasMany(Tag::class);
}
}
26 changes: 26 additions & 0 deletions tests/Fixtures/PostCreateFormRequest.php
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',
];
}
}
32 changes: 32 additions & 0 deletions tests/Fixtures/PostFactory.php
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,
];
}
}
24 changes: 24 additions & 0 deletions tests/Fixtures/PostUpdateFormRequest.php
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',
];
}
}
33 changes: 33 additions & 0 deletions tests/Fixtures/Tag.php
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);
}
}
34 changes: 34 additions & 0 deletions tests/Fixtures/TagFactory.php
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),
];
}
}
33 changes: 33 additions & 0 deletions tests/Fixtures/UpdatePostWithRouteBindingData.php
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;
}
}
28 changes: 0 additions & 28 deletions tests/Integration/DataTransferObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,9 @@
use OpenSoutheners\LaravelDto\Tests\Fixtures\PostStatus;
use OpenSoutheners\LaravelDto\Tests\Fixtures\UpdatePostData;
use OpenSoutheners\LaravelDto\Tests\Fixtures\User;
use Orchestra\Testbench\TestCase;

class DataTransferObjectTest extends TestCase
{
/**
* 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' => '',
]);
}

public function testDataTransferObjectFromRequest()
{
$user = (new User())->forceFill([
Expand Down
57 changes: 57 additions & 0 deletions tests/Integration/TestCase.php
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,
];
}
}
64 changes: 64 additions & 0 deletions tests/Integration/ValidatedDataTransferObjectTest.php
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);
}
}
Loading

0 comments on commit 42bd343

Please sign in to comment.