Skip to content

Commit 5720a58

Browse files
committed
Merge pull request #19 from jdecool/unit-tests
Add complete bundle unit testing
2 parents e09e939 + d37b2a2 commit 5720a58

14 files changed

+953
-20
lines changed

.atoum.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$script->addTestsFromDirectory(__DIR__ . '/Tests/Units');

.bootstrap.atoum.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require_once __DIR__ . '/vendor/autoload.php';

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ php:
77
- 5.3
88

99
before_script:
10-
- wget http://getcomposer.org/composer.phar
11-
- php composer.phar install --dev
10+
- composer install --dev
1211

1312
script:
14-
vendor/bin/atoum -bf Tests/bootstrap.atoum.php -d Tests
13+
vendor/bin/atoum

Manager/BaseEntityWithFileManager.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class BaseEntityWithFileManager
5252
*/
5353
public function __construct($arrayFilepath, $entityManager)
5454
{
55+
if (!isset($arrayFilepath['bundle.web'])) {
56+
throw new \InvalidArgumentException('$arrayFilepath must have a bundle.web key (even empty).');
57+
}
58+
5559
$this->entityManager = $entityManager;
5660
$this->webPath = $arrayFilepath['bundle.web'];
5761

@@ -106,9 +110,12 @@ protected function setter($propertyName, $filenameOnly = false)
106110
public function getFileAbsolutePath(BaseEntityWithFile $entity, $propertyName)
107111
{
108112
$getter = $this->getter($propertyName, true);
109-
$entity->$getter();
110-
$path = sprintf('%s%s', $this->rootPath,
111-
$entity->$getter());
113+
114+
try {
115+
$path = sprintf('%s%s', $this->rootPath, $entity->$getter());
116+
} catch (\Exception $e) {
117+
throw new \UnexpectedValueException();
118+
}
112119

113120
return $path;
114121
}
@@ -125,7 +132,13 @@ public function getFileWebPath(BaseEntityWithFile $entity, $propertyName)
125132
{
126133
$getter = $this->getter($propertyName, true);
127134

128-
return sprintf('%s%s', $this->webPath, $entity->$getter());
135+
try {
136+
$path = sprintf('%s%s', $this->webPath, $entity->$getter());
137+
} catch (\Exception $e) {
138+
throw new \UnexpectedValueException();
139+
}
140+
141+
return $path;
129142
}
130143

131144
/**
@@ -253,14 +266,22 @@ protected function buildDestination(BaseEntityWithFile $entity, $propertyName, $
253266
$this->arrayFilepath[$propertyName]);
254267

255268
//Replace slugged placeholder
256-
$fileDestinationName = preg_replace(
257-
'#{slug::([^}-]+)}#ie', '$this->slug($entity->get("$1"))', $fileDestinationName);
269+
$fileDestinationName = preg_replace_callback(
270+
'#{slug::([^}-]+)}#i',
271+
create_function('$matches', 'return $this->slug($entity->get("$1"));'),
272+
$fileDestinationName);
273+
258274
//Replace date format placeholder
259-
$fileDestinationName = preg_replace(
260-
'#{date::([^}-]+)::([^}-]+)}#ie', '$entity->get("$2")->format("$1")', $fileDestinationName);
275+
$fileDestinationName = preg_replace_callback(
276+
'#{date::([^}-]+)::([^}-]+)}#i',
277+
create_function('$matches', 'return $entity->get("$2")->format("$1");'),
278+
$fileDestinationName);
279+
261280
//Replace classic placeholder
262-
$fileDestinationName = preg_replace(
263-
'#{([^}-]+)}#ie', '$entity->get("$1")', $fileDestinationName);
281+
$fileDestinationName = preg_replace_callback(
282+
'#{([^}-]+)}#i',
283+
create_function('$matches', 'return $entity->get("$1");'),
284+
$fileDestinationName);
264285

265286
return $fileDestinationName;
266287
}
@@ -409,6 +430,10 @@ public function removeFiles(BaseEntityWithFile $entity, $properties = array(), $
409430
*/
410431
public function replaceFile(BaseEntityWithFile $entity, $propertyName, $sourceFilepath, $destFilepath = null, $operation = 'copy')
411432
{
433+
if (!in_array($operation, array('copy', 'rename'))) {
434+
throw new \InvalidArgumentException('$operation only accept "copy" or "rename" value');
435+
}
436+
412437
$propertyGetter = $this->getter($propertyName);
413438
$propertyFileNameGetter = $this->getter($propertyName, true);
414439
$propertyFileNameSetter = $this->setter($propertyName, true);

Manager/BaseEntityWithImageManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,11 @@ public function removeFiles(BaseEntityWithFile $entity, $properties = array(), $
271271
* @param string $propertyName The property linked to the file
272272
* @param string $sourceFilepath The image source folder
273273
* @param string|null $destFilepath The image destination folder
274+
* @param string $operation 'copy' or 'rename'
274275
*
275276
* @return array|null An array containing informations about the copied file
276277
*/
277-
public function replaceFile(BaseEntityWithFile $entity, $propertyName, $sourceFilepath, $destFilepath = null)
278+
public function replaceFile(BaseEntityWithFile $entity, $propertyName, $sourceFilepath, $destFilepath = null, $operation = 'copy')
278279
{
279280
$propertyGetter = $this->getter($propertyName);
280281
$propertyFileNameGetter = $this->getter($propertyName, true);

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Unit tests are written using [atoum](https://github.com/atoum/atoum). You will g
1818
running `composer install`. To run tests, you will need to run the following command :
1919

2020
``` sh
21-
$ vendor/bin/atoum -bf Tests/bootstrap.atoum.php -d Tests
21+
$ vendor/bin/atoum
2222

2323
# To run tests in a loop, ideal to do TDD
24-
$ vendor/bin/atoum -bf Tests/bootstrap.atoum.php -d Tests --loop
24+
$ vendor/bin/atoum --loop
2525
```
2626

2727
### Contributors:

Resources/doc/reference-BaseEntityWithImageManager.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Replace a property file by another, giver it&#039;s path
102102
- `$propertyName` (`string`) &mdash; The property linked to the file
103103
- `$sourceFilepath` (`string`) &mdash; The image source folder
104104
- `$destFilepath` (`string`|`null`) &mdash; The image destination folder
105+
- `$operation` (`string`) &mdash; &#039;copy&#039; or &#039;rename&#039;
105106
- _Returns:_ An array containing informations about the copied file
106107
- `array`
107108
- `null`

Tests/Helper/BaseManagerTestCase.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Novaway\Bundle\FileManagementBundle\Tests\Helper;
4+
5+
class BaseManagerTestCase extends FileTestCase
6+
{
7+
protected function createMockEntityManager()
8+
{
9+
return new \mock\EntityManager();
10+
}
11+
12+
protected function createMockFileType($originalFilename = null)
13+
{
14+
$controller = null;
15+
if (null === $originalFilename) {
16+
$controller = new \atoum\mock\controller();
17+
$controller->__construct = function() { };
18+
}
19+
20+
$filetype = new \mock\Symfony\Component\HttpFoundation\File\UploadedFile(
21+
$originalFilename,
22+
basename($originalFilename),
23+
null,
24+
null,
25+
null,
26+
true,
27+
$controller
28+
);
29+
30+
if (null !== $originalFilename) {
31+
$filetype->getMockController()->getClientOriginalName = function() use ($originalFilename) {
32+
return $originalFilename;
33+
};
34+
}
35+
36+
$filetype->getMockController()->getError = function() {
37+
return UPLOAD_ERR_OK;
38+
};
39+
40+
return $filetype;
41+
}
42+
43+
protected function createMockEntity(array $fields = array())
44+
{
45+
$entity = new \mock\Novaway\Bundle\FileManagementBundle\Entity\BaseEntityWithFile();
46+
foreach ($fields as $name => $value) {
47+
$entity->$name = $value;
48+
49+
// mock accessors
50+
$accessor = sprintf('get%s', ucfirst($name));
51+
$entity->getMockController()->$accessor = function() use ($entity, $name) {
52+
return $entity->$name;
53+
};
54+
55+
// mock setters
56+
$setter = sprintf('set%s', ucfirst($name));
57+
$entity->getMockController()->$setter = function($param) use ($entity, $name) {
58+
$entity->$name = $param;
59+
return $entity;
60+
};
61+
}
62+
63+
return $entity;
64+
}
65+
}

Tests/Helper/FileTestCase.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Novaway\Bundle\FileManagementBundle\Tests\Helper;
4+
5+
use mageekguy\atoum;
6+
7+
class FileTestCase extends atoum\test
8+
{
9+
protected $workspace = null;
10+
protected static $symlinkOnWindows = null;
11+
12+
public function beforeTestMethod($testMethod)
13+
{
14+
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
15+
static::$symlinkOnWindows = true;
16+
$originDir = tempnam(sys_get_temp_dir(), 'sl');
17+
$targetDir = tempnam(sys_get_temp_dir(), 'sl');
18+
if (true !== @symlink($originDir, $targetDir)) {
19+
$report = error_get_last();
20+
if (is_array($report) && false !== strpos($report['message'], 'error code(1314)')) {
21+
static::$symlinkOnWindows = false;
22+
}
23+
}
24+
}
25+
26+
$this->workspace = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.time().rand(0, 1000).uniqid();
27+
mkdir($this->workspace, 0777, true);
28+
$this->workspace = realpath($this->workspace);
29+
}
30+
31+
public function afterTestMethod($testMethod)
32+
{
33+
$this->clean($this->workspace);
34+
}
35+
36+
protected function clean($file)
37+
{
38+
if (is_dir($file) && !is_link($file)) {
39+
$dir = new \FilesystemIterator($file);
40+
foreach ($dir as $childFile) {
41+
$this->clean($childFile);
42+
}
43+
44+
rmdir($file);
45+
} else {
46+
unlink($file);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)