Add new suite for database testing

This commit is contained in:
Samuel Georges 2015-09-09 19:28:47 +10:00
parent 50812adcaf
commit cd1edcb055
11 changed files with 313 additions and 4 deletions

View File

@ -0,0 +1,17 @@
<?php namespace Database\Tester;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function pluginDetails()
{
return [
'name' => 'Database Tester Plugin',
'description' => 'Plugin for loading tests that involve the database.',
'author' => 'Alexey Bobkov, Samuel Georges'
];
}
}

View File

@ -0,0 +1,28 @@
<?php namespace Database\Tester\Models;
use Model;
/**
* Author Model
*/
class Author extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'database_tester_authors';
/**
* @var array Guarded fields
*/
protected $guarded = [];
/**
* @var array Relations
*/
public $hasMany = [
'posts' => 'Database\Tester\Models\Post',
];
}

View File

@ -0,0 +1,33 @@
<?php namespace Database\Tester\Models;
use Model;
/**
* Post Model
*/
class Post extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'database_tester_posts';
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = [];
/**
* @var array Relations
*/
public $belongsTo = [
'author' => 'Database\Tester\Models\Author',
];
}

View File

@ -0,0 +1,30 @@
<?php namespace Database\Tester\Models;
use Model;
/**
* Post Model
*/
class SluggablePost extends Model
{
use \October\Rain\Database\Traits\Sluggable;
/**
* @var string The database table used by the model.
*/
public $table = 'database_tester_posts';
/**
* @var array Guarded fields
*/
protected $guarded = [];
/**
* @var array List of attributes to automatically generate unique URL names (slugs) for.
*/
protected $slugs = [
'slug' => 'title'
];
}

View File

@ -0,0 +1,26 @@
<?php namespace Database\Tester\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class CreateAuthorsTable extends Migration
{
public function up()
{
Schema::create('database_tester_authors', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name')->nullable();
$table->string('email')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('database_tester_authors');
}
}

View File

@ -0,0 +1,30 @@
<?php namespace Database\Tester\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('database_tester_posts', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('title')->nullable();
$table->string('slug')->nullable()->index();
$table->text('description')->nullable();
$table->boolean('is_published')->default(false);
$table->timestamp('published_at')->nullable();
$table->integer('author_id')->unsigned()->index()->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('database_tester_posts');
}
}

View File

@ -0,0 +1,5 @@
1.0.1: First version of Tester
1.0.2:
- Create tables
- create_posts_table.php
- create_authors_table.php

View File

@ -0,0 +1,49 @@
<?php
use Database\Tester\Models\Post;
use Database\Tester\Models\Author;
class BelongsToModelTest extends PluginTestCase
{
public function setUp()
{
parent::setUp();
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Post.php';
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Author.php';
$this->runPluginRefreshCommand('Database.Tester');
}
public function testSetRelationValueBelongsTo()
{
Model::unguard();
$post = Post::create(['title' => "First post", 'description' => "Yay!!"]);
$author1 = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
$author2 = Author::create(['name' => 'Louie', 'email' => 'louie@email.tld']);
$author3 = Author::make(['name' => 'Charlie', 'email' => 'charlie@email.tld']);
Model::reguard();
// Set by Model object
$post->author = $author1;
$this->assertEquals($author1->id, $post->author_id);
$this->assertEquals('Stevie', $post->author->name);
// Set by primary key
$post->author = $author2->id;
$this->assertEquals($author2->id, $post->author_id);
$this->assertEquals('Louie', $post->author->name);
// Nullify
$post->author = null;
$this->assertNull($post->author_id);
$this->assertNull($post->author);
// Deferred
$post->author = $author3;
$this->assertEquals('Charlie', $post->author->name);
$this->assertNull($post->author_id);
$author3->save();
$this->assertEquals($author3->id, $post->author_id);
}
}

View File

@ -0,0 +1,34 @@
<?php
use Database\Tester\Models\Post;
class ModelTest extends PluginTestCase
{
public function setUp()
{
parent::setUp();
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Post.php';
$this->runPluginRefreshCommand('Database.Tester');
}
public function testCreateFirstPost()
{
Post::truncate();
$post = new Post;
$post->title = "First post";
$post->description = "Yay!!";
$post->save();
$this->assertEquals(1, $post->id);
}
/**
* @expectedException \Illuminate\Database\Eloquent\MassAssignmentException
* @expectedExceptionMessage title
*/
public function testGuardedAttribute()
{
Post::create(['title' => 'Hi!', 'slug' => 'authenticity']);
}
}

View File

@ -0,0 +1,51 @@
<?php
use Database\Tester\Models\SluggablePost;
class SluggableModelTest extends PluginTestCase
{
public function setUp()
{
parent::setUp();
include_once base_path().'/tests/fixtures/plugins/database/tester/models/SluggablePost.php';
$this->runPluginRefreshCommand('Database.Tester');
}
public function testFillPost()
{
$post = SluggablePost::create(['title' => 'Hello World!']);
$this->assertEquals('hello-world', $post->slug);
}
public function testSetAttributeOnPost()
{
$post = new SluggablePost;
$post->title = "Let's go, rock show!";
$post->save();
$this->assertEquals('lets-go-rock-show', $post->slug);
}
public function testSetSlugAttributeManually()
{
$post = new SluggablePost;
$post->title = 'We parked in a comfortable spot';
$post->slug = 'war-is-pain';
$post->save();
$this->assertEquals('war-is-pain', $post->slug);
}
public function testDuplicateSlug()
{
$post1 = SluggablePost::create(['title' => 'Pace yourself']);
$post2 = SluggablePost::create(['title' => 'Pace yourself']);
$post3 = SluggablePost::create(['title' => 'Pace yourself']);
$this->assertEquals('pace-yourself', $post1->slug);
$this->assertEquals('pace-yourself-2', $post2->slug);
$this->assertEquals('pace-yourself-3', $post3->slug);
}
}

View File

@ -52,15 +52,17 @@ class PluginManagerTest extends TestCase
$manager = PluginManager::instance();
$result = self::callProtectedMethod($manager, 'loadPlugins');
$this->assertCount(4, $result);
$this->assertCount(5, $result);
$this->assertArrayHasKey('October.NoUpdates', $result);
$this->assertArrayHasKey('October.Sample', $result);
$this->assertArrayHasKey('October.Tester', $result);
$this->assertArrayHasKey('Database.Tester', $result);
$this->assertArrayHasKey('TestVendor.Test', $result);
$this->assertInstanceOf('October\NoUpdates\Plugin', $result['October.NoUpdates']);
$this->assertInstanceOf('October\Sample\Plugin', $result['October.Sample']);
$this->assertInstanceOf('October\Tester\Plugin', $result['October.Tester']);
$this->assertInstanceOf('Database\Tester\Plugin', $result['Database.Tester']);
$this->assertInstanceOf('TestVendor\Test\Plugin', $result['TestVendor.Test']);
}
@ -77,15 +79,17 @@ class PluginManagerTest extends TestCase
$manager = PluginManager::instance();
$result = $manager->getPlugins();
$this->assertCount(4, $result);
$this->assertCount(5, $result);
$this->assertArrayHasKey('October.NoUpdates', $result);
$this->assertArrayHasKey('October.Sample', $result);
$this->assertArrayHasKey('October.Tester', $result);
$this->assertArrayHasKey('Database.Tester', $result);
$this->assertArrayHasKey('TestVendor.Test', $result);
$this->assertInstanceOf('October\NoUpdates\Plugin', $result['October.NoUpdates']);
$this->assertInstanceOf('October\Sample\Plugin', $result['October.Sample']);
$this->assertInstanceOf('October\Tester\Plugin', $result['October.Tester']);
$this->assertInstanceOf('Database\Tester\Plugin', $result['Database.Tester']);
$this->assertInstanceOf('TestVendor\Test\Plugin', $result['TestVendor.Test']);
}
@ -111,10 +115,11 @@ class PluginManagerTest extends TestCase
$manager = PluginManager::instance();
$result = $manager->getPluginNamespaces();
$this->assertCount(4, $result);
$this->assertCount(5, $result);
$this->assertArrayHasKey('\october\noupdates', $result);
$this->assertArrayHasKey('\october\sample', $result);
$this->assertArrayHasKey('\october\tester', $result);
$this->assertArrayHasKey('\database\tester', $result);
$this->assertArrayHasKey('\testvendor\test', $result);
}
@ -124,8 +129,9 @@ class PluginManagerTest extends TestCase
$vendors = $manager->getVendorAndPluginNames();
$this->assertArrayHasKey('october', $vendors);
$this->assertArrayHasKey('database', $vendors);
$this->assertArrayHasKey('testvendor', $vendors);
$this->assertCount(2, $vendors);
$this->assertCount(3, $vendors);
}
public function testPluginDetails()