Skip to content

Commit c99a54f

Browse files
committed
Assuming that deleteAll statement inside _unlink is always OK if no exceptions occurred. Removing unnecessary test
1 parent 9a853a1 commit c99a54f

File tree

2 files changed

+16
-107
lines changed

2 files changed

+16
-107
lines changed

src/ORM/Association/HasMany.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ public function saveAssociated(EntityInterface $entity, array $options = [])
148148
$original = $targetEntities;
149149
$options['_sourceTable'] = $this->source();
150150

151+
$unlinkSuccessful = null;
151152
if ($this->_saveStrategy === self::SAVE_REPLACE) {
152153
$unlinkSuccessful = $this->_unlinkAssociated($properties, $entity, $target, $targetEntities);
153154
}
154155

155-
if (isset($unlinkSuccessful) && !$unlinkSuccessful) {
156+
if ($unlinkSuccessful === false) {
156157
return false;
157158
}
158159

@@ -414,27 +415,25 @@ function ($v) {
414415
*/
415416
protected function _unlink(array $foreignKey, Table $target, array $conditions = [])
416417
{
417-
$ok = true;
418418
$mustBeDependent = (!$this->_foreignKeyAcceptsNull($target, $foreignKey) || $this->dependent());
419-
$persistedEntitiesExist = $this->exists($conditions);
420-
421-
if ($persistedEntitiesExist) {
422-
if ($mustBeDependent) {
423-
if ($this->_cascadeCallbacks) {
424-
$query = $this->find('all')->where($conditions);
425-
foreach ($query as $assoc) {
426-
$ok = $ok && $target->delete($assoc);
427-
}
428-
} else {
429-
$ok = ($target->deleteAll($conditions) > 0);
419+
420+
if ($mustBeDependent) {
421+
if ($this->_cascadeCallbacks) {
422+
$query = $this->find('all')->where($conditions);
423+
$ok = true;
424+
foreach ($query as $assoc) {
425+
$ok = $ok && $target->delete($assoc);
430426
}
427+
return $ok;
431428
} else {
432-
$updateFields = array_fill_keys($foreignKey, null);
433-
$ok = $target->updateAll($updateFields, $conditions);
434-
429+
$target->deleteAll($conditions);
430+
return true;
435431
}
432+
} else {
433+
$updateFields = array_fill_keys($foreignKey, null);
434+
return $target->updateAll($updateFields, $conditions);
435+
436436
}
437-
return $ok;
438437
}
439438

440439
/**

tests/TestCase/ORM/TableTest.php

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4215,96 +4215,6 @@ public function testReplaceHasManyOnError()
42154215
}
42164216

42174217

4218-
/**
4219-
* Integration test for replacing entities which depend on their source entity with HasMany and failing transaction. False should be returned when
4220-
* unlinking fails while replacing
4221-
*
4222-
* @return void
4223-
*/
4224-
public function testReplaceHasManyOnErrorDependent()
4225-
{
4226-
$articles = $this->getMock(
4227-
'Cake\ORM\Table',
4228-
['deleteAll'],
4229-
[[
4230-
'connection' => $this->connection,
4231-
'alias' => 'Articles',
4232-
'table' => 'articles',
4233-
]]
4234-
);
4235-
4236-
$articles->method('deleteAll')->willReturn(false);
4237-
4238-
$associations = new AssociationCollection();
4239-
4240-
$hasManyArticles = $this->getMock(
4241-
'Cake\ORM\Association\HasMany',
4242-
['target'],
4243-
[
4244-
'articles',
4245-
[
4246-
'target' => $articles,
4247-
'foreignKey' => 'author_id',
4248-
'dependent' => true
4249-
]
4250-
]
4251-
);
4252-
$hasManyArticles->method('target')->willReturn($articles);
4253-
4254-
$associations->add('articles', $hasManyArticles);
4255-
4256-
$authors = new Table([
4257-
'connection' => $this->connection,
4258-
'alias' => 'Authors',
4259-
'table' => 'authors',
4260-
'associations' => $associations
4261-
]);
4262-
$authors->Articles->source($authors);
4263-
4264-
$author = $authors->newEntity(['name' => 'mylux']);
4265-
$author = $authors->save($author);
4266-
4267-
$newArticles = $articles->newEntities(
4268-
[
4269-
[
4270-
'title' => 'New bakery next corner',
4271-
'body' => 'They sell tastefull cakes'
4272-
],
4273-
[
4274-
'title' => 'Spicy cake recipe',
4275-
'body' => 'chocolate and peppers'
4276-
]
4277-
]
4278-
);
4279-
4280-
$sizeArticles = count($newArticles);
4281-
4282-
$this->assertTrue($authors->Articles->link($author, $newArticles));
4283-
$this->assertEquals($authors->Articles->findAllByAuthorId($author->id)->count(), $sizeArticles);
4284-
$this->assertEquals(count($author->articles), $sizeArticles);
4285-
4286-
$newArticles = array_merge(
4287-
$author->articles,
4288-
$articles->newEntities(
4289-
[
4290-
[
4291-
'title' => 'Cheese cake recipe',
4292-
'body' => 'The secrets of mixing salt and sugar'
4293-
],
4294-
[
4295-
'title' => 'Not another piece of cake',
4296-
'body' => 'This is the best'
4297-
]
4298-
]
4299-
)
4300-
);
4301-
unset($newArticles[0]);
4302-
4303-
$this->assertFalse($authors->Articles->replace($author, $newArticles));
4304-
$this->assertCount($sizeArticles, $authors->Articles->findAllByAuthorId($author->id));
4305-
}
4306-
4307-
43084218
/**
43094219
* Integration test for replacing entities which depend on their source entity with HasMany and failing transaction. False should be returned when
43104220
* unlinking fails while replacing even when cascadeCallbacks is enabled

0 commit comments

Comments
 (0)