From cd1edcb055ba1758d8bb93829aa3d1367709d462 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Wed, 9 Sep 2015 19:28:47 +1000 Subject: [PATCH] Add new suite for database testing --- .../plugins/database/tester/Plugin.php | 17 +++++++ .../plugins/database/tester/models/Author.php | 28 ++++++++++ .../plugins/database/tester/models/Post.php | 33 ++++++++++++ .../database/tester/models/SluggablePost.php | 30 +++++++++++ .../tester/updates/create_authors_table.php | 26 ++++++++++ .../tester/updates/create_posts_table.php | 30 +++++++++++ .../database/tester/updates/version.yaml | 5 ++ .../plugins/database/BelongsToModelTest.php | 49 ++++++++++++++++++ tests/unit/plugins/database/ModelTest.php | 34 +++++++++++++ .../plugins/database/SluggableModelTest.php | 51 +++++++++++++++++++ .../unit/system/classes/PluginManagerTest.php | 14 +++-- 11 files changed, 313 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/plugins/database/tester/Plugin.php create mode 100644 tests/fixtures/plugins/database/tester/models/Author.php create mode 100644 tests/fixtures/plugins/database/tester/models/Post.php create mode 100644 tests/fixtures/plugins/database/tester/models/SluggablePost.php create mode 100644 tests/fixtures/plugins/database/tester/updates/create_authors_table.php create mode 100644 tests/fixtures/plugins/database/tester/updates/create_posts_table.php create mode 100644 tests/fixtures/plugins/database/tester/updates/version.yaml create mode 100644 tests/unit/plugins/database/BelongsToModelTest.php create mode 100644 tests/unit/plugins/database/ModelTest.php create mode 100644 tests/unit/plugins/database/SluggableModelTest.php diff --git a/tests/fixtures/plugins/database/tester/Plugin.php b/tests/fixtures/plugins/database/tester/Plugin.php new file mode 100644 index 000000000..4cad4adf7 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/Plugin.php @@ -0,0 +1,17 @@ + 'Database Tester Plugin', + 'description' => 'Plugin for loading tests that involve the database.', + 'author' => 'Alexey Bobkov, Samuel Georges' + ]; + } + +} \ No newline at end of file diff --git a/tests/fixtures/plugins/database/tester/models/Author.php b/tests/fixtures/plugins/database/tester/models/Author.php new file mode 100644 index 000000000..b89d5c34e --- /dev/null +++ b/tests/fixtures/plugins/database/tester/models/Author.php @@ -0,0 +1,28 @@ + 'Database\Tester\Models\Post', + ]; + +} \ No newline at end of file diff --git a/tests/fixtures/plugins/database/tester/models/Post.php b/tests/fixtures/plugins/database/tester/models/Post.php new file mode 100644 index 000000000..5cdedc7f8 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/models/Post.php @@ -0,0 +1,33 @@ + 'Database\Tester\Models\Author', + ]; + +} \ No newline at end of file diff --git a/tests/fixtures/plugins/database/tester/models/SluggablePost.php b/tests/fixtures/plugins/database/tester/models/SluggablePost.php new file mode 100644 index 000000000..86ed609d6 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/models/SluggablePost.php @@ -0,0 +1,30 @@ + 'title' + ]; + +} \ No newline at end of file diff --git a/tests/fixtures/plugins/database/tester/updates/create_authors_table.php b/tests/fixtures/plugins/database/tester/updates/create_authors_table.php new file mode 100644 index 000000000..ccc15e383 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/updates/create_authors_table.php @@ -0,0 +1,26 @@ +engine = 'InnoDB'; + $table->increments('id'); + $table->string('name')->nullable(); + $table->string('email')->nullable(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('database_tester_authors'); + } + +} diff --git a/tests/fixtures/plugins/database/tester/updates/create_posts_table.php b/tests/fixtures/plugins/database/tester/updates/create_posts_table.php new file mode 100644 index 000000000..37dcd6ca7 --- /dev/null +++ b/tests/fixtures/plugins/database/tester/updates/create_posts_table.php @@ -0,0 +1,30 @@ +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'); + } + +} diff --git a/tests/fixtures/plugins/database/tester/updates/version.yaml b/tests/fixtures/plugins/database/tester/updates/version.yaml new file mode 100644 index 000000000..519509f0c --- /dev/null +++ b/tests/fixtures/plugins/database/tester/updates/version.yaml @@ -0,0 +1,5 @@ +1.0.1: First version of Tester +1.0.2: + - Create tables + - create_posts_table.php + - create_authors_table.php diff --git a/tests/unit/plugins/database/BelongsToModelTest.php b/tests/unit/plugins/database/BelongsToModelTest.php new file mode 100644 index 000000000..06886e7c5 --- /dev/null +++ b/tests/unit/plugins/database/BelongsToModelTest.php @@ -0,0 +1,49 @@ +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); + } +} \ No newline at end of file diff --git a/tests/unit/plugins/database/ModelTest.php b/tests/unit/plugins/database/ModelTest.php new file mode 100644 index 000000000..4dd6bc8fc --- /dev/null +++ b/tests/unit/plugins/database/ModelTest.php @@ -0,0 +1,34 @@ +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']); + } +} \ No newline at end of file diff --git a/tests/unit/plugins/database/SluggableModelTest.php b/tests/unit/plugins/database/SluggableModelTest.php new file mode 100644 index 000000000..0518ae75f --- /dev/null +++ b/tests/unit/plugins/database/SluggableModelTest.php @@ -0,0 +1,51 @@ +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); + } +} \ No newline at end of file diff --git a/tests/unit/system/classes/PluginManagerTest.php b/tests/unit/system/classes/PluginManagerTest.php index 4720e2ce0..c4f610f44 100644 --- a/tests/unit/system/classes/PluginManagerTest.php +++ b/tests/unit/system/classes/PluginManagerTest.php @@ -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()