Skip to content

Commit 324dc73

Browse files
authored
Merge pull request #603 from ndm2/8.x-fix-after-delete
8.x - Fix `afterDelete` expecting an `UploadedFileInterface` object.
2 parents dda6529 + b61880a commit 324dc73

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

src/Model/Behavior/UploadBehavior.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,7 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra
176176
if ($callback && is_callable($callback)) {
177177
$files = $callback($path, $entity, $field, $settings);
178178
} else {
179-
/** @var \Psr\Http\Message\UploadedFileInterface $uploaded */
180-
$uploaded = $entity->get($field);
181-
182-
$files = [$path . $uploaded->getClientFilename()];
179+
$files = [$path . $entity->get($field)];
183180
}
184181

185182
$writer = $this->getWriter($entity, null, $field, $settings);

tests/TestCase/Model/Behavior/UploadBehaviorTest.php

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public function testAfterDeleteOk()
438438
$this->entity->expects($this->any())
439439
->method('get')
440440
->with('field')
441-
->will($this->returnValue($this->dataOk['field']));
441+
->will($this->returnValue('file.txt'));
442442
$behavior->expects($this->any())
443443
->method('getPathProcessor')
444444
->willReturn($this->processor);
@@ -463,7 +463,7 @@ public function testAfterDeleteFail()
463463
$this->entity->expects($this->any())
464464
->method('get')
465465
->with('field')
466-
->will($this->returnValue($this->dataOk['field']));
466+
->will($this->returnValue('file.txt'));
467467
$behavior->expects($this->any())
468468
->method('getPathProcessor')
469469
->willReturn($this->processor);
@@ -498,13 +498,7 @@ public function testAfterDeleteSkip()
498498
public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile()
499499
{
500500
$dir = '/some/path/';
501-
$field = new UploadedFile(
502-
fopen('php://temp', 'wb+'),
503-
1,
504-
UPLOAD_ERR_OK,
505-
'file.txt',
506-
'text/plain'
507-
);
501+
$field = 'file.txt';
508502

509503
$methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']);
510504
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
@@ -540,7 +534,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile()
540534
// and here we check that file with right path will be deleted
541535
$this->writer->expects($this->once())
542536
->method('delete')
543-
->with([$dir . $field->getClientFilename()])
537+
->with([$dir . $field])
544538
->willReturn([true]);
545539

546540
$behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject());
@@ -549,13 +543,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile()
549543
public function testAfterDeletePrefersStoredPathOverPathProcessor()
550544
{
551545
$dir = '/some/path/';
552-
$field = new UploadedFile(
553-
fopen('php://temp', 'wb+'),
554-
1,
555-
UPLOAD_ERR_OK,
556-
'file.txt',
557-
'text/plain'
558-
);
546+
$field = 'file.txt';
559547

560548
$methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']);
561549
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
@@ -582,15 +570,15 @@ public function testAfterDeletePrefersStoredPathOverPathProcessor()
582570

583571
$this->writer->expects($this->once())
584572
->method('delete')
585-
->with([$dir . $field->getClientFilename()])
573+
->with([$dir . $field])
586574
->will($this->returnValue([true]));
587575

588576
$this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject()));
589577
}
590578

591579
public function testAfterDeleteNoDeleteCallback()
592580
{
593-
$this->entity->field = rand(1000, 9999);
581+
$field = (string)rand(1000, 9999);
594582
$path = rand(1000, 9999) . DIRECTORY_SEPARATOR;
595583
$methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']);
596584
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
@@ -601,12 +589,12 @@ public function testAfterDeleteNoDeleteCallback()
601589
$this->configOk['field']['deleteCallback'] = null;
602590

603591
$behavior->setConfig($this->configOk);
604-
$this->entity->expects($this->any())
592+
$this->entity->expects($this->exactly(2))
605593
->method('get')
606594
->with('field')
607-
->will($this->returnValue($this->dataOk['field']));
595+
->will($this->returnValue($field));
608596
$behavior->expects($this->once())->method('getPathProcessor')
609-
->with($this->entity, $this->dataOk['field'], 'field', $this->configOk['field'])
597+
->with($this->entity, $field, 'field', $this->configOk['field'])
610598
->willReturn($this->processor);
611599
$this->processor->expects($this->once())->method('basepath')
612600
->willReturn($path);
@@ -616,7 +604,7 @@ public function testAfterDeleteNoDeleteCallback()
616604
$this->writer->expects($this->once())
617605
->method('delete')
618606
->with([
619-
$path . $this->entity->field . $this->dataOk['field']->getClientFilename(),
607+
$path . $field,
620608
])
621609
->willReturn([true, true, true]);
622610

@@ -625,7 +613,7 @@ public function testAfterDeleteNoDeleteCallback()
625613

626614
public function testAfterDeleteUsesDeleteCallback()
627615
{
628-
$this->entity->field = rand(1000, 9999);
616+
$field = (string)rand(1000, 9999);
629617
$path = rand(1000, 9999) . DIRECTORY_SEPARATOR;
630618
$methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']);
631619
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
@@ -635,19 +623,19 @@ public function testAfterDeleteUsesDeleteCallback()
635623

636624
$this->configOk['field']['deleteCallback'] = function ($path, $entity, $field, $settings) {
637625
return [
638-
$path . $entity->{$field},
639-
$path . 'sm-' . $entity->{$field},
640-
$path . 'lg-' . $entity->{$field},
626+
$path . $entity->get($field),
627+
$path . 'sm-' . $entity->get($field),
628+
$path . 'lg-' . $entity->get($field),
641629
];
642630
};
643631

644632
$behavior->setConfig($this->configOk);
645-
$this->entity->expects($this->any())
633+
$this->entity->expects($this->exactly(4))
646634
->method('get')
647635
->with('field')
648-
->will($this->returnValue($this->dataOk['field']));
636+
->will($this->returnValue($field));
649637
$behavior->expects($this->once())->method('getPathProcessor')
650-
->with($this->entity, $this->dataOk['field'], 'field', $this->configOk['field'])
638+
->with($this->entity, $field, 'field', $this->configOk['field'])
651639
->willReturn($this->processor);
652640
$this->processor->expects($this->once())->method('basepath')
653641
->willReturn($path);
@@ -657,9 +645,9 @@ public function testAfterDeleteUsesDeleteCallback()
657645
$this->writer->expects($this->once())
658646
->method('delete')
659647
->with([
660-
$path . $this->entity->field,
661-
$path . 'sm-' . $this->entity->field,
662-
$path . 'lg-' . $this->entity->field,
648+
$path . $field,
649+
$path . 'sm-' . $field,
650+
$path . 'lg-' . $field,
663651
])
664652
->willReturn([true, true, true]);
665653

0 commit comments

Comments
 (0)