From 08f1dc50f519344e5a86552e93c35920238b0e89 Mon Sep 17 00:00:00 2001 From: James Judd Date: Wed, 18 Apr 2018 15:55:57 +0100 Subject: [PATCH] adds orchestra and first tests to project --- composer.json | 8 +++++ phpunit.xml | 29 +++++++++++++++ tests/Models/User.php | 20 +++++++++++ tests/RevisionTest.php | 82 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 phpunit.xml create mode 100644 tests/Models/User.php create mode 100644 tests/RevisionTest.php diff --git a/composer.json b/composer.json index 52677d68..dc58ed00 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,13 @@ "psr-0": { "Venturecraft\\Revisionable": "src/" } + }, + "autoload-dev": { + "psr-4": { + "Venturecraft\\Revisionable\\Tests\\": "tests/" + } + }, + "require-dev": { + "orchestra/testbench": "~3.0" } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..96d96447 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,29 @@ + + + + + + ./tests/ + + + + + + src/ + + + + + + + \ No newline at end of file diff --git a/tests/Models/User.php b/tests/Models/User.php new file mode 100644 index 00000000..1bbc835d --- /dev/null +++ b/tests/Models/User.php @@ -0,0 +1,20 @@ +loadLaravelMigrations(['--database' => 'testing']); + + // call migrations specific to our tests, e.g. to seed the db + // the path option should be an absolute path. + $this->loadMigrationsFrom([ + '--database' => 'testing', + '--path' => realpath(__DIR__.'/../src/migrations'), + ]); + } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function getEnvironmentSetUp($app) + { + // Setup default database to use sqlite :memory: + $app['config']->set('database.default', 'testbench'); + $app['config']->set('database.connections.testbench', array( + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + )); + } + + /** + * Test we can interact with the database + */ + public function testUsersTable() + { + User::create([ + 'name' => 'James Judd', + 'email' => 'james.judd@revisionable.test', + 'password' => \Hash::make('456'), + ]); + + $users = User::findOrFail(1); + $this->assertEquals('james.judd@revisionable.test', $users->email); + $this->assertTrue(\Hash::check('456', $users->password)); + } + + /** + * Make sure revisions are created + */ + public function testRevisionsStored() + { + $user = User::create([ + 'name' => 'James Judd', + 'email' => 'james.judd@revisionable.test', + 'password' => \Hash::make('456'), + ]); + + // change to my nickname + $user->update([ + 'name' => 'Judd' + ]); + + // change to my forename + $user->update([ + 'name' => 'James' + ]); + + // we should have two revisions to my name + $this->assertCount(2, $user->revisionHistory); + } +}