UploadableEntityTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. Gedmo\Uploadable\Stub\UploadableListenerStub,
  11. Gedmo\Uploadable\FileInfo\FileInfoArray;
  12. /**
  13. * These are tests for Uploadable behavior
  14. *
  15. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  16. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  17. * @package Gedmo.Uploadable
  18. * @link http://www.gediminasm.org
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. class UploadableEntityTest extends BaseTestCaseORM
  22. {
  23. const IMAGE_CLASS = 'Uploadable\Fixture\Entity\Image';
  24. const ARTICLE_CLASS = 'Uploadable\Fixture\Entity\Article';
  25. const FILE_CLASS = 'Uploadable\Fixture\Entity\File';
  26. const FILE_WITHOUT_PATH_CLASS = 'Uploadable\Fixture\Entity\FileWithoutPath';
  27. private $listener;
  28. private $testFile;
  29. private $testFile2;
  30. private $destinationTestDir;
  31. private $destinationTestFile;
  32. private $destinationTestFile2;
  33. private $testFilename;
  34. private $testFilename2;
  35. private $testFileSize;
  36. private $testFileMimeType;
  37. protected function setUp()
  38. {
  39. parent::setUp();
  40. $evm = new EventManager;
  41. $this->listener = new UploadableListenerStub();
  42. $evm->addEventSubscriber($this->listener);
  43. $config = $this->getMockAnnotatedConfig();
  44. $this->em = $this->getMockSqliteEntityManager($evm, $config);
  45. $this->testFile = __DIR__.'/../../data/test.txt';
  46. $this->testFile2 = __DIR__.'/../../data/test2.txt';
  47. $this->destinationTestDir = __DIR__.'/../../temp/uploadable';
  48. $this->destinationTestFile = $this->destinationTestDir.'/test.txt';
  49. $this->destinationTestFile2 = $this->destinationTestDir.'/test2.txt';
  50. $this->testFilename = substr($this->testFile, strrpos($this->testFile, '/') + 1);
  51. $this->testFilename2 = substr($this->testFile2, strrpos($this->testFile2, '/') + 1);
  52. $this->testFileSize = 4;
  53. $this->testFileMimeType = 'text/plain';
  54. $this->clearFilesAndDirectories();
  55. mkdir($this->destinationTestDir);
  56. }
  57. public function tearDown()
  58. {
  59. $this->clearFilesAndDirectories();
  60. }
  61. public function testUploadableEntity()
  62. {
  63. // INSERTION of an Uploadable Entity
  64. // If there was no uploaded file, we do nothing
  65. $image = new Image();
  66. $image->setTitle('123');
  67. $this->em->persist($image);
  68. $this->em->flush();
  69. $this->assertNull($image->getFilePath());
  70. // If there is an uploaded file, we process it
  71. $fileInfo = $this->generateUploadedFile();
  72. $image2 = new Image();
  73. $image2->setTitle('456');
  74. $image2->setFileInfo($fileInfo);
  75. $this->em->persist($image2);
  76. $this->em->flush();
  77. $this->em->refresh($image2);
  78. // We need to set this again because of the recent refresh
  79. $firstFile = $image2->getFilePath();
  80. $this->assertEquals($image2->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'], $image2->getFilePath());
  81. $this->assertTrue(is_file($firstFile));
  82. // UPDATE of an Uploadable Entity
  83. // We change the "uploaded" file
  84. $fileInfo['tmp_name'] = $this->testFile2;
  85. $fileInfo['name'] = $this->testFilename2;
  86. // We use a FileInfoInterface instance here
  87. $image2->setFileInfo(new FileInfoArray($fileInfo));
  88. // For now, we need to force the update changing one of the managed fields. If we don't do this,
  89. // entity won't be marked for update
  90. $image2->setTitle($image2->getTitle().'7892');
  91. $this->em->flush();
  92. $this->em->refresh($image2);
  93. $lastFile = $image2->getFilePath();
  94. $this->assertEquals($image2->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'], $image2->getFilePath());
  95. $this->assertTrue(is_file($lastFile));
  96. // First file should be removed on update
  97. $this->assertFalse(is_file($firstFile));
  98. // REMOVAL of an Uploadable Entity
  99. $this->em->remove($image2);
  100. $this->em->flush();
  101. $this->assertFalse(is_file($lastFile));
  102. }
  103. public function testEntityWithUploadableEntities()
  104. {
  105. $artRepo = $this->em->getRepository(self::ARTICLE_CLASS);
  106. $article = new Article();
  107. $article->setTitle('Test');
  108. $file1 = new File();
  109. $file2 = new File();
  110. $file3 = new File();
  111. $article->addFile($file1);
  112. $article->addFile($file2);
  113. $article->addFile($file3);
  114. $filesArrayIndex = 'file';
  115. $fileInfo = $this->generateUploadedFile($filesArrayIndex);
  116. $fileInfo2 = $this->generateUploadedFile($filesArrayIndex);
  117. $fileInfo3 = $this->generateUploadedFile($filesArrayIndex);
  118. $file1->setFileInfo($fileInfo);
  119. $file2->setFileInfo($fileInfo2);
  120. $file3->setFileInfo($fileInfo3);
  121. $this->em->persist($article);
  122. $this->em->flush();
  123. $art = $artRepo->findOneByTitle('Test');
  124. $files = $art->getFiles();
  125. $file1Path = $file1->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'];
  126. $file2Path = $file2->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'];
  127. $file3Path = $file3->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'];
  128. $this->assertEquals($file1Path, $files[0]->getFilePath());
  129. $this->assertEquals($file2Path, $files[1]->getFilePath());
  130. $this->assertEquals($file3Path, $files[2]->getFilePath());
  131. }
  132. /**
  133. * @expectedException Gedmo\Exception\UploadableNoPathDefinedException
  134. */
  135. public function testNoPathDefinedOnEntityOrListenerThrowsException()
  136. {
  137. $file = new FileWithoutPath();
  138. $fileInfo = $this->generateUploadedFile();
  139. $file->setFileInfo($fileInfo);
  140. $this->em->persist($file);
  141. $this->em->flush();
  142. }
  143. public function testNoPathDefinedOnEntityButDefinedOnListenerUsesDefaultPath()
  144. {
  145. // We set the default path on the listener
  146. $this->listener->setDefaultPath($this->destinationTestDir);
  147. $file = new FileWithoutPath();
  148. $fileInfo = $this->generateUploadedFile();
  149. $file->setFileInfo($fileInfo);
  150. $this->em->persist($file);
  151. $this->em->flush();
  152. $this->em->refresh($file);
  153. $this->assertEquals($this->destinationTestFile, $file->getFilePath());
  154. }
  155. public function testCallbackIsCalledIfItsSetOnEntity()
  156. {
  157. $file = new File();
  158. $fileInfo = $this->generateUploadedFile();
  159. $file->setFileInfo($fileInfo);
  160. $this->em->persist($file);
  161. $this->em->flush();
  162. $this->assertTrue($file->callbackWasCalled);
  163. }
  164. /**
  165. * @dataProvider uploadExceptionsProvider
  166. */
  167. public function testUploadExceptions($error, $exceptionClass)
  168. {
  169. $this->setExpectedException($exceptionClass);
  170. $file = new File();
  171. $fileInfo = $this->generateUploadedFile();
  172. $fileInfo['error'] = $error;
  173. $file->setFileInfo($fileInfo);
  174. $this->em->persist($file);
  175. $this->em->flush();
  176. }
  177. // Data Providers
  178. public function uploadExceptionsProvider()
  179. {
  180. return array(
  181. array(1, 'Gedmo\Exception\UploadableIniSizeException'),
  182. array(2, 'Gedmo\Exception\UploadableFormSizeException'),
  183. array(3, 'Gedmo\Exception\UploadablePartialException'),
  184. array(4, 'Gedmo\Exception\UploadableNoFileException'),
  185. array(6, 'Gedmo\Exception\UploadableNoTmpDirException'),
  186. array(7, 'Gedmo\Exception\UploadableCantWriteException'),
  187. array(8, 'Gedmo\Exception\UploadableExtensionException'),
  188. array(999, 'Gedmo\Exception\UploadableUploadException')
  189. );
  190. }
  191. // Util
  192. private function generateUploadedFile($index = 'image', $file = false, array $info = array())
  193. {
  194. if (empty($info)) {
  195. $info = array(
  196. 'tmp_name' => !$file ? $this->testFile : $file,
  197. 'name' => $this->testFilename,
  198. 'size' => $this->testFileSize,
  199. 'type' => $this->testFileMimeType,
  200. 'error' => 0
  201. );
  202. }
  203. return $info;
  204. }
  205. protected function getUsedEntityFixtures()
  206. {
  207. return array(
  208. self::IMAGE_CLASS,
  209. self::ARTICLE_CLASS,
  210. self::FILE_CLASS,
  211. self::FILE_WITHOUT_PATH_CLASS
  212. );
  213. }
  214. private function clearFilesAndDirectories()
  215. {
  216. if (is_file($this->destinationTestFile)) {
  217. unlink($this->destinationTestFile);
  218. }
  219. if (is_file($this->destinationTestFile2)) {
  220. unlink($this->destinationTestFile2);
  221. }
  222. if (is_dir($this->destinationTestDir)) {
  223. rmdir($this->destinationTestDir);
  224. }
  225. }
  226. }