Skip to content

Commit 96a36d5

Browse files
committed
add save without changes test
1 parent 417e04e commit 96a36d5

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

EmbedsManyBehavior.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ public function getStorage()
3535
{
3636
if (empty($this->_storage)) {
3737
$this->_storage = new Storage();
38-
foreach ((array)$this->owner->{$this->attribute} as $attributes) {
39-
$model = $this->createEmbedded($attributes, false);
38+
39+
$attributes = (array)$this->owner->{$this->attribute};
40+
if (empty($attributes) && in_array($this->owner->scenario, $this->initEmptyScenarios)) {
41+
$attributes[] = [];
42+
}
43+
44+
foreach ($attributes as $modelAttributes) {
45+
$model = $this->createEmbedded($modelAttributes, false);
4046
$model->setFormName($this->getFormName($this->_storage->getNextIndex()));
41-
$this->_storage[] = $this->createEmbedded($attributes, false);
47+
$this->_storage[] = $model;
4248
}
4349
}
4450
return $this->_storage;

tests/unit.suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
# suite for unit (internal) tests.
44
class_name: UnitTester
55
modules:
6-
enabled: [Asserts, UnitHelper]
6+
enabled: [Asserts]

tests/unit/EmbedsOneBehaviorTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
use consultnn\embedded\tests\models\SlaveEmbeddedClass;
44

55

6-
class EmbedsOneBehaviorTest extends \Codeception\TestCase\Test
6+
class EmbedsTest extends \Codeception\TestCase\Test
77
{
88
/**
99
* @var \UnitTester
1010
*/
1111
protected $tester;
1212
/**
1313
*
14-
* @var Company
14+
* @var MasterTestClass
1515
*/
1616
protected $company;
1717

@@ -20,6 +20,27 @@ protected function _before()
2020
$this->company = new MasterTestClass();
2121
$this->company->deleteAll();
2222
}
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+
}
2344

2445
/**
2546
* @dataProvider testSaveDataProvider

0 commit comments

Comments
 (0)