Skip to content

Commit 988e403

Browse files
authored
Merge pull request #77 from angelxmoreno/fix/cakephp-52-entity-patch-deprecation
fix: resolved CakePHP deprecations in behavior and test suite
2 parents bbdaab7 + 859e118 commit 988e403

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

src/Model/Behavior/TrashBehavior.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public function trash(EntityInterface $entity, array $options = []): bool
160160
}
161161
}
162162

163-
$entity->set($this->getTrashField(false), new DateTime());
163+
$entity->patch([$this->getTrashField(false) => new DateTime()]);
164164

165165
return (bool)$this->_table->save($entity, $options);
166166
}
@@ -287,7 +287,7 @@ public function restoreTrash(?EntityInterface $entity = null, array $options = [
287287
if ($entity->isDirty()) {
288288
throw new CakeException('Can not restore from a dirty entity.');
289289
}
290-
$entity->set($data, ['guard' => false]);
290+
$entity->patch($data, ['guard' => false]);
291291

292292
return $this->_table->save($entity, $options);
293293
}
@@ -313,7 +313,9 @@ public function cascadingRestoreTrash(
313313
if ($this->_isRecursable($association, $this->_table)) {
314314
if ($entity === null) {
315315
if ($result > 1) {
316-
$result += $association->getTarget()->cascadingRestoreTrash(null, $options);
316+
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
317+
$behavior = $association->getTarget()->getBehavior('Trash');
318+
$result += $behavior->cascadingRestoreTrash(null, $options);
317319
}
318320
} else {
319321
/** @var list<string> $foreignKey */
@@ -323,10 +325,10 @@ public function cascadingRestoreTrash(
323325
$conditions = array_combine($foreignKey, $entity->extract($bindingKey));
324326

325327
foreach ($association->find('withTrashed')->where($conditions) as $related) {
328+
/** @var \Muffin\Trash\Model\Behavior\TrashBehavior $behavior */
329+
$behavior = $association->getTarget()->getBehavior('Trash');
326330
if (
327-
!$association
328-
->getTarget()
329-
->cascadingRestoreTrash($related, ['_primary' => false] + $options)
331+
!$behavior->cascadingRestoreTrash($related, ['_primary' => false] + $options)
330332
) {
331333
$result = false;
332334
}

tests/TestCase/Model/Behavior/TrashBehaviorTest.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ function (
304304
public function testTrashComposite()
305305
{
306306
$item = $this->CompositeArticlesUsers->get([3, 1]);
307-
$result = $this->CompositeArticlesUsers->trash($item);
307+
$result = $this->CompositeArticlesUsers->getBehavior('Trash')->trash($item);
308308

309309
$this->assertTrue($result);
310310
$this->assertCount(1, $this->CompositeArticlesUsers->find('onlyTrashed'));
@@ -318,7 +318,7 @@ public function testTrashComposite()
318318
public function testTrash()
319319
{
320320
$article = $this->Articles->get(1);
321-
$result = $this->Articles->trash($article);
321+
$result = $this->Articles->getBehavior('Trash')->trash($article);
322322

323323
$this->assertTrue($result);
324324
$this->assertCount(3, $this->Articles->find('withTrashed'));
@@ -343,7 +343,7 @@ public function testTrashNoPrimaryKey()
343343
$article = $this->Articles->get(1);
344344
$article->unset('id');
345345
$this->expectException(CakeException::class);
346-
$this->Articles->trash($article);
346+
$this->Articles->getBehavior('Trash')->trash($article);
347347
}
348348

349349
/**
@@ -355,7 +355,7 @@ public function testTrashNonAccessibleProperty()
355355
{
356356
$article = $this->Articles->get(1);
357357
$article->setAccess('trashed', false);
358-
$result = $this->Articles->trash($article);
358+
$result = $this->Articles->getBehavior('Trash')->trash($article);
359359

360360
$this->assertTrue($result);
361361
$this->assertCount(3, $this->Articles->find('withTrashed'));
@@ -399,7 +399,7 @@ public function testFindWithTrashed()
399399
*/
400400
public function testEmptyTrash()
401401
{
402-
$this->Articles->emptyTrash();
402+
$this->Articles->getBehavior('Trash')->emptyTrash();
403403

404404
$this->assertCount(1, $this->Articles->find());
405405
}
@@ -411,7 +411,7 @@ public function testEmptyTrash()
411411
*/
412412
public function testRestoreTrash()
413413
{
414-
$this->Articles->restoreTrash();
414+
$this->Articles->getBehavior('Trash')->restoreTrash();
415415

416416
$this->assertCount(3, $this->Articles->find());
417417
}
@@ -427,7 +427,7 @@ public function testRestoreDirtyEntity()
427427
$entity->setDirty('title');
428428

429429
$this->expectException(CakeException::class);
430-
$this->Articles->restoreTrash($entity);
430+
$this->Articles->getBehavior('Trash')->restoreTrash($entity);
431431
}
432432

433433
/**
@@ -439,7 +439,7 @@ public function testTrashAll()
439439
{
440440
$this->assertCount(1, $this->Articles->find());
441441

442-
$this->Articles->trashAll('1 = 1');
442+
$this->Articles->getBehavior('Trash')->trashAll('1 = 1');
443443
$this->assertCount(0, $this->Articles->find());
444444
}
445445

@@ -450,7 +450,7 @@ public function testTrashAll()
450450
*/
451451
public function testRestoreTrashEntity()
452452
{
453-
$this->Articles->restoreTrash(new Entity([
453+
$this->Articles->getBehavior('Trash')->restoreTrash(new Entity([
454454
'id' => 2,
455455
], ['markNew' => false, 'markClean' => true]));
456456

@@ -502,7 +502,7 @@ public function testInteroperabilityWithCounterCache()
502502
public function testInteroperabilityWithCounterCacheAndTrashMethod()
503503
{
504504
$comment = $this->Comments->get(1);
505-
$this->Comments->trash($comment);
505+
$this->Comments->getBehavior('Trash')->trash($comment);
506506
$result = $this->Articles->get(1);
507507

508508
$this->assertEquals(0, $result->comment_count);
@@ -521,7 +521,7 @@ public function testCascadingTrash()
521521
$association->setCascadeCallbacks(true);
522522

523523
$article = $this->Articles->get(1);
524-
$this->Articles->trash($article);
524+
$this->Articles->getBehavior('Trash')->trash($article);
525525

526526
$article = $this->Articles->find('withTrashed')
527527
->where(['Articles.id' => 1])
@@ -553,7 +553,7 @@ public function testDisabledCascadingForTrash()
553553
$this->Articles->behaviors()->get('Trash')->setConfig('cascadeOnTrash', false);
554554

555555
$article = $this->Articles->get(1);
556-
$this->Articles->trash($article);
556+
$this->Articles->getBehavior('Trash')->trash($article);
557557

558558
$article = $this->Articles->find('withTrashed')
559559
->where(['Articles.id' => 1])
@@ -575,10 +575,10 @@ public function testCascadingUntrashOptionsArePassedToSave()
575575
$association->setDependent(true);
576576
$association->setCascadeCallbacks(true);
577577

578-
$this->Articles->Comments->getTarget()->trashAll([]);
578+
$this->Articles->Comments->getTarget()->getBehavior('Trash')->trashAll([]);
579579
$this->assertEquals(0, $this->Articles->Comments->getTarget()->find()->count());
580580

581-
$this->Articles->trashAll([]);
581+
$this->Articles->getBehavior('Trash')->trashAll([]);
582582
$this->assertEquals(0, $this->Articles->find()->count());
583583

584584
$article = $this->Articles
@@ -615,7 +615,7 @@ function (
615615
}
616616
);
617617

618-
$result = $this->Articles->cascadingRestoreTrash($article, [
618+
$result = $this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article, [
619619
'restoreOptions' => true,
620620
]);
621621

@@ -641,13 +641,13 @@ public function testCascadingUntrashEntity()
641641
$association->setDependent(true);
642642
$association->setCascadeCallbacks(true);
643643

644-
$this->Articles->Comments->getTarget()->trashAll([]);
644+
$this->Articles->Comments->getTarget()->getBehavior('Trash')->trashAll([]);
645645
$this->assertEquals(0, $this->Articles->Comments->getTarget()->find()->count());
646646

647-
$this->Articles->CompositeArticlesUsers->getTarget()->trashAll([]);
647+
$this->Articles->CompositeArticlesUsers->getTarget()->getBehavior('Trash')->trashAll([]);
648648
$this->assertEquals(0, $this->Articles->CompositeArticlesUsers->getTarget()->find()->count());
649649

650-
$this->Articles->trashAll([]);
650+
$this->Articles->getBehavior('Trash')->trashAll([]);
651651
$this->assertEquals(0, $this->Articles->find()->count());
652652

653653
$article = $this->Articles
@@ -690,7 +690,7 @@ public function testCascadingUntrashEntity()
690690

691691
$this->assertInstanceOf(
692692
EntityInterface::class,
693-
$this->Articles->cascadingRestoreTrash($article)
693+
$this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article)
694694
);
695695

696696
$article = $this->Articles
@@ -735,13 +735,13 @@ public function testCascadingUntrashAll()
735735
$association->setDependent(true);
736736
$association->setCascadeCallbacks(true);
737737

738-
$this->Articles->Comments->getTarget()->trashAll([]);
738+
$this->Articles->Comments->getTarget()->getBehavior('Trash')->trashAll([]);
739739
$this->assertEquals(0, $this->Articles->Comments->getTarget()->find()->count());
740740

741-
$this->Articles->CompositeArticlesUsers->getTarget()->trashAll([]);
741+
$this->Articles->CompositeArticlesUsers->getTarget()->getBehavior('Trash')->trashAll([]);
742742
$this->assertEquals(0, $this->Articles->CompositeArticlesUsers->getTarget()->find()->count());
743743

744-
$this->Articles->trashAll([]);
744+
$this->Articles->getBehavior('Trash')->trashAll([]);
745745
$this->assertEquals(0, $this->Articles->find()->count());
746746

747747
$article = $this->Articles
@@ -766,7 +766,7 @@ public function testCascadingUntrashAll()
766766
$this->assertNotEmpty($article->composite_articles_users[0]->trashed);
767767
$this->assertInstanceOf(DateTime::class, $article->composite_articles_users[0]->trashed);
768768

769-
$this->assertEquals(8, $this->Articles->cascadingRestoreTrash());
769+
$this->assertEquals(8, $this->Articles->getBehavior('Trash')->cascadingRestoreTrash());
770770

771771
$article = $this->Articles
772772
->find()
@@ -793,22 +793,23 @@ public function testCascadingUntrashFailure()
793793
$association = $this->Articles->Comments;
794794
$association->setDependent(true);
795795
$association->setCascadeCallbacks(true);
796-
$association->getEventManager()->on('Model.beforeSave', function () {
797-
return false;
796+
$association->getEventManager()->on('Model.beforeSave', function ($event) {
797+
$event->setResult(false);
798+
$event->stopPropagation();
798799
});
799800

800-
$association->getTarget()->trashAll([]);
801+
$association->getTarget()->getBehavior('Trash')->trashAll([]);
801802
$this->assertEquals(0, $association->getTarget()->find()->count());
802803

803-
$this->Articles->trashAll([]);
804+
$this->Articles->getBehavior('Trash')->trashAll([]);
804805
$this->assertEquals(0, $this->Articles->find()->count());
805806

806807
$article = $this->Articles
807808
->find('withTrashed')
808809
->where(['Articles.id' => 1])
809810
->first();
810811

811-
$this->assertFalse($this->Articles->cascadingRestoreTrash($article));
812+
$this->assertFalse($this->Articles->getBehavior('Trash')->cascadingRestoreTrash($article));
812813
}
813814

814815
/**

0 commit comments

Comments
 (0)