Skip to content
This repository was archived by the owner on Jul 17, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Model/UploadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
}
}
64 changes: 64 additions & 0 deletions Tests/Model/UploadTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace FlexModel\FlexModelBundle\Tests\Model;

use FlexModel\FlexModelBundle\Tests\UploadEntityMock;
use FlexModel\FlexModelBundle\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
);
}
}
18 changes: 18 additions & 0 deletions Tests/UploadEntityMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace FlexModel\FlexModelBundle\Tests;

use FlexModel\FlexModelBundle\Model\UploadTrait;

/**
* Mock class for testing the UploadTrait.
*
* @author Niels Nijens <niels@connectholland.nl>
*/
class UploadEntityMock
{
use UploadTrait {
getFileUpload as getImageUpload;
setFileUpload as setImageUpload;
}
}
23 changes: 23 additions & 0 deletions Tests/UploadEntityProxyMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace FlexModel\FlexModelBundle\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.
*
* @param UploadedFile $file
*/
public function setImageUpload(UploadedFile $file = null)
{
parent::setImageUpload($file);
}
}