mirror of
				https://github.com/elyby/accounts.git
				synced 2025-05-31 14:11:46 +05:30 
			
		
		
		
	Логика генерации значения первичного ключа для строк вынесена в поведение
This commit is contained in:
		@@ -0,0 +1,70 @@
 | 
			
		||||
<?php
 | 
			
		||||
namespace codeception\common\unit\behaviors;
 | 
			
		||||
 | 
			
		||||
use Codeception\Specify;
 | 
			
		||||
use common\behaviors\PrimaryKeyValueBehavior;
 | 
			
		||||
use tests\codeception\api\unit\TestCase;
 | 
			
		||||
use yii\db\ActiveRecord;
 | 
			
		||||
 | 
			
		||||
class PrimaryKeyValueBehaviorTest extends TestCase {
 | 
			
		||||
    use Specify;
 | 
			
		||||
 | 
			
		||||
    public function testSetPrimaryKeyValue() {
 | 
			
		||||
        $this->specify('method should generate value for primary key field on call', function() {
 | 
			
		||||
            $model = new DummyModel();
 | 
			
		||||
            /** @var PrimaryKeyValueBehavior|\PHPUnit_Framework_MockObject_MockObject $behavior */
 | 
			
		||||
            $behavior = $this->getMockBuilder(PrimaryKeyValueBehavior::class)
 | 
			
		||||
                 ->setMethods(['isValueExists'])
 | 
			
		||||
                 ->setConstructorArgs([[
 | 
			
		||||
                     'value' => function() {
 | 
			
		||||
                         return 'mock';
 | 
			
		||||
                     },
 | 
			
		||||
                 ]])
 | 
			
		||||
                 ->getMock();
 | 
			
		||||
 | 
			
		||||
            $behavior->expects($this->once())
 | 
			
		||||
                     ->method('isValueExists')
 | 
			
		||||
                     ->will($this->returnValue(false));
 | 
			
		||||
 | 
			
		||||
            $model->attachBehavior('primary-key-value-behavior', $behavior);
 | 
			
		||||
            $behavior->setPrimaryKeyValue();
 | 
			
		||||
            expect($model->id)->equals('mock');
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $this->specify('method should repeat value generation if generated value duplicate with exists', function() {
 | 
			
		||||
            $model = new DummyModel();
 | 
			
		||||
            /** @var PrimaryKeyValueBehavior|\PHPUnit_Framework_MockObject_MockObject $behavior */
 | 
			
		||||
            $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))
 | 
			
		||||
                  ->method('generateValue')
 | 
			
		||||
                  ->will($this->onConsecutiveCalls('1', '2', '3'));
 | 
			
		||||
 | 
			
		||||
            $behavior->expects($this->exactly(3))
 | 
			
		||||
                  ->method('isValueExists')
 | 
			
		||||
                  ->will($this->onConsecutiveCalls(true, true, false));
 | 
			
		||||
 | 
			
		||||
            $model->attachBehavior('primary-key-value-behavior', $behavior);
 | 
			
		||||
            $behavior->setPrimaryKeyValue();
 | 
			
		||||
            expect($model->id)->equals('3');
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class DummyModel extends ActiveRecord {
 | 
			
		||||
 | 
			
		||||
    public $id;
 | 
			
		||||
 | 
			
		||||
    public static function primaryKey() {
 | 
			
		||||
        return ['id'];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -28,30 +28,4 @@ class EmailActivationTest extends DbTestCase {
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function testBeforeSave() {
 | 
			
		||||
        $this->specify('method should generate value for key field if it empty', function() {
 | 
			
		||||
            $model = new EmailActivation();
 | 
			
		||||
            $model->beforeSave(true);
 | 
			
		||||
            expect($model->key)->notNull();
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        $this->specify('method should repeat code generation if code duplicate with exists', function() {
 | 
			
		||||
            /** @var EmailActivation|\PHPUnit_Framework_MockObject_MockObject $model */
 | 
			
		||||
            $model = $this->getMockBuilder(EmailActivation::class)
 | 
			
		||||
                ->setMethods(['generateKey', 'isKeyExists'])
 | 
			
		||||
                ->getMock();
 | 
			
		||||
 | 
			
		||||
            $model->expects($this->exactly(3))
 | 
			
		||||
                ->method('generateKey')
 | 
			
		||||
                ->will($this->onConsecutiveCalls('1', '2', '3'));
 | 
			
		||||
 | 
			
		||||
            $model->expects($this->exactly(3))
 | 
			
		||||
                  ->method('isKeyExists')
 | 
			
		||||
                  ->will($this->onConsecutiveCalls(true, true, false));
 | 
			
		||||
 | 
			
		||||
            $model->beforeSave(true);
 | 
			
		||||
            expect($model->key)->equals('3');
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user