Overview

Namespaces

  • Composer
    • Autoload
  • Guzzle
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
        • Header
      • QueryAggregator
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Mock
    • Stream
  • Mockery
    • Adapter
      • Phpunit
    • CountValidator
    • Exception
    • Generator
      • StringManipulation
        • Pass
    • Loader
    • Matcher
  • None
  • Omnipay
    • Common
      • Exception
      • Message
    • Dummy
      • Message
    • Fatzebra
      • Message
  • PHP
  • Symfony
    • Component
      • EventDispatcher
        • Debug
        • DependencyInjection
        • Tests
          • Debug
          • DependencyInjection
      • HttpFoundation
        • File
          • Exception
          • MimeType
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
        • Tests
          • File
            • MimeType
          • Session
            • Attribute
            • Flash
            • Storage
              • Handler
              • Proxy
      • Yaml
        • Exception
        • Tests

Classes

  • Symfony\Component\Yaml\Tests\A
  • Symfony\Component\Yaml\Tests\B
  • Symfony\Component\Yaml\Tests\DumperTest
  • Symfony\Component\Yaml\Tests\InlineTest
  • Symfony\Component\Yaml\Tests\ParseExceptionTest
  • Symfony\Component\Yaml\Tests\ParserTest
  • Symfony\Component\Yaml\Tests\YamlTest
  • Overview
  • Namespace
  • Function
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * This file is part of the Symfony package.
  5:  *
  6:  * (c) Fabien Potencier <fabien@symfony.com>
  7:  *
  8:  * For the full copyright and license information, please view the LICENSE
  9:  * file that was distributed with this source code.
 10:  */
 11: 
 12: namespace Symfony\Component\HttpFoundation\Tests\File;
 13: 
 14: use Symfony\Component\HttpFoundation\File\UploadedFile;
 15: 
 16: class UploadedFileTest extends \PHPUnit_Framework_TestCase
 17: {
 18:     protected function setUp()
 19:     {
 20:         if (!ini_get('file_uploads')) {
 21:             $this->markTestSkipped('file_uploads is disabled in php.ini');
 22:         }
 23:     }
 24: 
 25:     public function testConstructWhenFileNotExists()
 26:     {
 27:         $this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
 28: 
 29:         new UploadedFile(
 30:             __DIR__.'/Fixtures/not_here',
 31:             'original.gif',
 32:             null
 33:         );
 34:     }
 35: 
 36:     public function testFileUploadsWithNoMimeType()
 37:     {
 38:         $file = new UploadedFile(
 39:             __DIR__.'/Fixtures/test.gif',
 40:             'original.gif',
 41:             null,
 42:             filesize(__DIR__.'/Fixtures/test.gif'),
 43:             UPLOAD_ERR_OK
 44:         );
 45: 
 46:         $this->assertEquals('application/octet-stream', $file->getClientMimeType());
 47: 
 48:         if (extension_loaded('fileinfo')) {
 49:             $this->assertEquals('image/gif', $file->getMimeType());
 50:         }
 51:     }
 52: 
 53:     public function testFileUploadsWithUnknownMimeType()
 54:     {
 55:         $file = new UploadedFile(
 56:             __DIR__.'/Fixtures/.unknownextension',
 57:             'original.gif',
 58:             null,
 59:             filesize(__DIR__.'/Fixtures/.unknownextension'),
 60:             UPLOAD_ERR_OK
 61:         );
 62: 
 63:         $this->assertEquals('application/octet-stream', $file->getClientMimeType());
 64:     }
 65: 
 66:     public function testGuessClientExtension()
 67:     {
 68:         $file = new UploadedFile(
 69:             __DIR__.'/Fixtures/test.gif',
 70:             'original.gif',
 71:             'image/gif',
 72:             filesize(__DIR__.'/Fixtures/test.gif'),
 73:             null
 74:         );
 75: 
 76:         $this->assertEquals('gif', $file->guessClientExtension());
 77:     }
 78: 
 79:     public function testGuessClientExtensionWithIncorrectMimeType()
 80:     {
 81:         $file = new UploadedFile(
 82:             __DIR__.'/Fixtures/test.gif',
 83:             'original.gif',
 84:             'image/jpeg',
 85:             filesize(__DIR__.'/Fixtures/test.gif'),
 86:             null
 87:         );
 88: 
 89:         $this->assertEquals('jpeg', $file->guessClientExtension());
 90:     }
 91: 
 92:     public function testErrorIsOkByDefault()
 93:     {
 94:         $file = new UploadedFile(
 95:             __DIR__.'/Fixtures/test.gif',
 96:             'original.gif',
 97:             'image/gif',
 98:             filesize(__DIR__.'/Fixtures/test.gif'),
 99:             null
100:         );
101: 
102:         $this->assertEquals(UPLOAD_ERR_OK, $file->getError());
103:     }
104: 
105:     public function testGetClientOriginalName()
106:     {
107:         $file = new UploadedFile(
108:             __DIR__.'/Fixtures/test.gif',
109:             'original.gif',
110:             'image/gif',
111:             filesize(__DIR__.'/Fixtures/test.gif'),
112:             null
113:         );
114: 
115:         $this->assertEquals('original.gif', $file->getClientOriginalName());
116:     }
117: 
118:     public function testGetClientOriginalExtension()
119:     {
120:         $file = new UploadedFile(
121:             __DIR__.'/Fixtures/test.gif',
122:             'original.gif',
123:             'image/gif',
124:             filesize(__DIR__.'/Fixtures/test.gif'),
125:             null
126:         );
127: 
128:         $this->assertEquals('gif', $file->getClientOriginalExtension());
129:     }
130: 
131:     /**
132:      * @expectedException \Symfony\Component\HttpFoundation\File\Exception\FileException
133:      */
134:     public function testMoveLocalFileIsNotAllowed()
135:     {
136:         $file = new UploadedFile(
137:             __DIR__.'/Fixtures/test.gif',
138:             'original.gif',
139:             'image/gif',
140:             filesize(__DIR__.'/Fixtures/test.gif'),
141:             UPLOAD_ERR_OK
142:         );
143: 
144:         $movedFile = $file->move(__DIR__.'/Fixtures/directory');
145:     }
146: 
147:     public function testMoveLocalFileIsAllowedInTestMode()
148:     {
149:         $path = __DIR__.'/Fixtures/test.copy.gif';
150:         $targetDir = __DIR__.'/Fixtures/directory';
151:         $targetPath = $targetDir.'/test.copy.gif';
152:         @unlink($path);
153:         @unlink($targetPath);
154:         copy(__DIR__.'/Fixtures/test.gif', $path);
155: 
156:         $file = new UploadedFile(
157:             $path,
158:             'original.gif',
159:             'image/gif',
160:             filesize($path),
161:             UPLOAD_ERR_OK,
162:             true
163:         );
164: 
165:         $movedFile = $file->move(__DIR__.'/Fixtures/directory');
166: 
167:         $this->assertTrue(file_exists($targetPath));
168:         $this->assertFalse(file_exists($path));
169:         $this->assertEquals(realpath($targetPath), $movedFile->getRealPath());
170: 
171:         @unlink($targetPath);
172:     }
173: 
174:     public function testGetClientOriginalNameSanitizeFilename()
175:     {
176:         $file = new UploadedFile(
177:             __DIR__.'/Fixtures/test.gif',
178:             '../../original.gif',
179:             'image/gif',
180:             filesize(__DIR__.'/Fixtures/test.gif'),
181:             null
182:         );
183: 
184:         $this->assertEquals('original.gif', $file->getClientOriginalName());
185:     }
186: 
187:     public function testGetSize()
188:     {
189:         $file = new UploadedFile(
190:             __DIR__.'/Fixtures/test.gif',
191:             'original.gif',
192:             'image/gif',
193:             filesize(__DIR__.'/Fixtures/test.gif'),
194:             null
195:         );
196: 
197:         $this->assertEquals(filesize(__DIR__.'/Fixtures/test.gif'), $file->getSize());
198: 
199:         $file = new UploadedFile(
200:             __DIR__.'/Fixtures/test',
201:             'original.gif',
202:             'image/gif'
203:         );
204: 
205:         $this->assertEquals(filesize(__DIR__.'/Fixtures/test'), $file->getSize());
206:     }
207: 
208:     public function testGetExtension()
209:     {
210:         $file = new UploadedFile(
211:             __DIR__.'/Fixtures/test.gif',
212:             'original.gif',
213:             null
214:         );
215: 
216:         $this->assertEquals('gif', $file->getExtension());
217:     }
218: 
219:     public function testIsValid()
220:     {
221:         $file = new UploadedFile(
222:             __DIR__.'/Fixtures/test.gif',
223:             'original.gif',
224:             null,
225:             filesize(__DIR__.'/Fixtures/test.gif'),
226:             UPLOAD_ERR_OK,
227:             true
228:         );
229: 
230:         $this->assertTrue($file->isValid());
231:     }
232: 
233:     /**
234:      * @dataProvider uploadedFileErrorProvider
235:      */
236:     public function testIsInvalidOnUploadError($error)
237:     {
238:         $file = new UploadedFile(
239:             __DIR__.'/Fixtures/test.gif',
240:             'original.gif',
241:             null,
242:             filesize(__DIR__.'/Fixtures/test.gif'),
243:             $error
244:         );
245: 
246:         $this->assertFalse($file->isValid());
247:     }
248: 
249:     public function uploadedFileErrorProvider()
250:     {
251:         return array(
252:             array(UPLOAD_ERR_INI_SIZE),
253:             array(UPLOAD_ERR_FORM_SIZE),
254:             array(UPLOAD_ERR_PARTIAL),
255:             array(UPLOAD_ERR_NO_TMP_DIR),
256:             array(UPLOAD_ERR_EXTENSION),
257:         );
258:     }
259: 
260:     public function testIsInvalidIfNotHttpUpload()
261:     {
262:         $file = new UploadedFile(
263:             __DIR__.'/Fixtures/test.gif',
264:             'original.gif',
265:             null,
266:             filesize(__DIR__.'/Fixtures/test.gif'),
267:             UPLOAD_ERR_OK
268:         );
269: 
270:         $this->assertFalse($file->isValid());
271:     }
272: }
273: 
Omnipay Fat Zebra / Paystream Gateway Module API Documentation API documentation generated by ApiGen