Skip to content
This repository was archived by the owner on Jul 17, 2020. It is now read-only.

Commit a9e57e2

Browse files
authored
Merge pull request #57 from niels-nijens/fix-doctrine-proxy-upload
Fix call stack when a Doctrine proxy subclass is active
2 parents 3da686c + b791192 commit a9e57e2

4 files changed

Lines changed: 114 additions & 3 deletions

File tree

Model/UploadTrait.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getFileUploads()
5151
*/
5252
public function setFileUpload(UploadedFile $file = null)
5353
{
54-
$propertyName = $this->getFileUploadPropertyName();
54+
$propertyName = $this->getFileUploadPropertyName(__FUNCTION__);
5555

5656
unset($this->fileUploads[$propertyName]);
5757
if ($file instanceof UploadedFile) {
@@ -74,8 +74,14 @@ public function setFileUploadPath($directory)
7474
*
7575
* @return string
7676
*/
77-
private function getFileUploadPropertyName()
77+
private function getFileUploadPropertyName($realCallerMethod)
7878
{
79-
return lcfirst(substr(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[1]['function'], 3, -6));
79+
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
80+
$callerMethodName = $backtrace[1]['function'];
81+
if ($callerMethodName === $realCallerMethod) {
82+
$callerMethodName = $backtrace[2]['function'];
83+
}
84+
85+
return lcfirst(substr($callerMethodName, 3, -6));
8086
}
8187
}

Tests/Model/UploadTraitTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace FlexModel\FlexModelBundle\Tests\Model;
4+
5+
use FlexModel\FlexModelBundle\Tests\UploadEntityMock;
6+
use FlexModel\FlexModelBundle\Tests\UploadEntityProxyMock;
7+
use PHPUnit_Framework_TestCase;
8+
use Symfony\Component\HttpFoundation\File\UploadedFile;
9+
10+
/**
11+
* UploadTraitTest.
12+
*
13+
* @author Niels Nijens <niels@connectholland.nl>
14+
*/
15+
class UploadTraitTest extends PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* Tests if the UploadEntityMock::setImageUpload (alias of UploadTrait::setFileUpload)
19+
* sets the expected file uploads property.
20+
*/
21+
public function testSetFileUpload()
22+
{
23+
$uploadedFileMock = $this->getMockBuilder(UploadedFile::class)
24+
->disableOriginalConstructor()
25+
->getMock();
26+
27+
$entityMock = new UploadEntityMock();
28+
$entityMock->setImageUpload($uploadedFileMock);
29+
30+
$this->assertAttributeSame(
31+
array(
32+
'image' => $uploadedFileMock,
33+
),
34+
'fileUploads',
35+
$entityMock
36+
);
37+
}
38+
39+
/**
40+
* Tests if the UploadEntityProxyMock::setImageUpload (alias of UploadTrait::setFileUpload)
41+
* sets the expected file uploads property.
42+
*
43+
* This tests the scenario of a Doctrine entity being a parent class of
44+
* a proxy class with all the method overloaded as this changes the
45+
* PHP stack to determine the caller method.
46+
*/
47+
public function testSetFileUploadFromProxy()
48+
{
49+
$uploadedFileMock = $this->getMockBuilder(UploadedFile::class)
50+
->disableOriginalConstructor()
51+
->getMock();
52+
53+
$uploadEntityProxyMock = new UploadEntityProxyMock();
54+
$uploadEntityProxyMock->setImageUpload($uploadedFileMock);
55+
56+
$this->assertAttributeSame(
57+
array(
58+
'image' => $uploadedFileMock,
59+
),
60+
'fileUploads',
61+
$uploadEntityProxyMock
62+
);
63+
}
64+
}

Tests/UploadEntityMock.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace FlexModel\FlexModelBundle\Tests;
4+
5+
use FlexModel\FlexModelBundle\Model\UploadTrait;
6+
7+
/**
8+
* Mock class for testing the UploadTrait.
9+
*
10+
* @author Niels Nijens <niels@connectholland.nl>
11+
*/
12+
class UploadEntityMock
13+
{
14+
use UploadTrait {
15+
getFileUpload as getImageUpload;
16+
setFileUpload as setImageUpload;
17+
}
18+
}

Tests/UploadEntityProxyMock.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace FlexModel\FlexModelBundle\Tests;
4+
5+
use Symfony\Component\HttpFoundation\File\UploadedFile;
6+
7+
/**
8+
* Mock class mimicking the behavior of a Doctrine proxy class.
9+
*
10+
* @author Niels Nijens <niels@connectholland.nl>
11+
*/
12+
class UploadEntityProxyMock extends UploadEntityMock
13+
{
14+
/**
15+
* Overloaded trait method alias.
16+
*
17+
* @param UploadedFile $file
18+
*/
19+
public function setImageUpload(UploadedFile $file = null)
20+
{
21+
parent::setImageUpload($file);
22+
}
23+
}

0 commit comments

Comments
 (0)