added tests

This commit is contained in:
Jake Mitchell 2016-06-25 22:10:21 +00:00
parent ea3ec0bacc
commit ddea4dd573
5 changed files with 47 additions and 12 deletions

View File

@ -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.

View File

@ -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"

View File

@ -1,5 +1,25 @@
<?php
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
use Glmdev\Meta\Meta;
require __DIR__ . '/vendor/autoload.php';
date_default_timezone_set('UTC');
/*
* Prepare the db connection (spoofing that shit)
*/
$capsule = new Capsule;
$capsule->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);
});

View File

@ -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);
}

View File

@ -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());
}
}