Skip to content

Commit

Permalink
Some model functionality unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
martywallace committed Nov 30, 2017
1 parent 2e81650 commit 8175c1d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Tempest/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ public static function getNonPrimaryFields() {
});
}

/**
* Retrieve all fields with no index associated with it.
*
* @return SealedField[]
*/
public static function getNonIndexedFields() {
return array_filter(static::getFields(), function(SealedField $field) {
return count($field->getIndexes()) === 0;
});
}

/**
* Retrieve the auto-incrementing field, if this model declared one.
*
Expand Down
1 change: 1 addition & 0 deletions src/Tempest/Database/Models/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @property int $id The session ID.
* @property Carbon $created The time that the session was created.
* @property Carbon $updated The time that the session was last updated.
* @property string $ip The IP address of the session creator.
* @property mixed $data The data stored in the session.
*
* @author Marty Wallace
Expand Down
19 changes: 19 additions & 0 deletions tests/Material/ExampleModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php namespace Tempest\Tests\Material;

use Tempest\Database\Field;
use Tempest\Database\Model;

class ExampleModel extends Model {

protected static function fields() {
return [
'id' => Field::int()->setAutoIncrements(),
'first' => Field::string(),
'last' => Field::string(),
'age' => Field::int()->setDefault(18),
'email' => Field::string()->addUniqueKey('identity'),
'mobile' => Field::string()->addUniqueKey('identity')
];
}

}
64 changes: 64 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace Tempest\Tests;

use PHPUnit\Framework\TestCase;
use Tempest\Tests\Material\ExampleModel;

class ModelTest extends TestCase {

public function testCreation() {
$this->assertInstanceOf(ExampleModel::class, ExampleModel::create());
}

public function testCreationWithValues() {
$this->assertEquals('Marty', ExampleModel::create(['first' => 'Marty'])->getRaw('first'));
}

public function testDefaultValues() {
$this->assertEquals(18, ExampleModel::create()->age);
}

public function testGetterAccess() {
$model = ExampleModel::create([
'first' => 'Marty',
'last' => 'Wallace'
]);

$this->assertEquals('Marty', $model->first);
$this->assertEquals('Wallace', $model->last);
}

public function testSetValues() {
$model = ExampleModel::create();

$model->setFieldValue('first', 'Marty');
$model->last = 'Wallace';

$this->assertEquals('Marty', $model->first);
$this->assertEquals('Wallace', $model->last);
}

public function testFill() {
$model = ExampleModel::create()->fill([
'email' => '[email protected]',
'first' => 'Marty'
]);

$this->assertEquals('[email protected]', $model->email);
$this->assertEquals('Marty', $model->first);
$this->assertEquals(18, $model->age);
$this->assertNull($model->last);
}

public function testGetPrimaryKey() {
$this->assertEquals('id', ExampleModel::getPrimaryFields()[0]->getName());
}

public function testGetUniqueIndexWithMultipleFields() {
$this->assertCount(2, ExampleModel::getIndexByName('identity')->getFields());
}

public function testGetNonIndexedFields() {
$this->assertCount(3, ExampleModel::getNonIndexedFields());
}

}

0 comments on commit 8175c1d

Please sign in to comment.