1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Partnermarketing\FileSystemBundle\Tests\Functional\Factory; |
4
|
|
|
|
5
|
|
|
use Partnermarketing\FileSystemBundle\Factory\FileSystemFactory; |
6
|
|
|
use Partnermarketing\TestBundle\Tests\Base\BaseFunctionalTest; |
7
|
|
|
|
8
|
|
|
class FileSystemFactoryTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
$this->config = [ |
|
|
|
|
13
|
|
|
'local_storage' => ['path' => '', 'url' => ''], |
14
|
|
|
'amazon_s3' => ['key' => '', 'secret' => '', 'region' => '', 'bucket' => ''], |
15
|
|
|
]; |
16
|
|
|
$this->factory = new FileSystemFactory('local_storage', $this->config, '/tmp'); |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @expectedException \Exception |
21
|
|
|
* @expectedExceptionMessage the given adapter name did not match any existing file system |
22
|
|
|
*/ |
23
|
|
|
public function testThrowsAnErrorForInvalidAdapter() |
24
|
|
|
{ |
25
|
|
|
$this->factory->build('xyz'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testBuildDefault() |
29
|
|
|
{ |
30
|
|
|
$this->assertInstanceOf( |
31
|
|
|
'Partnermarketing\FileSystemBundle\Adapter\AdapterInterface', |
32
|
|
|
$this->factory->build() |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testBuildLocalStorage() |
37
|
|
|
{ |
38
|
|
|
$this->assertInstanceOf( |
39
|
|
|
'Partnermarketing\FileSystemBundle\Adapter\LocalStorage', |
40
|
|
|
$this->factory->build('local_storage') |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function testBuildAmazonS3() |
45
|
|
|
{ |
46
|
|
|
$this->assertInstanceOf( |
47
|
|
|
'Partnermarketing\FileSystemBundle\Adapter\AmazonS3', |
48
|
|
|
$this->factory->build('amazon_s3') |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @expectedException \Exception |
54
|
|
|
* @expectedExceptionMessage Invalid S3 acl value. |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function testAmazonConfigAclInvalid() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->config = [ |
59
|
|
|
'local_storage' => ['path' => '', 'url' => ''], |
60
|
|
|
'amazon_s3' => ['key' => '', 'secret' => '', 'region' => '', 'bucket' => '', 'acl' => 'read'], |
61
|
|
|
]; |
62
|
|
|
$this->factory = new FileSystemFactory('amazon_s3', $this->config, '/tmp'); |
63
|
|
|
$this->factory->build('amazon_s3'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Should |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function testAmazonConfigAclValid() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->config = [ |
72
|
|
|
'local_storage' => ['path' => '', 'url' => ''], |
73
|
|
|
'amazon_s3' => ['key' => '', 'secret' => '', 'region' => '', 'bucket' => '', 'acl' => 'public-read'], |
74
|
|
|
]; |
75
|
|
|
$this->factory = new FileSystemFactory('amazon_s3', $this->config, '/tmp'); |
76
|
|
|
|
77
|
|
|
$s3Adaptor = $this->factory->build('amazon_s3'); |
78
|
|
|
$this->assertInstanceOf('Partnermarketing\FileSystemBundle\Adapter\AmazonS3', $s3Adaptor); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testDefaultsTempDirCorrectly() |
82
|
|
|
{ |
83
|
|
|
if (defined('HHVM_VERSION')) { |
84
|
|
|
$this->markTestSkipped('HHVM does not support the moving of the configured temp directory as PHP does.'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//Force PHP to use this directory as the system temp directory. |
88
|
|
|
putenv('TMPDIR='.__DIR__.'/'); |
89
|
|
|
|
90
|
|
|
$this->config = [ |
91
|
|
|
'local_storage' => ['path' => '', 'url' => 'http://localhost/tmp'], |
92
|
|
|
'amazon_s3' => ['key' => '', 'secret' => '', 'region' => '', 'bucket' => ''], |
93
|
|
|
]; |
94
|
|
|
$this->factory = new FileSystemFactory('amazon_s3', $this->config); |
95
|
|
|
$adapter = $this->factory->build('local_storage'); |
96
|
|
|
|
97
|
|
|
$initFile = __DIR__.'/lorem.txt'; |
98
|
|
|
$adapter->writeContent($initFile, 'Lorem Ipsum'); |
99
|
|
|
$tmpFile = $adapter->copyToLocalTemporaryFile($initFile); |
100
|
|
|
|
101
|
|
|
$this->assertCount(4, $adapter->getFiles(__DIR__)); |
102
|
|
|
$this->assertTrue($adapter->exists($tmpFile)); |
103
|
|
|
|
104
|
|
|
$adapter->delete($initFile); |
105
|
|
|
$adapter->delete($tmpFile); |
106
|
|
|
$adapter->delete(substr($tmpFile, 0, (strrpos($tmpFile, ".")))); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.