Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
Expanded and improved encoding unit/integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Coen Zimmerman committed Apr 15, 2017
1 parent 14b53fc commit 4b4c16d
Show file tree
Hide file tree
Showing 21 changed files with 1,041 additions and 157 deletions.
142 changes: 108 additions & 34 deletions tests/AbstractSeededTestCase.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
namespace Czim\JsonApi\Test;

use Czim\JsonApi\Test\Helpers\Models\TestSimpleModel;
use Czim\JsonApi\Test\Helpers\Models\TestRelatedModel;
use Czim\JsonApi\Test\Helpers\Models\TestAuthor;
use Czim\JsonApi\Test\Helpers\Models\TestComment;
use Czim\JsonApi\Test\Helpers\Models\TestPost;
use Illuminate\Support\Facades\Schema;

abstract class AbstractSeededTestCase extends DatabaseTestCase
{

protected function migrateDatabase()
{
Schema::create('test_simple_models', function($table) {
Schema::create('test_simple_models', function ($table) {
/** @var \Illuminate\Database\Schema\Blueprint $table */
$table->increments('id');
$table->string('unique_field', 255);
Expand All @@ -22,65 +23,138 @@ protected function migrateDatabase()
$table->nullableTimestamps();
});

Schema::create('test_related_models', function($table) {
Schema::create('test_authors', function ($table) {
/** @var \Illuminate\Database\Schema\Blueprint $table */
$table->increments('id');
$table->string('name', 255);
$table->integer('test_simple_model_id')->nullable();
$table->enum('gender', ['m', 'f'])->default('f');
$table->string('image_file_name')->nullable();
$table->integer('image_file_size')->nullable();
$table->string('image_content_type')->nullable();
$table->timestamp('image_updated_at')->nullable();
$table->nullableTimestamps();
});

Schema::create('test_posts', function ($table) {
/** @var \Illuminate\Database\Schema\Blueprint $table */
$table->increments('id');
$table->integer('test_author_id')->nullable()->unsigned();
$table->integer('test_genre_id')->nullable()->unsigned();
$table->string('title', 50);
$table->text('body');
$table->string('description', 255)->nullable();
$table->enum('type', ['announcement', 'news', 'notice', 'periodical'])->default('news');
$table->boolean('checked')->default(false);
$table->nullableTimestamps();
});

Schema::create('test_comments', function ($table) {
/** @var \Illuminate\Database\Schema\Blueprint $table */
$table->increments('id');
$table->integer('test_post_id')->unsigned();
$table->integer('test_author_id')->nullable()->unsigned();
$table->string('title', 50)->nullable();
$table->text('body')->nullable();
$table->string('description', 255)->nullable();
$table->nullableTimestamps();
});

Schema::create('test_seos', function ($table) {
/** @var \Illuminate\Database\Schema\Blueprint $table */
$table->increments('id');
$table->integer('seoable_id')->unsigned()->nullable();
$table->string('seoable_type', 255)->nullable();
$table->string('slug', 255);
$table->nullableTimestamps();
});
}


protected function seedDatabase()
{
$this->seedSimpleModels()
->seedRelatedModels();
$this->seedAuthors()
->seedPosts()
->seedComments();
}


protected function seedSimpleModels()
protected function seedAuthors()
{
TestAuthor::create([
'name' => 'Test Testington',
]);

TestAuthor::create([
'name' => 'Tosti Tortellini Von Testering',
]);

return $this;
}

protected function seedPosts()
{
TestSimpleModel::create([
'name' => 'Test A',
'unique_field' => 'test-a',
'second_field' => 'testing something',
'active' => true,
'hidden' => 'okay',
$post = new TestPost([
'title' => 'Some Basic Title',
'body' => 'Lorem ipsum dolor sit amet, egg beater batter pan consectetur adipiscing elit.',
'type' => 'notice',
'checked' => true,
'description' => 'the best possible post for testing',
]);
$post->author()->associate(TestAuthor::first());
$post->save();

TestSimpleModel::create([
'name' => 'Test B',
'unique_field' => 'test-b',
'second_field' => 'another testing',
'active' => false,
'hidden' => 'done',

$post = new TestPost([
'title' => 'Elaborate Alternative Title',
'body' => 'Donec nec metus urna. Tosti pancake frying pan tortellini Fusce ex massa.',
'type' => 'news',
'checked' => false,
'description' => 'some alternative testing post',
]);
$post->author()->associate(TestAuthor::first());
$post->save();


$post = new TestPost([
'title' => 'Surprising Testing Title',
'body' => 'Aliquam pancake batter frying pan ut mauris eros.',
'type' => 'warning',
'checked' => true,
'description' => 'something else',
]);
$post->author()->associate(TestAuthor::skip(1)->first());
$post->save();

return $this;
}

protected function seedRelatedModels()
protected function seedComments()
{
$related = new TestRelatedModel([
'name' => 'Related X',
$comment = new TestComment([
'title' => 'Comment Title A',
'body' => 'Lorem ipsum dolor sit amet.',
'description' => 'comment one',
]);
$related->parent()->associate(TestSimpleModel::first());
$related->save();
$comment->author()->associate(TestAuthor::skip(1)->first());
TestPost::find(1)->comments()->save($comment);

$related->simples()->save(TestSimpleModel::skip(1)->first());

$related = new TestRelatedModel([
'name' => 'Related Y',
$comment = new TestComment([
'title' => 'Comment Title B',
'body' => 'Phasellus iaculis velit nec purus rutrum eleifend.',
'description' => 'comment two',
]);
$related->parent()->associate(TestSimpleModel::first());
$related->save();
$comment->author()->associate(TestAuthor::skip(1)->first());
TestPost::find(1)->comments()->save($comment);


$related = new TestRelatedModel([
'name' => 'Related Z',
$comment = new TestComment([
'title' => 'Comment Title C',
'body' => 'Nam eget magna quis arcu consectetur pellentesque.',
'description' => 'comment three',
]);
$related->parent()->associate(TestSimpleModel::first());
$related->save();
$comment->author()->associate(TestAuthor::first());
TestPost::find(3)->comments()->save($comment);

return $this;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Encoder/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
use Czim\JsonApi\Test\TestCase;
use Mockery;

/**
* Class EncoderTest
*
* @group encoding
*/
class EncoderTest extends TestCase
{

Expand Down
5 changes: 5 additions & 0 deletions tests/Encoder/Factories/TransformerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

/**
* Class TransformerFactoryTest
*
* @group encoding
*/
class TransformerFactoryTest extends TestCase
{

Expand Down
5 changes: 5 additions & 0 deletions tests/Encoder/Transformers/ErrorDataTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Czim\JsonApi\Test\TestCase;
use Mockery;

/**
* Class ErrorDataTransformerTest
*
* @group encoding
*/
class ErrorDataTransformerTest extends TestCase
{

Expand Down
5 changes: 5 additions & 0 deletions tests/Encoder/Transformers/ExceptionTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
use Illuminate\Http\Exception\HttpResponseException;
use Mockery;

/**
* Class ExceptionTransformerTest
*
* @group encoding
*/
class ExceptionTransformerTest extends TestCase
{

Expand Down
5 changes: 5 additions & 0 deletions tests/Encoder/Transformers/ModelTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use Czim\JsonApi\Test\TestCase;
use Mockery;

/**
* Class ModelTransformerTest
*
* @group encoding
*/
class ModelTransformerTest extends TestCase
{

Expand Down
5 changes: 5 additions & 0 deletions tests/Encoder/Transformers/SimpleTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Czim\JsonApi\Test\TestCase;
use Mockery;

/**
* Class SimpleTransformerTest
*
* @group encoding
*/
class SimpleTransformerTest extends TestCase
{

Expand Down
23 changes: 23 additions & 0 deletions tests/Helpers/Models/TestAuthor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace Czim\JsonApi\Test\Helpers\Models;

use Illuminate\Database\Eloquent\Model;

class TestAuthor extends Model
{
protected $fillable = [
'name',
'image',
];

public function posts()
{
return $this->hasMany(TestPost::class);
}

public function comments()
{
return $this->hasMany(TestComment::class);
}

}
29 changes: 29 additions & 0 deletions tests/Helpers/Models/TestComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace Czim\JsonApi\Test\Helpers\Models;

use Illuminate\Database\Eloquent\Model;

class TestComment extends Model
{
protected $fillable = [
'title',
'body',
'description',
];

public function author()
{
return $this->belongsTo(TestAuthor::class, 'test_author_id');
}

public function post()
{
return $this->belongsTo(TestPost::class, 'test_post_id');
}

public function seos()
{
return $this->morphMany(TestSeo::class, 'seoable');
}

}
55 changes: 55 additions & 0 deletions tests/Helpers/Models/TestPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace Czim\JsonApi\Test\Helpers\Models;

use Illuminate\Database\Eloquent\Model;

/**
* Class TestPost
*
* @property string $title
* @property string $body
* @property string $type
* @property bool $checked
* @property string $description
*/
class TestPost extends Model
{
protected $fillable = [
'title',
'body',
'type',
'checked',
'description',
];

protected $casts = [
'checked' => 'boolean',
];

public $test = false;


public function author()
{
return $this->belongsTo(TestAuthor::class, 'test_author_id');
}

public function comments()
{
return $this->hasMany(TestComment::class);
}

public function seo()
{
return $this->morphOne(TestSeo::class, 'seoable');
}

/**
* @return string
*/
public function testMethod()
{
return 'testing method value';
}

}
12 changes: 12 additions & 0 deletions tests/Helpers/Models/TestPostTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Czim\JsonApi\Test\Helpers\Models;

use Illuminate\Database\Eloquent\Model;

class TestPostTranslation extends Model
{
protected $fillable = [
'body',
];

}
Loading

0 comments on commit 4b4c16d

Please sign in to comment.