diff --git a/composer.json b/composer.json index db6213d7..2dfd15d4 100644 --- a/composer.json +++ b/composer.json @@ -26,5 +26,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); + } +}