Skip to content

Commit

Permalink
Merge pull request #311 from Juddling/master
Browse files Browse the repository at this point in the history
Adds orchestra testing setup
  • Loading branch information
duellsy committed Aug 9, 2018
2 parents 62fd3fe + 08f1dc5 commit 1e751ee
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
8 changes: 8 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@
"psr-0": {
"Venturecraft\\Revisionable": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Venturecraft\\Revisionable\\Tests\\": "tests/"
}
},
"require-dev": {
"orchestra/testbench": "~3.0"
}
}
29 changes: 29 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
>

<testsuites>
<testsuite name="Testbench Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

<php>
<env name="APP_ENV" value="testing"/>
</php>
</phpunit>
20 changes: 20 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Venturecraft\Revisionable\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Venturecraft\Revisionable\RevisionableTrait;

/**
* Add a revisionable model for testing purposes
* I've chosen User, purely because the migration will already exist
*
* Class User
* @package Venturecraft\Revisionable\Tests\Models
*/
class User extends Model
{
use RevisionableTrait;

protected $guarded = [];
}
82 changes: 82 additions & 0 deletions tests/RevisionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Venturecraft\Revisionable\Tests;

use Venturecraft\Revisionable\Tests\Models\User;

class RevisionTest extends \Orchestra\Testbench\TestCase
{
/**
* Setup the test environment.
*/
protected function setUp()
{
parent::setUp();
$this->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);
}
}

0 comments on commit 1e751ee

Please sign in to comment.