diff --git a/Model/UploadTrait.php b/Model/UploadTrait.php index afd7422..45dc383 100644 --- a/Model/UploadTrait.php +++ b/Model/UploadTrait.php @@ -51,7 +51,7 @@ public function getFileUploads() */ public function setFileUpload(UploadedFile $file = null) { - $propertyName = $this->getFileUploadPropertyName(); + $propertyName = $this->getFileUploadPropertyName(__FUNCTION__); unset($this->fileUploads[$propertyName]); if ($file instanceof UploadedFile) { @@ -74,8 +74,14 @@ public function setFileUploadPath($directory) * * @return string */ - private function getFileUploadPropertyName() + private function getFileUploadPropertyName($realCallerMethod) { - return lcfirst(substr(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[1]['function'], 3, -6)); + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); + $callerMethodName = $backtrace[1]['function']; + if ($callerMethodName === $realCallerMethod) { + $callerMethodName = $backtrace[2]['function']; + } + + return lcfirst(substr($callerMethodName, 3, -6)); } } diff --git a/Tests/Model/UploadTraitTest.php b/Tests/Model/UploadTraitTest.php new file mode 100644 index 0000000..4b4067c --- /dev/null +++ b/Tests/Model/UploadTraitTest.php @@ -0,0 +1,64 @@ + + */ +class UploadTraitTest extends PHPUnit_Framework_TestCase +{ + /** + * Tests if the UploadEntityMock::setImageUpload (alias of UploadTrait::setFileUpload) + * sets the expected file uploads property. + */ + public function testSetFileUpload() + { + $uploadedFileMock = $this->getMockBuilder(UploadedFile::class) + ->disableOriginalConstructor() + ->getMock(); + + $entityMock = new UploadEntityMock(); + $entityMock->setImageUpload($uploadedFileMock); + + $this->assertAttributeSame( + array( + 'image' => $uploadedFileMock, + ), + 'fileUploads', + $entityMock + ); + } + + /** + * Tests if the UploadEntityProxyMock::setImageUpload (alias of UploadTrait::setFileUpload) + * sets the expected file uploads property. + * + * This tests the scenario of a Doctrine entity being a parent class of + * a proxy class with all the method overloaded as this changes the + * PHP stack to determine the caller method. + */ + public function testSetFileUploadFromProxy() + { + $uploadedFileMock = $this->getMockBuilder(UploadedFile::class) + ->disableOriginalConstructor() + ->getMock(); + + $uploadEntityProxyMock = new UploadEntityProxyMock(); + $uploadEntityProxyMock->setImageUpload($uploadedFileMock); + + $this->assertAttributeSame( + array( + 'image' => $uploadedFileMock, + ), + 'fileUploads', + $uploadEntityProxyMock + ); + } +} diff --git a/Tests/UploadEntityMock.php b/Tests/UploadEntityMock.php new file mode 100644 index 0000000..425f15d --- /dev/null +++ b/Tests/UploadEntityMock.php @@ -0,0 +1,18 @@ + + */ +class UploadEntityMock +{ + use UploadTrait { + getFileUpload as getImageUpload; + setFileUpload as setImageUpload; + } +} diff --git a/Tests/UploadEntityProxyMock.php b/Tests/UploadEntityProxyMock.php new file mode 100644 index 0000000..bef5dbe --- /dev/null +++ b/Tests/UploadEntityProxyMock.php @@ -0,0 +1,23 @@ + + */ +class UploadEntityProxyMock extends UploadEntityMock +{ + /** + * Overloaded trait method alias. + * + * @param UploadedFile $file + */ + public function setImageUpload(UploadedFile $file = null) + { + parent::setImageUpload($file); + } +}