Skip to content

Unit testing

Romans Malinovskis edited this page Apr 18, 2016 · 1 revision

When it comes to unit-testing, we must be able to test the models by detaching persistence layer. We have built an integrated methods to test your models easily (something something SRP)

Here is your typical test-script:

$db = new atk4\data\db\Mock();
$db->expect('load', 1, ['name'=>'John', 'type'=>'client']);
$db->expect('loadBy', [['type','=','admin']], []);
$db->expect('save', ['name'=>'Peter', 'type'=>'admin']);

$u = $db->add('Model_User')->load(1);
$this->assertEquals('John', $u['name']);

$a = $db->add('Model_Admin')->tryLoadAny();
$this->assertEquals(false, $a->loaded());

$a['name']='Peter';
$a->save();

You can also use Array persistence model:

$db = new atk4\data\db\Array([
  'user'=>[1=>['name'=>'John', 'type'=>'client']],
]);

$u = $db->add('Model_User')->load(1);
$this->assertEquals('John', $u['name']);

$a = $db->add('Model_Admin')->tryLoadAny();
$this->assertEquals(false, $a->loaded());

$a['name']='Peter';
$a->save();

$this->assertEquals([
  'user'=>[1=>['name'=>'John', 'type'=>'client'], 2=>['name'=>'Peter', 'type'=>'admin'],
], $db->data);

Next: Aggregation and Reports