From 23fd19ad1ebe71560d387b50c9f8e49dac3b7bc6 Mon Sep 17 00:00:00 2001 From: Alex Goodwin Date: Mon, 18 Jul 2016 23:49:45 +1000 Subject: [PATCH 1/5] Fix stock test cases --- app/tests/ExampleTest.php | 14 -------------- app/tests/TestCase.php | 5 +++++ 2 files changed, 5 insertions(+), 14 deletions(-) delete mode 100644 app/tests/ExampleTest.php diff --git a/app/tests/ExampleTest.php b/app/tests/ExampleTest.php deleted file mode 100644 index 67ba067..0000000 --- a/app/tests/ExampleTest.php +++ /dev/null @@ -1,14 +0,0 @@ -client->request('GET', '/'); - $this->assertTrue($this->client->getResponse()->isOk()); - } -} \ No newline at end of file diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php index 38c2567..0f932e3 100644 --- a/app/tests/TestCase.php +++ b/app/tests/TestCase.php @@ -11,4 +11,9 @@ public function createApplication() $testEnvironment = 'testing'; return require __DIR__.'/../../bootstrap/start.php'; } + + public function testTrivial() + { + $this->assertTrue(true); + } } \ No newline at end of file From 9b5385c19b8b8b25fc782d0d008c039c6deb36fd Mon Sep 17 00:00:00 2001 From: Alex Goodwin Date: Mon, 18 Jul 2016 23:51:17 +1000 Subject: [PATCH 2/5] Upgrade php unit and clean up mock objects in tests --- app/tests/TestCase.php | 5 +++++ composer.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php index 0f932e3..bbab1a3 100644 --- a/app/tests/TestCase.php +++ b/app/tests/TestCase.php @@ -16,4 +16,9 @@ public function testTrivial() { $this->assertTrue(true); } + + public function tearDown() + { + Mockery::close(); + } } \ No newline at end of file diff --git a/composer.json b/composer.json index 129d0be..1dacf27 100755 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "kmd/logviewer": "1.1.*" }, "require-dev": { - "phpunit/phpunit": "3.7.*", + "phpunit/phpunit": "4.8.*", "mockery/mockery": "0.9.*" }, "autoload": { From ac3bdaffec73c0cb2f860a6471577fe566ed325d Mon Sep 17 00:00:00 2001 From: Alex Goodwin Date: Mon, 18 Jul 2016 23:52:26 +1000 Subject: [PATCH 3/5] Hook up in-memory DB for testing. This diff renders database as used in automated test suite independent of configured production database connection - you can freely run tests without affecting your production database. I tested the above by turning off my local MySQL server - test suite did not complain and ran exactly as it had before I turned my local server off. I am aiming to enable more aggressive and comprehensive automated testing, at unit, integration and functional levels. For example, assuming Laravel's functionality is up to it, it now should be possible to write a functional test that creates a new user (possibly with some problems) and verify that the new user gets created cleanly or not at all.. --- app/config/testing/database.php | 14 ++++++++++++++ app/tests/TestCase.php | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 app/config/testing/database.php diff --git a/app/config/testing/database.php b/app/config/testing/database.php new file mode 100644 index 0000000..b15c97c --- /dev/null +++ b/app/config/testing/database.php @@ -0,0 +1,14 @@ + 'sqlite', + + 'connections' => array( + 'sqlite' => array( + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '' + ), + ) +); \ No newline at end of file diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php index bbab1a3..41a83a0 100644 --- a/app/tests/TestCase.php +++ b/app/tests/TestCase.php @@ -17,8 +17,32 @@ public function testTrivial() $this->assertTrue(true); } + /** + * Default preparation for each test + * + */ + public function setUp() + { + parent::setUp(); // Don't forget this! + + $this->prepareForTests(); + } + + /** + * Migrates the database and set the mailer to 'pretend'. + * This will cause the tests to run quickly. + * + */ + private function prepareForTests() + { + Artisan::call('migrate'); + Artisan::call('db:seed'); + Mail::pretend(true); + } + public function tearDown() { + parent::TearDown(); Mockery::close(); } } \ No newline at end of file From fe49660311afdcbdd5b4f28cef4cf9d97a34350d Mon Sep 17 00:00:00 2001 From: Alex Goodwin Date: Tue, 19 Jul 2016 19:07:50 +1000 Subject: [PATCH 4/5] Update Install controller test to reflect use of Laravel DB gubbins --- app/tests/InstallControllerTest.php | 5 ++--- app/tests/TestCase.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/tests/InstallControllerTest.php b/app/tests/InstallControllerTest.php index 5d3e677..4bfe2bc 100644 --- a/app/tests/InstallControllerTest.php +++ b/app/tests/InstallControllerTest.php @@ -53,9 +53,8 @@ public function testPostDatabaseSuperGut() ]; //Mock it... yeah, mock it.. - DB::shouldReceive('unprepared')->withAnyArgs()->andReturnNull()->once(); - Artisan::shouldReceive('migrate')->withAnyArgs()->andReturnNull()->once(); - Artisan::shouldReceive('db:seed')->withAnyArgs()->andReturnNull()->once(); + Artisan::shouldReceive('call')->withArgs(['migrate'])->andReturnNull()->once(); + Artisan::shouldReceive('call')->withArgs(['db:seed'])->andReturnNull()->once(); $externalMock = \Mockery::mock('overload:October\Rain\Config\Rewrite'); $externalMock->shouldReceive('toFile')->withArgs([$expectedPath, $expectedFeed])->andReturnNull()->once(); diff --git a/app/tests/TestCase.php b/app/tests/TestCase.php index 41a83a0..e6c28a0 100644 --- a/app/tests/TestCase.php +++ b/app/tests/TestCase.php @@ -43,6 +43,6 @@ private function prepareForTests() public function tearDown() { parent::TearDown(); - Mockery::close(); + \Mockery::close(); } } \ No newline at end of file From 4bd6ee2cbb4687de5df403f1d8bcafc175c20e3f Mon Sep 17 00:00:00 2001 From: Alex Goodwin Date: Tue, 19 Jul 2016 19:17:39 +1000 Subject: [PATCH 5/5] Add example of join using Laravel DB gubbins --- app/tests/JoinTest.php | 48 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/tests/JoinTest.php diff --git a/app/tests/JoinTest.php b/app/tests/JoinTest.php new file mode 100644 index 0000000..d10f948 --- /dev/null +++ b/app/tests/JoinTest.php @@ -0,0 +1,48 @@ +getId(); + + // set up sample users + $aliceCredentials = array( + 'first_name' => 'Alice', + 'last_name' => 'Example', + 'email' => 'alice@example.com', + 'password' => 'bruceschneier', + 'activated' => true, + ); + $bobCredentials = array( + 'first_name' => 'Bob', + 'last_name' => 'Demonstration', + 'email' => 'bob@demonstration.net', + 'password' => 'oursharedsecret', + 'activated' => true, + ); + + $alice = Sentry::createUser($aliceCredentials); + $bob = Sentry::createUser($bobCredentials); + + $alice->addGroup($group); + $bob->addGroup($group); + + //now try joining alice and bob on admin group + $result = DB::table('users') + ->join('users_groups', 'users.id', '=', 'users_groups.user_id') + ->select('users.id', 'users_groups.group_id') + ->where('users.id', '=', $alice->id) + ->orWhere('users.id', '=', $bob->id) + ->orderBy('users.id') + ->get(); + + //if we've joined correctly, retrieved group ID for both cases should be admin group's ID, + //since that was only thing retrieved + $this->assertEquals(2, sizeof($result)); + $this->assertEquals($groupID, $result[0]->group_id); + $this->assertEquals($groupID, $result[1]->group_id); + } +} \ No newline at end of file