├── .gitignore ├── AbstractEmbeddedBehavior.php ├── EmbeddedDocument.php ├── EmbedsManyBehavior.php ├── EmbedsOneBehavior.php ├── README.md ├── Storage.php ├── StorageInterface.php ├── codeception.yml ├── composer.json └── tests ├── .gitignore ├── _bootstrap.php ├── _config.php ├── models ├── MasterTestClass.php └── SlaveEmbeddedClass.php ├── unit.suite.yml └── unit ├── EmbedsOneBehaviorTest.php ├── UnitTester.php └── _bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.lock 3 | composer.phar 4 | .idea -------------------------------------------------------------------------------- /AbstractEmbeddedBehavior.php: -------------------------------------------------------------------------------- 1 | 'validate', 52 | ActiveRecord::EVENT_BEFORE_INSERT => 'proxy', 53 | ActiveRecord::EVENT_BEFORE_UPDATE => 'proxy' 54 | ]; 55 | 56 | /** 57 | * @var mixed 58 | */ 59 | protected $_storage; 60 | 61 | /** 62 | * set $_storage property and return it 63 | * @return mixed 64 | */ 65 | abstract public function getStorage(); 66 | 67 | /** 68 | * Set attributes to storage 69 | * @param array $attributes 70 | * @param bool $safeOnly 71 | */ 72 | abstract protected function setAttributes($attributes, $safeOnly = true); 73 | 74 | /** 75 | * Return storage attributes. 76 | * 77 | * Возвращает данные для сохранения в бд. 78 | * @return mixed 79 | */ 80 | abstract protected function getAttributes(); 81 | 82 | public function events() 83 | { 84 | return $this->events; 85 | } 86 | 87 | /** 88 | * @inheritdoc 89 | */ 90 | public function __get($name) 91 | { 92 | if ($this->checkName($name)) { 93 | return $this->storage; 94 | } 95 | 96 | return parent::__get($name); 97 | } 98 | 99 | public function __set($name, $value) 100 | { 101 | if ($this->checkName($name)) { 102 | $this->setAttributes($value); 103 | } else { 104 | parent::__set($name, $value); 105 | } 106 | } 107 | 108 | /** 109 | * @return string {@link $fakeAttribute} 110 | */ 111 | public function getFakeAttribute() 112 | { 113 | return substr($this->attribute, 1); 114 | } 115 | 116 | /** 117 | * Trigger owner event in storage 118 | * @param \yii\base\Event $event 119 | */ 120 | public function proxy($event) 121 | { 122 | $this->storage->setScenario($this->owner->scenario); 123 | $this->storage->trigger($event->name, $event); 124 | $this->owner->{$this->attribute} = $this->getAttributes(); 125 | } 126 | 127 | /** 128 | * Validate storage 129 | */ 130 | public function validate() 131 | { 132 | if ($this->owner->isAttributeSafe($this->fakeAttribute)) { 133 | $this->storage->setScenario($this->owner->scenario); 134 | if (!$this->storage->validate()) { 135 | $this->owner->addError( 136 | $this->fakeAttribute, 137 | Yii::t('yii', '{attribute} is invalid.', ['attribute' => $this->owner->getAttributeLabel($this->fakeAttribute)]) 138 | ); 139 | } 140 | } 141 | } 142 | 143 | /** 144 | * @inheritdoc 145 | */ 146 | public function canSetProperty($name, $checkVars = true) 147 | { 148 | return $this->checkName($name) || parent::canSetProperty($name, $checkVars); 149 | } 150 | 151 | /** 152 | * @inheritdoc 153 | */ 154 | public function canGetProperty($name, $checkVars = true) 155 | { 156 | return $this->checkName($name) || parent::canGetProperty($name, $checkVars); 157 | } 158 | 159 | /** 160 | * Check attribute name for getter 161 | * @param $name 162 | * @return bool 163 | */ 164 | protected function checkName($name) 165 | { 166 | return $this->getFakeAttribute() == $name; 167 | } 168 | 169 | protected function createEmbedded($attributes, $safeOnly = true, $config = []) 170 | { 171 | if (is_array($this->embedded)) { 172 | $embeddedConfig = $this->embedded; 173 | } else { 174 | $embeddedConfig = ['class' => $this->embedded]; 175 | } 176 | $embeddedConfig = array_merge($embeddedConfig, $config); 177 | /** @var EmbeddedDocument $model */ 178 | $model = \Yii::createObject($embeddedConfig); 179 | $model->scenario = $this->owner->scenario; 180 | $model->setAttributes($attributes, $safeOnly); 181 | return $model; 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /EmbeddedDocument.php: -------------------------------------------------------------------------------- 1 | _formName)) { 40 | return $this->_formName; 41 | } else { 42 | return parent::formName(); 43 | } 44 | } 45 | 46 | /** 47 | * @param $formName 48 | */ 49 | #[\ReturnTypeWillChange] 50 | public function setFormName($formName) 51 | { 52 | if (!empty($formName)) { 53 | $this->_formName = $formName; 54 | } 55 | } 56 | 57 | /** 58 | * set link to primary model 59 | * @param ActiveRecord $model 60 | */ 61 | #[\ReturnTypeWillChange] 62 | public function setPrimaryModel(ActiveRecord $model) 63 | { 64 | $this->_primaryModel = $model; 65 | } 66 | 67 | /** 68 | * get link to primary model 69 | * @return ActiveRecord 70 | * @throws UnknownPropertyException 71 | */ 72 | #[\ReturnTypeWillChange] 73 | public function getPrimaryModel() 74 | { 75 | if (!isset($this->_primaryModel)) { 76 | throw new UnknownPropertyException('primary model is not set'); 77 | } 78 | return $this->_primaryModel; 79 | } 80 | 81 | 82 | /** 83 | * set link to primary model attribute 84 | * @param $value 85 | */ 86 | #[\ReturnTypeWillChange] 87 | public function setSource($value) 88 | { 89 | $this->_source = $value; 90 | } 91 | 92 | /** 93 | * Save embedded model as attribute on primary model 94 | * @throws UnknownPropertyException 95 | */ 96 | #[\ReturnTypeWillChange] 97 | public function save() 98 | { 99 | if (!isset($this->_source) || !$this->primaryModel->hasAttribute($this->_source)) { 100 | throw new UnknownPropertyException('source attribute is not set or not exists'); 101 | } 102 | $this->primaryModel->save(false, [$this->_source]); 103 | } 104 | 105 | #[\ReturnTypeWillChange] 106 | public function setScenario($scenario) 107 | { 108 | if (array_key_exists($scenario, $this->scenarios())) { 109 | parent::setScenario($scenario); 110 | } 111 | } 112 | 113 | /** 114 | * Checks if embedded model is empty. 115 | * Doesn't take into account validator's "when" and "isEmpty" parameters. 116 | * 117 | * Проверяет объект на пустоту. 118 | * Пустым считается объект все атрибуты которого пусты (empty($value)) или равны значениям по-умолчанию. Не учитывает параметры "when" и "isEmpty" валидаторов "по-умолчанию". 119 | * @return bool 120 | */ 121 | #[\ReturnTypeWillChange] 122 | public function isEmpty() 123 | { 124 | $notEmptyAttributes = []; 125 | foreach ($this->attributes() as $attribute) { 126 | if (!empty($this->$attribute)) { 127 | $notEmptyAttributes[$attribute] = $attribute; 128 | } 129 | } 130 | 131 | foreach ($this->getActiveValidators() as $validator) { 132 | if (($validator instanceof DefaultValueValidator) && ($checkAttributes = array_intersect($validator->attributes, $notEmptyAttributes))) { 133 | /** @var \yii\validators\DefaultValueValidator $validator */ 134 | foreach ($checkAttributes as $attribute) { 135 | if ($this->$attribute == $validator->value) { 136 | unset($notEmptyAttributes[$attribute]); 137 | } 138 | } 139 | } 140 | } 141 | return empty($notEmptyAttributes); 142 | } 143 | } 144 | -------------------------------------------------------------------------------- /EmbedsManyBehavior.php: -------------------------------------------------------------------------------- 1 | setFormName) { 22 | return Html::getInputName($this->owner, $this->fakeAttribute."[{$index}]"); 23 | } 24 | 25 | return null; 26 | } 27 | 28 | /** 29 | * @inheritdoc 30 | */ 31 | #[\ReturnTypeWillChange] 32 | protected function setAttributes($attributes, $safeOnly = true) 33 | { 34 | $this->storage->removeAll(); 35 | 36 | if (empty($attributes)) { 37 | return; 38 | } 39 | 40 | foreach ($attributes as $modelAttributes) { 41 | $model = $this->createEmbedded( 42 | $modelAttributes, 43 | $safeOnly, 44 | ['formName' => $this->getFormName($this->storage->getNextIndex())] 45 | ); 46 | 47 | if ($this->saveEmpty || !$model->isEmpty()) { 48 | $this->storage[] = $model; 49 | } 50 | } 51 | } 52 | 53 | /** 54 | * @inheritdoc 55 | */ 56 | #[\ReturnTypeWillChange] 57 | protected function getAttributes() 58 | { 59 | return $this->storage->attributes; 60 | } 61 | 62 | /** 63 | * @return Storage 64 | */ 65 | #[\ReturnTypeWillChange] 66 | public function getStorage() 67 | { 68 | if (empty($this->_storage)) { 69 | $this->_storage = new Storage(); 70 | 71 | $attributes = (array)$this->owner->{$this->attribute}; 72 | if (empty($attributes) && in_array($this->owner->scenario, $this->initEmptyScenarios)) { 73 | $attributes[] = []; 74 | } 75 | 76 | foreach ($attributes as $modelAttributes) { 77 | $model = $this->createEmbedded( 78 | $modelAttributes, 79 | false, 80 | ['formName' => $this->getFormName($this->_storage->getNextIndex())] 81 | ); 82 | $this->_storage[] = $model; 83 | } 84 | } 85 | return $this->_storage; 86 | } 87 | } -------------------------------------------------------------------------------- /EmbedsOneBehavior.php: -------------------------------------------------------------------------------- 1 | storage->scenario = $this->owner->scenario; 18 | $this->storage->setAttributes($attributes, $safeOnly); 19 | } 20 | 21 | /** 22 | * @inheritdoc 23 | */ 24 | #[\ReturnTypeWillChange] 25 | protected function getAttributes() 26 | { 27 | if ($this->saveEmpty || !$this->storage->isEmpty()) { 28 | return $this->storage->attributes; 29 | } 30 | 31 | return null; 32 | } 33 | 34 | /** 35 | * @return EmbeddedDocument 36 | */ 37 | #[\ReturnTypeWillChange] 38 | public function getStorage() 39 | { 40 | if (empty($this->_storage)) { 41 | $this->_storage = $this->createEmbedded( 42 | (array)$this->owner->{$this->attribute}, 43 | false, 44 | ['formName' => $this->setFormName ? Html::getInputName($this->owner, $this->fakeAttribute) : null] 45 | ); 46 | } 47 | return $this->_storage; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii2 behaviors implement handling of mongodb embedded documents 2 | =============================================================== 3 | 4 | * Add attribute with name starting with underscore to model. 5 | ~~~ 6 | /** 7 | * @inheritdoc 8 | */ 9 | public function attributes() 10 | { 11 | return [ 12 | '_address', 13 | '_phones', 14 | ] 15 | } 16 | ~~~ 17 | * Add "safe" validation rule for attribute without underscore in name. 18 | ~~~ 19 | /** 20 | * @inheritdoc 21 | */ 22 | public function rules() 23 | { 24 | return [ 25 | [['address', 'phones'], 'safe'], 26 | ] 27 | } 28 | ~~~ 29 | * Add behavior with attribute name with underscore in name 30 | ~~~ 31 | 'address' => [ 32 | 'class' => EmbedsOneBehavior::className(), 33 | 'attribute' => '_address', 34 | 'embedded' => Address::className() 35 | ], 36 | 'phones' => [ 37 | 'class' => EmbedsManyBehavior::className(), 38 | 'attribute' => '_phones', 39 | 'embedded' => Phone::className() 40 | ], 41 | ~~~ 42 | * Your embedded documents must be inherited from [EmbeddedDocument](EmbeddedDocument.php) class. 43 | ~~~ 44 | class Phone extends EmbeddedDocument 45 | { 46 | public $number; 47 | public $type; 48 | 49 | public function rules() 50 | { 51 | return [ 52 | [['number', 'type'], 'string'], 53 | [['type'], 'in', 'range' => ['home', 'work']], 54 | ]; 55 | } 56 | } 57 | ~~~ 58 | * To create empty embedded document set base document's scenario to the value listed in initEmptyScenarios parameter of EmbedsManyBehavior 59 | ~~~ 60 | 'address' => [ 61 | 'class' => EmbedsOneBehavior::className(), 62 | 'attribute' => '_address', 63 | 'initEmptyScenarios' => ['create', 'update'], 64 | 'embedded' => Address::className() 65 | ], 66 | ~~~ 67 | * Use attribute without underscore in form or view 68 | ~~~ 69 | // Address 70 | echo $form->field($company->address, 'detail'); 71 | echo $form->field($company->address, 'id')->hiddenInput(); 72 | 73 | // Phones 74 | foreach($company->phones as $key => $phone) { 75 | echo $form->field($phone, 'number'); 76 | echo $form->field($phone, 'type'); 77 | } 78 | ~~~ 79 | -------------------------------------------------------------------------------- /Storage.php: -------------------------------------------------------------------------------- 1 | _container = []; 24 | $this->rewind(); 25 | } 26 | 27 | /** 28 | * @param string $name 29 | * @param Event $event 30 | */ 31 | #[\ReturnTypeWillChange] 32 | public function trigger($name, Event $event = null) 33 | { 34 | parent::trigger($name, $event); 35 | foreach ($this->_container as $model) { 36 | /** @var EmbeddedDocument $model */ 37 | $model->trigger($name, $event); 38 | } 39 | } 40 | 41 | /** 42 | * @inheritdoc 43 | */ 44 | #[\ReturnTypeWillChange] 45 | public function validate() 46 | { 47 | $hasError = false; 48 | foreach ($this->_container as $model) { 49 | /** @var EmbeddedDocument $model */ 50 | if (!$model->validate()) { 51 | $hasError = true; 52 | } 53 | } 54 | return !$hasError; 55 | } 56 | 57 | /** 58 | * @inheritdoc 59 | */ 60 | #[\ReturnTypeWillChange] 61 | public function getAttributes() 62 | { 63 | $attributes = []; 64 | foreach ($this->_container as $model) { 65 | /** @var EmbeddedDocument $model */ 66 | $attributes[] = $model->getAttributes(); 67 | } 68 | return $attributes; 69 | } 70 | 71 | /** 72 | * Get embedded model by condition 73 | * @param $condition 74 | * @return null| EmbeddedDocument 75 | */ 76 | #[\ReturnTypeWillChange] 77 | public function get($condition) 78 | { 79 | list($attribute, $value) = $condition; 80 | 81 | foreach ($this->_container as $object) { 82 | if ($object->$attribute == $value) { 83 | return $object; 84 | } 85 | } 86 | return null; 87 | } 88 | 89 | /** 90 | * Set embedded model by condition 91 | * @param $condition 92 | * @param $object 93 | * @return bool 94 | */ 95 | #[\ReturnTypeWillChange] 96 | public function set($condition, $object) 97 | { 98 | list($attribute, $value) = $condition; 99 | 100 | foreach ($this->_container as $key => $needleObject) { 101 | if ($needleObject->$attribute == $value) { 102 | $this->offsetSet($key, $object); 103 | return true; 104 | } 105 | } 106 | return false; 107 | } 108 | 109 | #[\ReturnTypeWillChange] 110 | public function sort($field) 111 | { 112 | $attributes = $this->attributes; 113 | if (empty($attributes) || !in_array($field, array_keys(reset($attributes)))) { 114 | return; 115 | } 116 | usort($this->_container, function ($item1, $item2) use ($field) { 117 | return $item1[$field] <=> $item2[$field]; 118 | }); 119 | } 120 | 121 | #[\ReturnTypeWillChange] 122 | public function offsetSet($offset, $model) 123 | { 124 | if (is_null($offset)) { 125 | $offset = $this->getNextIndex(); 126 | } 127 | 128 | $this->_container[$offset] = $model; 129 | } 130 | 131 | #[\ReturnTypeWillChange] 132 | public function current() 133 | { 134 | if ($this->valid($this->_cursor)) { 135 | return $this->_container[$this->_cursor]; 136 | } else { 137 | return null; 138 | } 139 | } 140 | 141 | #[\ReturnTypeWillChange] 142 | public function key() 143 | { 144 | return $this->_cursor; 145 | } 146 | 147 | #[\ReturnTypeWillChange] 148 | public function next() 149 | { 150 | ++$this->_cursor; 151 | } 152 | 153 | #[\ReturnTypeWillChange] 154 | public function rewind() 155 | { 156 | $this->_cursor = 0; 157 | } 158 | 159 | #[\ReturnTypeWillChange] 160 | public function valid() 161 | { 162 | return $this->offsetExists($this->_cursor); 163 | } 164 | 165 | #[\ReturnTypeWillChange] 166 | public function offsetExists($offset) 167 | { 168 | return isset($this->_container[$offset]); 169 | } 170 | 171 | #[\ReturnTypeWillChange] 172 | public function offsetUnset($offset) 173 | { 174 | unset($this->_container[$offset]); 175 | $this->_container = array_values($this->_container); 176 | } 177 | 178 | #[\ReturnTypeWillChange] 179 | public function offsetGet($offset) 180 | { 181 | return isset($this->_container[$offset]) ? $this->_container[$offset] : null; 182 | } 183 | 184 | #[\ReturnTypeWillChange] 185 | public function count() 186 | { 187 | return count($this->_container); 188 | } 189 | 190 | #[\ReturnTypeWillChange] 191 | public function getNextIndex() 192 | { 193 | $count = count($this->_container); 194 | return $count ? $count : 0; 195 | } 196 | 197 | /** 198 | * Set scenario to embedded model 199 | * @param string $scenario 200 | * @return void 201 | */ 202 | #[\ReturnTypeWillChange] 203 | public function setScenario($scenario) 204 | { 205 | foreach ($this->_container as $model) { 206 | /** @var EmbeddedDocument $model */ 207 | $model->setScenario($scenario); 208 | } 209 | } 210 | 211 | #[\ReturnTypeWillChange] 212 | public function __toString() 213 | { 214 | return ''; 215 | } 216 | } -------------------------------------------------------------------------------- /StorageInterface.php: -------------------------------------------------------------------------------- 1 | 'test', 7 | 'basePath' => '', 8 | 'components' => [ 9 | 'mailer' => [ 10 | 'useFileTransport' => true, 11 | ], 12 | 'urlManager' => [ 13 | 'showScriptName' => true, 14 | ], 15 | 'mongodb' => [ 16 | 'class' => 'yii\mongodb\Connection', 17 | 'dsn' => 'mongodb://localhost:27018/mongodb_embedded_test', 18 | ], 19 | ], 20 | ]; 21 | -------------------------------------------------------------------------------- /tests/models/MasterTestClass.php: -------------------------------------------------------------------------------- 1 | [ 16 | 'class' => EmbedsOneBehavior::className(), 17 | 'attribute' => '_one', 18 | 'embedded' => SlaveEmbeddedClass::className() 19 | ], 20 | 'many' => [ 21 | 'class' => EmbedsManyBehavior::className(), 22 | 'attribute' => '_many', 23 | 'initEmptyScenarios' => ['create', 'update'], 24 | 'embedded' => SlaveEmbeddedClass::className() 25 | ], 26 | ]; 27 | } 28 | 29 | /** 30 | * @inheritdoc 31 | */ 32 | public function attributes() 33 | { 34 | return [ 35 | '_id', 36 | '_one', 37 | '_many' 38 | ]; 39 | } 40 | 41 | /** 42 | * @inheritdoc 43 | */ 44 | public function rules() 45 | { 46 | return [ 47 | [['_id', 'one', 'many'], 'safe', 'on' => ['default', 'requiredName', 'requiredValue']] 48 | ]; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/models/SlaveEmbeddedClass.php: -------------------------------------------------------------------------------- 1 | 'requiredName'], 26 | ['value', 'boolean', 'on' => 'requiredValue'], 27 | [['name', 'value'], 'safe', 'on'=>'default'] 28 | ]; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/unit.suite.yml: -------------------------------------------------------------------------------- 1 | # Codeception Test Suite Configuration 2 | 3 | # suite for unit (internal) tests. 4 | class_name: UnitTester 5 | modules: 6 | enabled: [Asserts] 7 | -------------------------------------------------------------------------------- /tests/unit/EmbedsOneBehaviorTest.php: -------------------------------------------------------------------------------- 1 | company = new MasterTestClass(); 21 | $this->company->deleteAll(); 22 | } 23 | 24 | public function testSaveNotChanged() 25 | { 26 | $data = ['_id' => 1, 27 | '_one' => ['name'=>1, 'value' => 1], 28 | '_many' => [ 29 | ['name'=>1, 'value' => 1], 30 | ['name'=>1, 'value' => 1], 31 | ['name'=>1, 'value' => 1], 32 | ['name'=>1, 'value' => 1], 33 | ['name'=>1, 'value' => 1], 34 | ['name'=>1, 'value' => 1], 35 | ['name'=>1, 'value' => 1] 36 | ] 37 | ]; 38 | $this->company->setAttributes($data, false); 39 | $this->company->save(); 40 | $this->assertEquals($this->company->toArray(), $data); 41 | $this->company->save(); 42 | $this->assertEquals($this->company->toArray(), $data); 43 | } 44 | 45 | /** 46 | * @dataProvider testSaveDataProvider 47 | */ 48 | public function testSave($inputParams, $saveParams) 49 | { 50 | $this->company->setAttributes($inputParams); 51 | $this->company->load($saveParams, ''); 52 | $this->company->save(); 53 | $this->assertEquals($this->company->one->name, $saveParams['one']['name']); 54 | $this->assertEquals($this->company->_id, $saveParams['_id']); 55 | $this->assertEquals($this->company->many[0]->name, $saveParams['many'][0]['value']); 56 | } 57 | /** 58 | * @dataProvider testCompanyDataProvider 59 | */ 60 | public function testSetValidateAttribute($arr, $isValidValue, $isValidName) 61 | { 62 | $this->company->setAttributes($arr); 63 | $this->company->setScenario('requiredValue'); 64 | $this->assertEquals($this->company->validate(), $isValidValue); 65 | $this->company->setScenario('requiredName'); 66 | $this->assertEquals($this->company->validate(), $isValidName); 67 | } 68 | 69 | /** 70 | * @dataProvider testStorageDataProvider 71 | */ 72 | public function testSetGetStorage($data, $condition, $insert, $insertCondition) 73 | { 74 | $this->company->setAttributes($data); 75 | $branch = new SlaveEmbeddedClass(); 76 | $branch->setAttributes(['name' => $insert[0], 'value' => $insert[1]]); 77 | $this->company->many->set($condition, $branch); 78 | $this->assertEquals($this->company->many->get($insertCondition)->name, $insert[0]); 79 | $this->assertEquals($this->company->many->get($insertCondition)->value, $insert[1]); 80 | } 81 | 82 | public function testEmptyScenario() 83 | { 84 | $obj = new MasterTestClass(); 85 | $obj->setScenario('create'); 86 | $this->assertEquals($obj->many->count(), 1); 87 | $obj2 = new MasterTestClass(); 88 | $obj2->setScenario('update'); 89 | $this->assertEquals($obj2->many->count(), 1); 90 | } 91 | 92 | public function testCompanyDataProvider() 93 | { 94 | return [ 95 | [ 96 | 'data' => ['_id' => 1, 97 | 'one' => ['name'=>1, 'value' => 1], 98 | 'many' => [ 99 | ['name'=>1, 'value' => 1], 100 | ['name'=>1, 'value' => 1], 101 | ['name'=>1, 'value' => 1], 102 | ['name'=>1, 'value' => 1], 103 | ['name'=>1, 'value' => 1], 104 | ['name'=>1, 'value' => 1], 105 | ['name'=>1, 'value' => 1] 106 | ] 107 | ], 108 | 'isValidValue' => true, 109 | 'isValidName' => true 110 | ], 111 | [ 112 | 'data' => ['_id' => 2, 113 | 'one' => ['name'=>2, 'value' => 547], 114 | 'many' => [ 115 | ['name'=>2, 'value' => false], 116 | ['name'=>2, 'value' => true], 117 | ['name'=>2, 'value' => true], 118 | ['name'=>2, 'value' => false], 119 | ['name'=>2, 'value' => false], 120 | ['name'=>2, 'value' => false] 121 | ] 122 | ], 123 | 'isValidValue' => false, 124 | 'isValidName' => true 125 | ], 126 | [ 127 | 'data' => ['_id' => 3, 128 | 'one' => ['name'=>3, 'value' => false], 129 | 'many' => [ 130 | ['name'=>3, 'value' => false], 131 | ['name'=>'foo', 'value' => false], 132 | ['name'=>3, 'value' => true], 133 | ['name'=>3, 'value' => false], 134 | ['name'=>3, 'value' => true] 135 | ] 136 | ], 137 | 'isValidValue' => true, 138 | 'isValidName' => false 139 | ], 140 | [ 141 | 'data' => ['_id' => 4, 142 | 'one' => ['name'=>4, 'value' => 4], 143 | 'many' => [ 144 | ['name'=>4, 'value' => 4], 145 | ['name'=>'foo', 'value' => 4], 146 | ['name'=>4, 'value' => 4], 147 | ['name'=>4, 'value' => 4] 148 | ] 149 | ], 150 | 'isValidValue' => false, 151 | 'isValidName' => false 152 | ], 153 | ]; 154 | } 155 | 156 | public function testSaveDataProvider() 157 | { 158 | return [ 159 | [ 160 | 'baseData' => ['_id' => 1, 'one' => ['name'=>1, 'value' => 1], 'many' => [['name'=>1, 'value' => 1],['name'=>1, 'value' => 1]]], 161 | 'saveData' => ['_id' => 1, 'one' => ['name'=>7, 'value' => 7], 'many' => [['name'=>7, 'value' => 7]]] 162 | ], 163 | [ 164 | 'baseData' => ['_id' => 2, 'one' => ['name'=>2, 'value' => 2], 'many' => [['name'=>2, 'value' => 2],['name'=>2, 'value' => 2]]], 165 | 'saveData' => ['_id' => 2, 'one' => ['name'=>6, 'value' => 6], 'many' => [['name'=>6, 'value' => 6],['name'=>6, 'value' => 6]]] 166 | ], 167 | [ 168 | 'baseData' => ['_id' => 3, 'one' => ['name'=>3, 'value' => 3], 'many' => [['name'=>3, 'value' => 3],['name'=>3, 'value' => 3]]], 169 | 'saveData' => ['_id' => 3, 'one' => ['name'=>5, 'value' => 5], 'many' => [['name'=>5, 'value' => 5],['name'=>5, 'value' => 5]]] 170 | ], 171 | [ 172 | 'baseData' => ['_id' => 4, 'one' => ['name'=>4, 'value' => 4], 'many' => [['name'=>4, 'value' => 4],['name'=>4, 'value' => 4]]], 173 | 'saveData' => ['_id' => 4, 'one' => ['name'=>4, 'value' => 4], 'many' => [['name'=>4, 'value' => 4],['name'=>4, 'value' => 4]]] 174 | ], 175 | [ 176 | 'baseData' => ['_id' => 5, 'one' => ['name'=>5, 'value' => 5], 'many' => [['name'=>5, 'value' => 5],['name'=>5, 'value' => 5]]], 177 | 'saveData' => ['_id' => 5, 'one' => ['name'=>3, 'value' => 3], 'many' => [['name'=>3, 'value' => 3],['name'=>3, 'value' => 3]]] 178 | ], 179 | [ 180 | 'baseData' => ['_id' => 6, 'one' => ['name'=>6, 'value' => 6], 'many' => [['name'=>6, 'value' => 6],['name'=>6, 'value' => 6]]], 181 | 'saveData' => ['_id' => 6, 'one' => ['name'=>2, 'value' => 2], 'many' => [['name'=>2, 'value' => 2],['name'=>2, 'value' => 2]]] 182 | ], 183 | [ 184 | 'baseData' => ['_id' => 7, 'one' => ['name'=>7, 'value' => 7], 'many' => [['name'=>7, 'value' => 7]]], 185 | 'saveData' => ['_id' => 7, 'one' => ['name'=>1, 'value' => 1], 'many' => [['name'=>1, 'value' => 1],['name'=>1, 'value' => 1]]] 186 | ], 187 | ]; 188 | } 189 | 190 | public function testStorageDataProvider() 191 | { 192 | return [ 193 | 194 | [ 195 | 'initData' => ['_id' => 2, 'one' => ['name'=>3, 'value' => 2], 'many' => [['name'=>4, 'value' => 2],['name'=>'foo', 'value' => 2],['name'=>5, 'value' => 2],['name'=>2, 'value' => 2],['name'=>2, 'value' => 2],['name'=>2, 'value' => 2]]], 196 | 'insertCondition' => ['value', 2], 197 | 'insertData' => [999, 'foo'], 198 | 'selectData' => ['name', 999] 199 | ], 200 | [ 201 | 'initData' => ['_id' => 3, 'one' => ['name'=>3, 'value' => 3], 'many' => [['name'=>3, 'value' => 3],['name'=>'foo', 'value' => 3],['name'=>3, 'value' => 3],['name'=>3, 'value' => 3],['name'=>3, 'value' => 3]]], 202 | 'insertCondition' => ['name', 'foo'], 203 | 'insertData' => ['abc', 'gfr'], 204 | 'selectData' => ['value', 'gfr'] 205 | ], 206 | ]; 207 | } 208 | } -------------------------------------------------------------------------------- /tests/unit/UnitTester.php: -------------------------------------------------------------------------------- 1 | scenario->runStep(new \Codeception\Step\Action('assertEquals', func_get_args())); 43 | } 44 | 45 | 46 | /** 47 | * [!] Method is generated. Documentation taken from corresponding module. 48 | * 49 | * Checks that two variables are not equal 50 | * 51 | * @param $expected 52 | * @param $actual 53 | * @param string $message 54 | * @see \Codeception\Module\Asserts::assertNotEquals() 55 | */ 56 | public function assertNotEquals($expected, $actual, $message = null) { 57 | return $this->scenario->runStep(new \Codeception\Step\Action('assertNotEquals', func_get_args())); 58 | } 59 | 60 | 61 | /** 62 | * [!] Method is generated. Documentation taken from corresponding module. 63 | * 64 | * Checks that two variables are same 65 | * 66 | * @param $expected 67 | * @param $actual 68 | * @param string $message 69 | * 70 | * @return mixed 71 | * @see \Codeception\Module\Asserts::assertSame() 72 | */ 73 | public function assertSame($expected, $actual, $message = null) { 74 | return $this->scenario->runStep(new \Codeception\Step\Action('assertSame', func_get_args())); 75 | } 76 | 77 | 78 | /** 79 | * [!] Method is generated. Documentation taken from corresponding module. 80 | * 81 | * Checks that two variables are not same 82 | * 83 | * @param $expected 84 | * @param $actual 85 | * @param string $message 86 | * @see \Codeception\Module\Asserts::assertNotSame() 87 | */ 88 | public function assertNotSame($expected, $actual, $message = null) { 89 | return $this->scenario->runStep(new \Codeception\Step\Action('assertNotSame', func_get_args())); 90 | } 91 | 92 | 93 | /** 94 | * [!] Method is generated. Documentation taken from corresponding module. 95 | * 96 | * Checks that expected is greater than actual 97 | * 98 | * @param $expected 99 | * @param $actual 100 | * @param string $message 101 | * @see \Codeception\Module\Asserts::assertGreaterThan() 102 | */ 103 | public function assertGreaterThan($expected, $actual, $message = null) { 104 | return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThan', func_get_args())); 105 | } 106 | 107 | 108 | /** 109 | * [!] Method is generated. Documentation taken from corresponding module. 110 | * 111 | * @deprecated 112 | * @see \Codeception\Module\Asserts::assertGreaterThen() 113 | */ 114 | public function assertGreaterThen($expected, $actual, $message = null) { 115 | return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThen', func_get_args())); 116 | } 117 | 118 | 119 | /** 120 | * [!] Method is generated. Documentation taken from corresponding module. 121 | * 122 | * Checks that expected is greater or equal than actual 123 | * 124 | * @param $expected 125 | * @param $actual 126 | * @param string $message 127 | * @see \Codeception\Module\Asserts::assertGreaterThanOrEqual() 128 | */ 129 | public function assertGreaterThanOrEqual($expected, $actual, $message = null) { 130 | return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThanOrEqual', func_get_args())); 131 | } 132 | 133 | 134 | /** 135 | * [!] Method is generated. Documentation taken from corresponding module. 136 | * 137 | * @deprecated 138 | * @see \Codeception\Module\Asserts::assertGreaterThenOrEqual() 139 | */ 140 | public function assertGreaterThenOrEqual($expected, $actual, $message = null) { 141 | return $this->scenario->runStep(new \Codeception\Step\Action('assertGreaterThenOrEqual', func_get_args())); 142 | } 143 | 144 | 145 | /** 146 | * [!] Method is generated. Documentation taken from corresponding module. 147 | * 148 | * Checks that expected is less than actual 149 | * 150 | * @param $expected 151 | * @param $actual 152 | * @param string $message 153 | * @see \Codeception\Module\Asserts::assertLessThan() 154 | */ 155 | public function assertLessThan($expected, $actual, $message = null) { 156 | return $this->scenario->runStep(new \Codeception\Step\Action('assertLessThan', func_get_args())); 157 | } 158 | 159 | 160 | /** 161 | * [!] Method is generated. Documentation taken from corresponding module. 162 | * 163 | * Checks that expected is less or equal than actual 164 | * 165 | * @param $expected 166 | * @param $actual 167 | * @param string $message 168 | * @see \Codeception\Module\Asserts::assertLessThanOrEqual() 169 | */ 170 | public function assertLessThanOrEqual($expected, $actual, $message = null) { 171 | return $this->scenario->runStep(new \Codeception\Step\Action('assertLessThanOrEqual', func_get_args())); 172 | } 173 | 174 | 175 | /** 176 | * [!] Method is generated. Documentation taken from corresponding module. 177 | * 178 | * Checks that haystack contains needle 179 | * 180 | * @param $needle 181 | * @param $haystack 182 | * @param string $message 183 | * @see \Codeception\Module\Asserts::assertContains() 184 | */ 185 | public function assertContains($needle, $haystack, $message = null) { 186 | return $this->scenario->runStep(new \Codeception\Step\Action('assertContains', func_get_args())); 187 | } 188 | 189 | 190 | /** 191 | * [!] Method is generated. Documentation taken from corresponding module. 192 | * 193 | * Checks that haystack doesn't contain needle. 194 | * 195 | * @param $needle 196 | * @param $haystack 197 | * @param string $message 198 | * @see \Codeception\Module\Asserts::assertNotContains() 199 | */ 200 | public function assertNotContains($needle, $haystack, $message = null) { 201 | return $this->scenario->runStep(new \Codeception\Step\Action('assertNotContains', func_get_args())); 202 | } 203 | 204 | 205 | /** 206 | * [!] Method is generated. Documentation taken from corresponding module. 207 | * 208 | * Checks that variable is empty. 209 | * 210 | * @param $actual 211 | * @param string $message 212 | * @see \Codeception\Module\Asserts::assertEmpty() 213 | */ 214 | public function assertEmpty($actual, $message = null) { 215 | return $this->scenario->runStep(new \Codeception\Step\Action('assertEmpty', func_get_args())); 216 | } 217 | 218 | 219 | /** 220 | * [!] Method is generated. Documentation taken from corresponding module. 221 | * 222 | * Checks that variable is not empty. 223 | * 224 | * @param $actual 225 | * @param string $message 226 | * @see \Codeception\Module\Asserts::assertNotEmpty() 227 | */ 228 | public function assertNotEmpty($actual, $message = null) { 229 | return $this->scenario->runStep(new \Codeception\Step\Action('assertNotEmpty', func_get_args())); 230 | } 231 | 232 | 233 | /** 234 | * [!] Method is generated. Documentation taken from corresponding module. 235 | * 236 | * Checks that variable is NULL 237 | * 238 | * @param $actual 239 | * @param string $message 240 | * @see \Codeception\Module\Asserts::assertNull() 241 | */ 242 | public function assertNull($actual, $message = null) { 243 | return $this->scenario->runStep(new \Codeception\Step\Action('assertNull', func_get_args())); 244 | } 245 | 246 | 247 | /** 248 | * [!] Method is generated. Documentation taken from corresponding module. 249 | * 250 | * Checks that variable is not NULL 251 | * 252 | * @param $actual 253 | * @param string $message 254 | * @see \Codeception\Module\Asserts::assertNotNull() 255 | */ 256 | public function assertNotNull($actual, $message = null) { 257 | return $this->scenario->runStep(new \Codeception\Step\Action('assertNotNull', func_get_args())); 258 | } 259 | 260 | 261 | /** 262 | * [!] Method is generated. Documentation taken from corresponding module. 263 | * 264 | * Checks that condition is positive. 265 | * 266 | * @param $condition 267 | * @param string $message 268 | * @see \Codeception\Module\Asserts::assertTrue() 269 | */ 270 | public function assertTrue($condition, $message = null) { 271 | return $this->scenario->runStep(new \Codeception\Step\Action('assertTrue', func_get_args())); 272 | } 273 | 274 | 275 | /** 276 | * [!] Method is generated. Documentation taken from corresponding module. 277 | * 278 | * Checks that condition is negative. 279 | * 280 | * @param $condition 281 | * @param string $message 282 | * @see \Codeception\Module\Asserts::assertFalse() 283 | */ 284 | public function assertFalse($condition, $message = null) { 285 | return $this->scenario->runStep(new \Codeception\Step\Action('assertFalse', func_get_args())); 286 | } 287 | 288 | 289 | /** 290 | * [!] Method is generated. Documentation taken from corresponding module. 291 | * 292 | * Fails the test with message. 293 | * 294 | * @param $message 295 | * @see \Codeception\Module\Asserts::fail() 296 | */ 297 | public function fail($message) { 298 | return $this->scenario->runStep(new \Codeception\Step\Action('fail', func_get_args())); 299 | } 300 | } 301 | -------------------------------------------------------------------------------- /tests/unit/_bootstrap.php: -------------------------------------------------------------------------------- 1 |