This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from niels-nijens/fix-doctrine-proxy-upload
Fix call stack when a Doctrine proxy subclass is active
- Loading branch information
Showing
4 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace ConnectHolland\FileUploadBundle\Tests\Model; | ||
|
||
use ConnectHolland\FileUploadBundle\Tests\UploadEntityMock; | ||
use ConnectHolland\FileUploadBundle\Tests\UploadEntityProxyMock; | ||
use PHPUnit_Framework_TestCase; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
||
/** | ||
* UploadTraitTest. | ||
* | ||
* @author Niels Nijens <niels@connectholland.nl> | ||
*/ | ||
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 | ||
); | ||
} | ||
|
||
/** | ||
* Tests if the UploadEntityMock::getImageUpload (alias of UploadTrait::getFileUpload) | ||
* returns the expected file uploads property. | ||
*/ | ||
public function testGetFileUpload() | ||
{ | ||
$uploadedFileMock = $this->getMockBuilder(UploadedFile::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$entityMock = new UploadEntityMock(); | ||
$entityMock->setImageUpload($uploadedFileMock); | ||
|
||
$this->assertSame($uploadedFileMock, $entityMock->getImageUpload($uploadedFileMock)); | ||
} | ||
|
||
/** | ||
* Tests if the UploadEntityProxyMock::getImageUpload (alias of UploadTrait::getFileUpload) | ||
* 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 testGetFileUploadFromProxy() | ||
{ | ||
$uploadedFileMock = $this->getMockBuilder(UploadedFile::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$uploadEntityProxyMock = new UploadEntityProxyMock(); | ||
$uploadEntityProxyMock->setImageUpload($uploadedFileMock); | ||
|
||
$this->assertSame($uploadedFileMock, $uploadEntityProxyMock->getImageUpload($uploadedFileMock)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ConnectHolland\FileUploadBundle\Tests; | ||
|
||
use ConnectHolland\FileUploadBundle\Model\UploadTrait; | ||
|
||
/** | ||
* Mock class for testing the UploadTrait. | ||
* | ||
* @author Niels Nijens <niels@connectholland.nl> | ||
*/ | ||
class UploadEntityMock | ||
{ | ||
use UploadTrait { | ||
getFileUpload as getImageUpload; | ||
setFileUpload as setImageUpload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace ConnectHolland\FileUploadBundle\Tests; | ||
|
||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
||
/** | ||
* Mock class mimicking the behavior of a Doctrine proxy class. | ||
* | ||
* @author Niels Nijens <niels@connectholland.nl> | ||
*/ | ||
class UploadEntityProxyMock extends UploadEntityMock | ||
{ | ||
/** | ||
* Overloaded trait method alias. | ||
* | ||
* @return UploadedFile | ||
*/ | ||
public function getImageUpload() | ||
{ | ||
return parent::getImageUpload(); | ||
} | ||
|
||
/** | ||
* Overloaded trait method alias. | ||
* | ||
* @param UploadedFile $file | ||
*/ | ||
public function setImageUpload(UploadedFile $file = null) | ||
{ | ||
parent::setImageUpload($file); | ||
} | ||
} |