UploadableEntityTest.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. namespace Gedmo\Uploadable;
  3. use Tool\BaseTestCaseORM,
  4. Doctrine\Common\EventManager,
  5. Doctrine\Common\Util\Debug,
  6. Uploadable\Fixture\Entity\Image,
  7. Uploadable\Fixture\Entity\Article,
  8. Uploadable\Fixture\Entity\File,
  9. Uploadable\Fixture\Entity\FileWithoutPath,
  10. Uploadable\Fixture\Entity\FileWithSha1Name,
  11. Uploadable\Fixture\Entity\FileWithAlphanumericName,
  12. Uploadable\Fixture\Entity\FileWithCustomFilenameGenerator,
  13. Uploadable\Fixture\Entity\FileAppendNumber,
  14. Uploadable\Fixture\Entity\FileWithMaxSize,
  15. Uploadable\Fixture\Entity\FileWithAllowedTypes,
  16. Uploadable\Fixture\Entity\FileWithDisallowedTypes,
  17. Gedmo\Uploadable\Stub\UploadableListenerStub,
  18. Gedmo\Uploadable\Stub\MimeTypeGuesserStub,
  19. Gedmo\Uploadable\FileInfo\FileInfoArray;
  20. /**
  21. * These are tests for Uploadable behavior
  22. *
  23. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  24. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  25. * @package Gedmo.Uploadable
  26. * @link http://www.gediminasm.org
  27. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  28. */
  29. class UploadableEntityTest extends BaseTestCaseORM
  30. {
  31. const IMAGE_CLASS = 'Uploadable\Fixture\Entity\Image';
  32. const ARTICLE_CLASS = 'Uploadable\Fixture\Entity\Article';
  33. const FILE_CLASS = 'Uploadable\Fixture\Entity\File';
  34. const FILE_APPEND_NUMBER_CLASS = 'Uploadable\Fixture\Entity\FileAppendNumber';
  35. const FILE_WITHOUT_PATH_CLASS = 'Uploadable\Fixture\Entity\FileWithoutPath';
  36. const FILE_WITH_SHA1_NAME_CLASS = 'Uploadable\Fixture\Entity\FileWithSha1Name';
  37. const FILE_WITH_ALPHANUMERIC_NAME_CLASS = 'Uploadable\Fixture\Entity\FileWithAlphanumericName';
  38. const FILE_WITH_CUSTOM_FILENAME_GENERATOR_CLASS = 'Uploadable\Fixture\Entity\FileWithCustomFilenameGenerator';
  39. const FILE_WITH_MAX_SIZE_CLASS = 'Uploadable\Fixture\Entity\FileWithMaxSize';
  40. const FILE_WITH_ALLOWED_TYPES_CLASS = 'Uploadable\Fixture\Entity\FileWithAllowedTypes';
  41. const FILE_WITH_DISALLOWED_TYPES_CLASS = 'Uploadable\Fixture\Entity\FileWithDisallowedTypes';
  42. private $listener;
  43. private $testFile;
  44. private $testFile2;
  45. private $testFile3;
  46. private $testFileWithoutExt;
  47. private $testFileWithSpaces;
  48. private $destinationTestDir;
  49. private $destinationTestFile;
  50. private $destinationTestFile2;
  51. private $destinationTestFile3;
  52. private $destinationTestFileWithoutExt;
  53. private $destinationTestFileWithspaces;
  54. private $testFilename;
  55. private $testFilename2;
  56. private $testFilename3;
  57. private $testFilenameWithoutExt;
  58. private $testFilenameWithSpaces;
  59. private $testFileSize;
  60. private $testFileMimeType;
  61. protected function setUp()
  62. {
  63. parent::setUp();
  64. $evm = new EventManager;
  65. $this->listener = new UploadableListenerStub();
  66. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('text/plain'));
  67. $evm->addEventSubscriber($this->listener);
  68. $config = $this->getMockAnnotatedConfig();
  69. $this->em = $this->getMockSqliteEntityManager($evm, $config);
  70. $this->testFile = __DIR__.'/../../data/test.txt';
  71. $this->testFile2 = __DIR__.'/../../data/test2.txt';
  72. $this->testFile3 = __DIR__.'/../../data/test_3.txt';
  73. $this->testFileWithoutExt = __DIR__.'/../../data/test4';
  74. $this->testFileWithSpaces = __DIR__.'/../../data/test with spaces.txt';
  75. $this->destinationTestDir = __DIR__.'/../../temp/uploadable';
  76. $this->destinationTestFile = $this->destinationTestDir.'/test.txt';
  77. $this->destinationTestFile2 = $this->destinationTestDir.'/test2.txt';
  78. $this->destinationTestFile3 = $this->destinationTestDir.'/test_3.txt';
  79. $this->destinationTestFileWithoutExt = $this->destinationTestDir.'/test4';
  80. $this->destinationTestFileWithSpaces = $this->destinationTestDir.'/test with spaces';
  81. $this->testFilename = substr($this->testFile, strrpos($this->testFile, '/') + 1);
  82. $this->testFilename2 = substr($this->testFile2, strrpos($this->testFile2, '/') + 1);
  83. $this->testFilename3 = substr($this->testFile3, strrpos($this->testFile3, '/') + 1);
  84. $this->testFilenameWithoutExt = substr($this->testFileWithoutExt, strrpos($this->testFileWithoutExt, '/') + 1);
  85. $this->testFilenameWithSpaces= substr($this->testFileWithSpaces, strrpos($this->testFileWithSpaces, '/') + 1);
  86. $this->testFileSize = 4;
  87. $this->testFileMimeType = 'text/plain';
  88. $this->clearFilesAndDirectories();
  89. if (!is_dir($this->destinationTestDir)) {
  90. mkdir($this->destinationTestDir);
  91. };
  92. }
  93. public function tearDown()
  94. {
  95. $this->clearFilesAndDirectories();
  96. }
  97. public function testUploadableEntity()
  98. {
  99. // INSERTION of an Uploadable Entity
  100. // If there was no uploaded file, we do nothing
  101. $image = new Image();
  102. $image->setTitle('123');
  103. $this->em->persist($image);
  104. $this->em->flush();
  105. $this->assertNull($image->getFilePath());
  106. // If there is an uploaded file, we process it
  107. $fileInfo = $this->generateUploadedFile();
  108. $image2 = new Image();
  109. $image2->setTitle('456');
  110. $this->listener->addEntityFileInfo($image2, $fileInfo);
  111. $this->em->persist($image2);
  112. $this->em->flush();
  113. $this->em->refresh($image2);
  114. // We need to set this again because of the recent refresh
  115. $firstFile = $image2->getFilePath();
  116. $this->assertPathEquals($image2->getPath().'/'.$fileInfo['name'], $image2->getFilePath());
  117. $this->assertTrue(is_file($firstFile));
  118. $this->assertEquals($fileInfo['size'], $image2->getSize());
  119. $this->assertEquals($fileInfo['type'], $image2->getMime());
  120. // UPDATE of an Uploadable Entity
  121. // We change the "uploaded" file
  122. $fileInfo['tmp_name'] = $this->testFile2;
  123. $fileInfo['name'] = $this->testFilename2;
  124. // We use a FileInfoInterface instance here
  125. $this->listener->addEntityFileInfo($image2, new FileInfoArray($fileInfo));
  126. $this->em->flush();
  127. $this->em->refresh($image2);
  128. $lastFile = $image2->getFilePath();
  129. $this->assertPathEquals($image2->getPath().'/'.$fileInfo['name'], $image2->getFilePath());
  130. $this->assertTrue(is_file($lastFile));
  131. // First file should be removed on update
  132. $this->assertFalse(is_file($firstFile));
  133. // REMOVAL of an Uploadable Entity
  134. $this->em->remove($image2);
  135. $this->em->flush();
  136. $this->assertFalse(is_file($lastFile));
  137. }
  138. public function testEntityWithUploadableEntities()
  139. {
  140. $artRepo = $this->em->getRepository(self::ARTICLE_CLASS);
  141. $article = new Article();
  142. $article->setTitle('Test');
  143. $file1 = new File();
  144. $file2 = new File();
  145. $file3 = new File();
  146. $article->addFile($file1);
  147. $article->addFile($file2);
  148. $article->addFile($file3);
  149. $filesArrayIndex = 'file';
  150. $fileInfo = $this->generateUploadedFile($filesArrayIndex);
  151. $fileInfo2 = $this->generateUploadedFile($filesArrayIndex);
  152. $fileInfo3 = $this->generateUploadedFile($filesArrayIndex);
  153. $this->listener->addEntityFileInfo($file1, $fileInfo);
  154. $this->listener->addEntityFileInfo($file2, $fileInfo2);
  155. $this->listener->addEntityFileInfo($file3, $fileInfo3);
  156. $this->em->persist($article);
  157. $this->em->flush();
  158. $art = $artRepo->findOneByTitle('Test');
  159. $files = $art->getFiles();
  160. $file1Path = $file1->getPath().'/'.$fileInfo['name'];
  161. $file2Path = $file2->getPath().'/'.$fileInfo['name'];
  162. $file3Path = $file3->getPath().'/'.$fileInfo['name'];
  163. $this->assertPathEquals($file1Path, $files[0]->getFilePath());
  164. $this->assertPathEquals($file2Path, $files[1]->getFilePath());
  165. $this->assertPathEquals($file3Path, $files[2]->getFilePath());
  166. }
  167. /**
  168. * @expectedException Gedmo\Exception\UploadableNoPathDefinedException
  169. */
  170. public function testNoPathDefinedOnEntityOrListenerThrowsException()
  171. {
  172. $file = new FileWithoutPath();
  173. $fileInfo = $this->generateUploadedFile();
  174. $this->listener->addEntityFileInfo($file, $fileInfo);
  175. $this->em->persist($file);
  176. $this->em->flush();
  177. }
  178. public function testNoPathDefinedOnEntityButDefinedOnListenerUsesDefaultPath()
  179. {
  180. // We set the default path on the listener
  181. $this->listener->setDefaultPath($this->destinationTestDir);
  182. $file = new FileWithoutPath();
  183. $fileInfo = $this->generateUploadedFile();
  184. $this->listener->addEntityFileInfo($file, $fileInfo);
  185. $this->em->persist($file);
  186. $this->em->flush();
  187. $this->em->refresh($file);
  188. $this->assertPathEquals($this->destinationTestFile, $file->getFilePath());
  189. }
  190. public function testCallbackIsCalledIfItsSetOnEntity()
  191. {
  192. $file = new File();
  193. $fileInfo = $this->generateUploadedFile();
  194. $this->listener->addEntityFileInfo($file, $fileInfo);
  195. $this->em->persist($file);
  196. $this->em->flush();
  197. $this->assertTrue($file->callbackWasCalled);
  198. }
  199. /**
  200. * @dataProvider uploadExceptionsProvider
  201. */
  202. public function testUploadExceptions($error, $exceptionClass)
  203. {
  204. $this->setExpectedException($exceptionClass);
  205. $file = new File();
  206. $fileInfo = $this->generateUploadedFile();
  207. $fileInfo['error'] = $error;
  208. $this->listener->addEntityFileInfo($file, $fileInfo);
  209. $this->em->persist($file);
  210. $this->em->flush();
  211. }
  212. public function testSettingAnotherDefaultFileInfoClass()
  213. {
  214. $fileInfoStubClass = 'Gedmo\Uploadable\Stub\FileInfoStub';
  215. $this->listener->setDefaultFileInfoClass($fileInfoStubClass);
  216. $file = new File();
  217. $fileInfo = $this->generateUploadedFile();
  218. $this->listener->addEntityFileInfo($file, $fileInfo);
  219. $fileInfo = $this->listener->getEntityFileInfo($file);
  220. $this->assertInstanceOf($fileInfoStubClass, $fileInfo);
  221. }
  222. public function testFileWithFilenameSha1Generator()
  223. {
  224. $file = new FileWithSha1Name();
  225. $fileInfo = $this->generateUploadedFile();
  226. $this->listener->addEntityFileInfo($file, $fileInfo);
  227. $this->em->persist($file);
  228. $this->em->flush();
  229. $this->em->refresh($file);
  230. $sha1String = substr($file->getFilePath(), strrpos($file->getFilePath(), '/') + 1);
  231. $sha1String = str_replace('.txt', '', $sha1String);
  232. $this->assertRegExp('/[a-z0-9]{40}/', $sha1String);
  233. }
  234. public function testFileWithFilenameAlphanumericGenerator()
  235. {
  236. $file = new FileWithAlphanumericName();
  237. $fileInfo = $this->generateUploadedFile('image', $this->testFile3, $this->testFilename3);
  238. $this->listener->addEntityFileInfo($file, $fileInfo);
  239. $this->em->persist($file);
  240. $this->em->flush();
  241. $this->em->refresh($file);
  242. $filename = substr($file->getFilePath(), strrpos($file->getFilePath(), '/') + 1);
  243. $this->assertEquals('test-3.txt', $filename);
  244. }
  245. public function testFileWithCustomFilenameGenerator()
  246. {
  247. $file = new FileWithCustomFilenameGenerator();
  248. $fileInfo = $this->generateUploadedFile();
  249. $this->listener->addEntityFileInfo($file, $fileInfo);
  250. $this->em->persist($file);
  251. $this->em->flush();
  252. $this->em->refresh($file);
  253. $filename = substr($file->getFilePath(), strrpos($file->getFilePath(), '/') + 1);
  254. $this->assertEquals('123.txt', $filename);
  255. }
  256. public function testUploadFileWithoutExtension()
  257. {
  258. $file = new File();
  259. $fileInfo = $this->generateUploadedFile('image', $this->testFileWithoutExt, $this->testFilenameWithoutExt);
  260. $this->listener->addEntityFileInfo($file, $fileInfo);
  261. $this->em->persist($file);
  262. $this->em->flush();
  263. $this->em->refresh($file);
  264. $filePath = $file->getPath().'/'.$fileInfo['name'];
  265. $this->assertPathEquals($filePath, $file->getFilePath());
  266. }
  267. /**
  268. * @expectedException Gedmo\Exception\UploadableFileAlreadyExistsException
  269. */
  270. public function testFileAlreadyExistsException()
  271. {
  272. $file = new Image();
  273. $file->setTitle('test');
  274. $fileInfo = $this->generateUploadedFile('image', $this->testFileWithoutExt, $this->testFilenameWithoutExt);
  275. $this->listener->addEntityFileInfo($file, $fileInfo);
  276. $this->em->persist($file);
  277. $this->em->flush();
  278. $this->listener->addEntityFileInfo($file, $fileInfo);
  279. $this->em->flush();
  280. }
  281. public function test_removeFile_ifItsNotAFileThenReturnFalse()
  282. {
  283. $this->assertFalse($this->listener->removeFile('non_existent_file'));
  284. }
  285. public function test_moveFile_usingAppendNumberOptionAppendsNumberToFilenameIfItAlreadyExists()
  286. {
  287. $file = new FileAppendNumber();
  288. $file2 = new FileAppendNumber();
  289. $file->setTitle('test');
  290. $file2->setTitle('test2');
  291. $fileInfo = $this->generateUploadedFile();
  292. $this->listener->addEntityFileInfo($file, $fileInfo);
  293. $this->em->persist($file);
  294. $this->em->flush();
  295. $this->listener->addEntityFileInfo($file2, $fileInfo);
  296. $this->em->persist($file2);
  297. $this->em->flush();
  298. $this->em->refresh($file2);
  299. $filename = substr($file2->getFilePath(), strrpos($file2->getFilePath(), '/') + 1);
  300. $this->assertEquals('test-2.txt', $filename);
  301. }
  302. /**
  303. * @expectedException Gedmo\Exception\UploadableUploadException
  304. */
  305. public function test_moveFile_ifUploadedFileCantBeMovedThrowException()
  306. {
  307. $this->listener->returnFalseOnMoveUploadedFile = true;
  308. $file = new Image();
  309. $file->setTitle('test');
  310. $fileInfo = $this->generateUploadedFile();
  311. $this->listener->addEntityFileInfo($file, $fileInfo);
  312. $this->em->persist($file);
  313. $this->em->flush();
  314. }
  315. /**
  316. * @expectedException RuntimeException
  317. */
  318. public function test_addEntityFileInfo_ifFileInfoIsNotValidThrowException()
  319. {
  320. $this->listener->addEntityFileInfo(new Image, 'invalidFileInfo');
  321. }
  322. /**
  323. * @expectedException RuntimeException
  324. */
  325. public function test_getEntityFileInfo_ifTheresNoFileInfoForEntityThrowException()
  326. {
  327. $this->listener->getEntityFileInfo(new Image);
  328. }
  329. /**
  330. * @expectedException Gedmo\Exception\UploadableMaxSizeException
  331. */
  332. public function test_fileExceedingMaximumAllowedSizeThrowsException()
  333. {
  334. // We set the default path on the listener
  335. $this->listener->setDefaultPath($this->destinationTestDir);
  336. $file = new FileWithMaxSize();
  337. $fileInfo = $this->generateUploadedFile();
  338. $this->listener->addEntityFileInfo($file, $fileInfo);
  339. $this->em->persist($file);
  340. $this->em->flush();
  341. }
  342. public function test_fileNotExceedingMaximumAllowedSizeDoesntThrowException()
  343. {
  344. // We set the default path on the listener
  345. $this->listener->setDefaultPath($this->destinationTestDir);
  346. $file = new FileWithMaxSize();
  347. $size = 0.0001;
  348. $fileInfo = $this->generateUploadedFile('image', false, false, array('size' => $size));
  349. $this->listener->addEntityFileInfo($file, $fileInfo);
  350. $this->em->persist($file);
  351. $this->em->flush();
  352. $this->em->refresh($file);
  353. $this->assertEquals($size, $file->getFileSize());
  354. }
  355. /**
  356. * @expectedException Gedmo\Exception\UploadableCouldntGuessMimeTypeException
  357. */
  358. public function test_ifMimeTypeGuesserCantResolveTypeThrowException()
  359. {
  360. // We set the default path on the listener
  361. $this->listener->setDefaultPath($this->destinationTestDir);
  362. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub(null));
  363. $file = new FileWithAllowedTypes();
  364. $fileInfo = $this->generateUploadedFile();
  365. $this->listener->addEntityFileInfo($file, $fileInfo);
  366. $this->em->persist($file);
  367. $this->em->flush();
  368. }
  369. /**
  370. * @expectedException Gedmo\Exception\UploadableInvalidMimeTypeException
  371. */
  372. public function test_allowedTypesOption_ifMimeTypeIsInvalidThrowException()
  373. {
  374. // We set the default path on the listener
  375. $this->listener->setDefaultPath($this->destinationTestDir);
  376. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('text/css'));
  377. $file = new FileWithAllowedTypes();
  378. $fileInfo = $this->generateUploadedFile();
  379. $this->listener->addEntityFileInfo($file, $fileInfo);
  380. $this->em->persist($file);
  381. $this->em->flush();
  382. }
  383. public function test_allowedTypesOption_ifMimeTypeIsValidThenDontThrowException()
  384. {
  385. // We set the default path on the listener
  386. $this->listener->setDefaultPath($this->destinationTestDir);
  387. $file = new FileWithAllowedTypes();
  388. $fileInfo = $this->generateUploadedFile();
  389. $this->listener->addEntityFileInfo($file, $fileInfo);
  390. $this->em->persist($file);
  391. $this->em->flush();
  392. }
  393. /**
  394. * @expectedException Gedmo\Exception\UploadableInvalidMimeTypeException
  395. */
  396. public function test_disallowedTypesOption_ifMimeTypeIsInvalidThrowException()
  397. {
  398. // We set the default path on the listener
  399. $this->listener->setDefaultPath($this->destinationTestDir);
  400. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('text/css'));
  401. $file = new FileWithDisallowedTypes();
  402. $fileInfo = $this->generateUploadedFile();
  403. $this->listener->addEntityFileInfo($file, $fileInfo);
  404. $this->em->persist($file);
  405. $this->em->flush();
  406. }
  407. public function test_disallowedTypesOption_ifMimeTypeIsValidThenDontThrowException()
  408. {
  409. // We set the default path on the listener
  410. $this->listener->setDefaultPath($this->destinationTestDir);
  411. $this->listener->setMimeTypeGuesser(new MimeTypeGuesserStub('video/jpeg'));
  412. $file = new FileWithDisallowedTypes();
  413. $fileInfo = $this->generateUploadedFile();
  414. $this->listener->addEntityFileInfo($file, $fileInfo);
  415. $this->em->persist($file);
  416. $this->em->flush();
  417. }
  418. /**
  419. * @expectedException Gedmo\Exception\InvalidArgumentException
  420. * @dataProvider invalidFileInfoClassesProvider
  421. */
  422. public function test_setDefaultFileInfoClass_throwExceptionIfInvalidClassArePassed($class)
  423. {
  424. $this->listener->setDefaultFileInfoClass($class);
  425. }
  426. public function test_setDefaultFileInfoClass_setClassIfClassIsValid()
  427. {
  428. $validClass = 'Gedmo\\Uploadable\\FileInfo\\FileInfoArray';
  429. $this->listener->setDefaultFileInfoClass($validClass);
  430. $this->assertEquals($validClass, $this->listener->getDefaultFileInfoClass());
  431. }
  432. public function test_useGeneratedFilenameWhenAppendingNumbers()
  433. {
  434. // We set the default path on the listener
  435. $this->listener->setDefaultPath($this->destinationTestDir);
  436. $file = new FileWithAlphanumericName();
  437. $fileInfo = $this->generateUploadedFile('file', $this->testFileWithSpaces, $this->testFilenameWithSpaces);
  438. $this->listener->addEntityFileInfo($file, $fileInfo);
  439. $this->em->persist($file);
  440. $this->em->flush();
  441. $filePath = $file->getPath().'/'.str_replace(' ', '-', $fileInfo['name']);
  442. $this->assertPathEquals($filePath, $file->getFilePath());
  443. $file = new FileWithAlphanumericName();
  444. $this->listener->addEntityFileInfo($file, $fileInfo);
  445. $this->em->persist($file);
  446. $this->em->flush();
  447. $filePath = $file->getPath().'/'.str_replace(' ', '-', str_replace('.txt', '-2.txt', $fileInfo['name']));
  448. $this->assertPathEquals($filePath, $file->getFilePath());
  449. }
  450. // Data Providers
  451. public function invalidFileInfoClassesProvider()
  452. {
  453. return array(
  454. array(''),
  455. array(false),
  456. array(null),
  457. array('FakeFileInfo'),
  458. array(array()),
  459. array(new \DateTime())
  460. );
  461. }
  462. public function uploadExceptionsProvider()
  463. {
  464. return array(
  465. array(1, 'Gedmo\Exception\UploadableIniSizeException'),
  466. array(2, 'Gedmo\Exception\UploadableFormSizeException'),
  467. array(3, 'Gedmo\Exception\UploadablePartialException'),
  468. array(4, 'Gedmo\Exception\UploadableNoFileException'),
  469. array(6, 'Gedmo\Exception\UploadableNoTmpDirException'),
  470. array(7, 'Gedmo\Exception\UploadableCantWriteException'),
  471. array(8, 'Gedmo\Exception\UploadableExtensionException'),
  472. array(999, 'Gedmo\Exception\UploadableUploadException')
  473. );
  474. }
  475. // Util
  476. private function generateUploadedFile($index = 'image', $filePath = false, $filename = false, array $info = array())
  477. {
  478. $defaultInfo = array(
  479. 'tmp_name' => !$filePath ? $this->testFile : $filePath,
  480. 'name' => !$filename ? $this->testFilename : $filename,
  481. 'size' => $this->testFileSize,
  482. 'type' => $this->testFileMimeType,
  483. 'error' => 0
  484. );
  485. $info = array_merge($defaultInfo, $info);
  486. return $info;
  487. }
  488. protected function getUsedEntityFixtures()
  489. {
  490. return array(
  491. self::IMAGE_CLASS,
  492. self::ARTICLE_CLASS,
  493. self::FILE_CLASS,
  494. self::FILE_WITHOUT_PATH_CLASS,
  495. self::FILE_APPEND_NUMBER_CLASS,
  496. self::FILE_WITH_ALPHANUMERIC_NAME_CLASS,
  497. self::FILE_WITH_SHA1_NAME_CLASS,
  498. self::FILE_WITH_CUSTOM_FILENAME_GENERATOR_CLASS,
  499. self::FILE_WITH_MAX_SIZE_CLASS,
  500. self::FILE_WITH_ALLOWED_TYPES_CLASS,
  501. self::FILE_WITH_DISALLOWED_TYPES_CLASS
  502. );
  503. }
  504. private function clearFilesAndDirectories()
  505. {
  506. if (is_dir($this->destinationTestDir)) {
  507. $iter = new \DirectoryIterator($this->destinationTestDir);
  508. foreach ($iter as $fileInfo) {
  509. if (!$fileInfo->isDot()) {
  510. @unlink($fileInfo->getPathname());
  511. }
  512. }
  513. }
  514. }
  515. protected function assertPathEquals($expected, $path, $message = '')
  516. {
  517. $this->assertEquals($expected, $path, $message);
  518. }
  519. }
  520. class FakeFileInfo
  521. {
  522. }
  523. class FakeFilenameGenerator implements \Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorInterface
  524. {
  525. public static function generate($filename, $extension, $object = null)
  526. {
  527. return '123.txt';
  528. }
  529. }