-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathFileTest.php
More file actions
158 lines (136 loc) · 4.72 KB
/
FileTest.php
File metadata and controls
158 lines (136 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php namespace Codesleeve\Stapler\Factories;
use PHPUnit_Framework_TestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
class FileTest extends PHPUnit_Framework_TestCase
{
/**
* Setup method.
*
* @return void
*/
public function setUp()
{
}
/**
* Teardown method.
*
* @return void
*/
public function tearDown()
{
}
/**
* Test that the file factory can create a Codesleeve\Stapler\UploadedFile
* object from a symfony object.
*
* @test
* @return void
*/
public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_a_symfony_file_object()
{
$symfonyUploadedFile = $this->buildSymfonyUploadedFile(true);
$uploadedFile = File::create($symfonyUploadedFile);
$this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile);
}
/**
* Test that the file factory can create a Codesleeve\Stapler\UploadedFile
* object from an array.
*
* @test
* @return void
*/
public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_an_array()
{
$fileData = [
'tmp_name' => __DIR__ . '/../Fixtures/empty.gif',
'name' => 'empty.gif',
'type' => null,
'size' => null,
'error' => null
];
$uploadedFile = File::create($fileData, true);
$this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile);
}
/**
* Test that the file factory can create a Codesleeve\Stapler\UploadedFile
* object from a url
*
* @test
* @return void
*/
public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_a_url()
{
$uploadedFile = File::create('https://www.google.com/images/srpr/logo11w.png');
$this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile);
}
/**
* Test that the file factory can create a Codesleeve\Stapler\UploadedFile
* object from a redirect url
*
* @test
* @return void
*/
public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_a_redirect_url()
{
$uploadedFile = File::create('https://graph.facebook.com/10102210419817761/picture?type=large');
$this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile);
}
/**
* Test that the file factory throws an exception when an invalid URL is specified
*
* @test
* @expectedException Codesleeve\Stapler\Exceptions\FileException
* @expectedExceptionMessageRegExp #Unable to download file:.*#
* @return void
*/
public function it_should_fail_when_url_is_invalid()
{
$uploadedFile = File::create('https://graph.facebook.com/zuck/picture?type=large');
$this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile);
}
/**
* Test that file created by file factory is not containing unnecessary quer string
*
* @test
* @return void
*/
public function it_should_be_able_to_build_a_stapler_uploaded_file_object_without_following_querystring_in_basename() {
$url = "https://graph.facebook.com/10102210419817761/picture?type=large";
$uploadedFile = File::create($url);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
// To make sure that the exact image URL has query string
$this->assertGreaterThanOrEqual(0, strpos($info, '?'));
$this->assertFalse(strpos($uploadedFile->getFileName(), '?'));
}
/**
* Test that the file factory can create a Codesleeve\Stapler\UploadedFile
* object from a string filepath.
*
* @test
* @return void
*/
public function it_should_be_able_to_build_a_stapler_uploaded_file_object_from_a_string_file_path()
{
$uploadedFile = File::create(__DIR__ . '/../Fixtures/empty.gif');
$this->assertInstanceOf('Codesleeve\Stapler\File\FileInterface', $uploadedFile);
}
/**
* Helper method to build a mock Symfony UploadedFile object.
*
* @param boolean $testing
* @return UploadedFile
*/
protected function buildSymfonyUploadedFile($testing = true)
{
$path = __DIR__ . '/../Fixtures/empty.gif';
$originalName = 'empty.gif';
return new SymfonyUploadedFile($path, $originalName, null, null, null, $testing);
}
}