From ddea4dd5737ef6353909eb1bb36dff50fcdc9aa4 Mon Sep 17 00:00:00 2001 From: Jake Mitchell Date: Sat, 25 Jun 2016 22:10:21 +0000 Subject: [PATCH] added tests --- README.md | 8 +++----- composer.json | 3 +-- phpunit.php | 20 ++++++++++++++++++++ src/Meta.php | 2 +- tests/MetaTest.php | 26 ++++++++++++++++++++++---- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 6deda6f..f30a5f0 100644 --- a/README.md +++ b/README.md @@ -71,24 +71,22 @@ class CreatePostsTable extends Migration } ``` -Then, when you create a new model, a Post in this case, your function would look something like this: +Then, just create a model like usual: ``` php public function createPost () { $post = Post::create([ // post data ]); - - $post->setUuid(); } ``` -You now have access to a universal identifier for every model in the entire project. +But now, you have access to a universal identifier for every model in the entire project. You can check if a model has a UUID, and get it if it does: ``` php $post->hasUuid(); // returns true or false -$post->getUuid(); // returns the UUID if it exists, else null +$post->getUuid(); // returns the UUID ``` The other big feature of the Meta model is saving meta information. diff --git a/composer.json b/composer.json index 7e45d01..ed81ce7 100644 --- a/composer.json +++ b/composer.json @@ -9,11 +9,10 @@ } ], "require": { - "php": ">=5.6.4", "illuminate/support": "^5.2", "illuminate/database": "^5.2", "ramsey/uuid": "^3.4", - "glmdev/foundation": "^0.0.004" + "glmdev/foundation": "^0.0.10" }, "require-dev": { "phpunit/phpunit": "~4.0" diff --git a/phpunit.php b/phpunit.php index 50765d6..8e6aba8 100644 --- a/phpunit.php +++ b/phpunit.php @@ -1,5 +1,25 @@ addConnection([ + 'driver' => 'sqlite', + 'database' => ':memory:', +]); +$capsule->setAsGlobal(); +$capsule->bootEloquent(); +$capsule->schema()->dropIfExists('metas'); +$capsule->schema()->create('metas', function (Blueprint $table) { + $table->timestamps(); + Meta::formTable($table); +}); \ No newline at end of file diff --git a/src/Meta.php b/src/Meta.php index fb946bf..5f2e06f 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -27,7 +27,7 @@ class Meta extends Model implements FoundationModel, MetaContract */ public static function create( array $args = [] ){ $args['meta'] = (isset($args['meta'])) ? $args['meta'] : serialize([]); - $args['uuid'] = (isset($args['uuid'])) ? $args['uuid'] : ''; + $args['uuid'] = (isset($args['uuid'])) ? $args['uuid'] : Uuid::uuid4(); return parent::create($args); } diff --git a/tests/MetaTest.php b/tests/MetaTest.php index 7b88928..1e67d58 100644 --- a/tests/MetaTest.php +++ b/tests/MetaTest.php @@ -3,13 +3,31 @@ namespace Glmdev\Meta\Tests; use Glmdev\Meta\Meta; +use Ramsey\Uuid\Uuid; class MetaTest extends \PHPUnit_Framework_TestCase { - // TODO: write tests - - public function testItReturnsTrue () + public function testReadWrite () { - $this->assertTrue(true); + $model = Meta::create(); + + $model->write('name', 'John'); + $model->write('age', 15); + $model->write('email', 'Johnny@appleseed.com'); + + $this->assertEquals('John', $model->read('name')); + $this->assertEquals(15, $model->read('age')); + $this->assertEquals('Johnny@appleseed.com', $model->read('email')); + } + + public function testUuid () + { + $model = Meta::create(); + $uuid = Uuid::uuid4(); + + $this->assertTrue($model->hasUuid()); + + $model->setRawUuid($uuid); + $this->assertEquals($uuid, $model->getUuid()); } }