UploadableEntityTest.php 8.7 KB

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