2016-08-21 03:52:14 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\behaviors;
|
2016-08-21 03:52:14 +05:30
|
|
|
|
|
|
|
use Codeception\Specify;
|
|
|
|
use common\behaviors\PrimaryKeyValueBehavior;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\unit\TestCase;
|
2016-08-21 03:52:14 +05:30
|
|
|
use yii\db\ActiveRecord;
|
|
|
|
|
|
|
|
class PrimaryKeyValueBehaviorTest extends TestCase {
|
|
|
|
use Specify;
|
|
|
|
|
2016-08-21 04:51:39 +05:30
|
|
|
public function testRefreshPrimaryKeyValue() {
|
2016-08-21 03:52:14 +05:30
|
|
|
$this->specify('method should generate value for primary key field on call', function() {
|
|
|
|
$model = new DummyModel();
|
2019-05-14 04:28:29 +05:30
|
|
|
/** @var PrimaryKeyValueBehavior|\PHPUnit\Framework\MockObject\MockObject $behavior */
|
2016-08-21 03:52:14 +05:30
|
|
|
$behavior = $this->getMockBuilder(PrimaryKeyValueBehavior::class)
|
2018-04-18 02:17:25 +05:30
|
|
|
->setMethods(['isValueExists'])
|
|
|
|
->setConstructorArgs([[
|
|
|
|
'value' => function() {
|
|
|
|
return 'mock';
|
|
|
|
},
|
|
|
|
]])
|
|
|
|
->getMock();
|
2016-08-21 03:52:14 +05:30
|
|
|
|
|
|
|
$behavior->expects($this->once())
|
2018-04-18 02:17:25 +05:30
|
|
|
->method('isValueExists')
|
|
|
|
->will($this->returnValue(false));
|
2016-08-21 03:52:14 +05:30
|
|
|
|
|
|
|
$model->attachBehavior('primary-key-value-behavior', $behavior);
|
|
|
|
$behavior->setPrimaryKeyValue();
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame('mock', $model->id);
|
2016-08-21 03:52:14 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
$this->specify('method should repeat value generation if generated value duplicate with exists', function() {
|
|
|
|
$model = new DummyModel();
|
2019-05-14 04:28:29 +05:30
|
|
|
/** @var PrimaryKeyValueBehavior|\PHPUnit\Framework\MockObject\MockObject $behavior */
|
2016-08-21 03:52:14 +05:30
|
|
|
$behavior = $this->getMockBuilder(PrimaryKeyValueBehavior::class)
|
|
|
|
->setMethods(['isValueExists', 'generateValue'])
|
|
|
|
->setConstructorArgs([[
|
|
|
|
'value' => function() {
|
|
|
|
return 'this was mocked, but let be passed';
|
|
|
|
},
|
|
|
|
]])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$behavior->expects($this->exactly(3))
|
2018-04-18 02:17:25 +05:30
|
|
|
->method('generateValue')
|
|
|
|
->will($this->onConsecutiveCalls('1', '2', '3'));
|
2016-08-21 03:52:14 +05:30
|
|
|
|
|
|
|
$behavior->expects($this->exactly(3))
|
2018-04-18 02:17:25 +05:30
|
|
|
->method('isValueExists')
|
|
|
|
->will($this->onConsecutiveCalls(true, true, false));
|
2016-08-21 03:52:14 +05:30
|
|
|
|
|
|
|
$model->attachBehavior('primary-key-value-behavior', $behavior);
|
|
|
|
$behavior->setPrimaryKeyValue();
|
2019-05-14 04:28:29 +05:30
|
|
|
$this->assertSame('3', $model->id);
|
2016-08-21 03:52:14 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class DummyModel extends ActiveRecord {
|
|
|
|
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
public static function primaryKey() {
|
|
|
|
return ['id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|