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

Commit 303b4e0

Browse files
committed
Add tests for UploadTrait::setFileUpload
1 parent 3da686c commit 303b4e0

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

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)