Skip to content

Commit

Permalink
added proper schema statement
Browse files Browse the repository at this point in the history
  • Loading branch information
alsofronie committed Dec 14, 2015
1 parent 34b904c commit 7e995a9
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions tests/EloquentUuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\DB;

class EloquentUuidTest extends PHPUnit_Framework_TestCase {

/**
* Tests the creation of model with uuid as primary key
*
* @return void
*/
public function testCreation() {

// EloquentUserModel::unguard();

$creation = EloquentUserModel::create([
'username'=>'alsofronie',
'password'=>'secret'
]);
class EloquentUuidTest extends PHPUnit_Framework_TestCase
{
/**
* Tests the creation of model with uuid as primary key
*
* @return void
*/
public function testCreation()
{
// EloquentUserModel::unguard();
$creation = EloquentUserModel::create([
'username'=>'alsofronie',
'password'=>'secret'
]);

$this->assertEquals(36, strlen($creation->id));

$model = EloquentUserModel::first();
$model = EloquentUserModel::first();

$this->assertEquals(36, strlen($model->id));
$this->assertRegExp('/^[0-9a-f-]{36}$/',$model->id);
$this->assertEquals(36, strlen($model->id));
$this->assertRegExp('/^[0-9a-f-]{36}$/', $model->id);
$this->assertRegExp('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/', $model->id);

$this->assertEquals($creation->id, $model->id);

// EloquentuserModel::guard();
// EloquentuserModel::guard();

}
}

public function test32Creation() {
public function test32Creation()
{
$creation = Eloquent32UserModel::create([
'username'=>'alsofronie',
'password'=>'secret'
Expand All @@ -48,14 +48,14 @@ public function test32Creation() {
$model = Eloquent32UserModel::first();

$this->assertEquals(32, strlen($model->id));
$this->assertRegExp('/^[0-9a-f]{32}$/',$model->id);
$this->assertRegExp('/^[0-9a-f]{32}$/', $model->id);
$this->assertRegExp('/^[0-9a-f]{32}$/', $model->id);

$this->assertEquals($creation->id, $model->id);
$this->assertEquals($creation->id, $model->id);
}

public function testBinaryCreation() {

public function testBinaryCreation()
{
$creation = EloquentBinUserModel::create([
'username'=>'alsofronie-binary',
'password'=>'secret'
Expand All @@ -79,7 +79,7 @@ public function testBinaryCreation() {



/**
/**
* Bootstrap Eloquent.
*
* @return void
Expand Down Expand Up @@ -114,34 +114,30 @@ public static function tearDownAfterClass()
public function setUp()
{
$this->schema()->create('users', function ($table) {
$table->char('id',36);
$table->char('id', 36);
$table->string('username');
$table->string('password');
$table->timestamps();
$table->primary('id');
});

$this->schema()->create('users32', function($table) {
$table->char('id',36);
$this->schema()->create('users32', function ($table) {
$table->char('id', 36); // this is not a mistake, we need to be sure the field is not stripped down by the DB
$table->string('username');
$table->string('password');
$table->timestamps();
$table->primary('id');
$table->primary('id');
});

$this->schema()->create('usersb', function($table) {
$this->schema()->create('usersb', function ($table) {
$table->string('username');
$table->string('password');
$table->timestamps();
});

// unfortunately, we need to do this:
// DB::statement (...)

// This is not a mistake, We are testing if the binary we're save it's actually 16 bytes and
// not being cut by DBS

$this->connection()->statement('ALTER TABLE usersb ADD COLUMN id BINARY(18)');
$this->connection()->statement('ALTER TABLE `usersb` ADD `id` BINARY(16); ALTER TABLE `usersb` ADD PRIMARY KEY (`id`);');

}

Expand All @@ -157,7 +153,7 @@ public function tearDown()
$this->schema()->drop('usersb');
}

/**
/**
* Helpers...
*/

Expand All @@ -184,23 +180,24 @@ protected function schema()



class EloquentUserModel extends Eloquent {

use UuidModelTrait;
protected $table = 'users';

protected $guarded = [];
class EloquentUserModel extends Eloquent
{
use UuidModelTrait;
protected $table = 'users';

protected $guarded = [];
}

class Eloquent32UserModel extends Eloquent {
class Eloquent32UserModel extends Eloquent
{
use Uuid32ModelTrait;
protected $table = 'users32';

protected $guarded = [];
}

class EloquentBinUserModel extends Eloquent {
class EloquentBinUserModel extends Eloquent
{
use UuidBinaryModelTrait;
protected $table = 'usersb';

Expand Down Expand Up @@ -230,4 +227,3 @@ public function setDefaultConnection($name)
//
}
}

0 comments on commit 7e995a9

Please sign in to comment.