FileFieldTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form;
  11. require_once __DIR__.'/TestCase.php';
  12. use Symfony\Component\Form\FileField;
  13. use Symfony\Component\HttpFoundation\File\File;
  14. class FileFieldTest extends TestCase
  15. {
  16. public static $tmpFiles = array();
  17. protected static $tmpDir;
  18. protected $field;
  19. public static function setUpBeforeClass()
  20. {
  21. self::$tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'symfony-test';
  22. }
  23. protected function setUp()
  24. {
  25. parent::setUp();
  26. $this->field = $this->factory->create('file', 'file');
  27. }
  28. protected function tearDown()
  29. {
  30. foreach (self::$tmpFiles as $key => $file) {
  31. @unlink($file);
  32. unset(self::$tmpFiles[$key]);
  33. }
  34. }
  35. public function createTmpFile($path)
  36. {
  37. self::$tmpFiles[] = $path;
  38. file_put_contents($path, 'foobar');
  39. }
  40. public function testSubmitUploadsNewFiles()
  41. {
  42. $tmpDir = self::$tmpDir;
  43. $generatedToken = '';
  44. $this->storage->expects($this->atLeastOnce())
  45. ->method('getTempDir')
  46. ->will($this->returnCallback(function ($token) use ($tmpDir, &$generatedToken) {
  47. // A 6-digit token is generated by FileUploader and passed
  48. // to getTempDir()
  49. $generatedToken = $token;
  50. return $tmpDir;
  51. }));
  52. $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $file->expects($this->once())
  56. ->method('move')
  57. ->with($this->equalTo($tmpDir));
  58. $file->expects($this->any())
  59. ->method('isValid')
  60. ->will($this->returnValue(true));
  61. $file->expects($this->any())
  62. ->method('getName')
  63. ->will($this->returnValue('original_name.jpg'));
  64. $file->expects($this->any())
  65. ->method('getPath')
  66. ->will($this->returnValue($tmpDir.'/original_name.jpg'));
  67. $this->field->bind(array(
  68. 'file' => $file,
  69. 'token' => '',
  70. 'name' => '',
  71. ));
  72. $this->assertRegExp('/^\d{6}$/', $generatedToken);
  73. $this->assertEquals(array(
  74. 'file' => $file,
  75. 'token' => $generatedToken,
  76. 'name' => 'original_name.jpg',
  77. ), $this->field->getClientData());
  78. $this->assertEquals($tmpDir.'/original_name.jpg', $this->field->getData());
  79. }
  80. public function testSubmitKeepsUploadedFilesOnErrors()
  81. {
  82. $tmpDir = self::$tmpDir;
  83. $tmpPath = $tmpDir . DIRECTORY_SEPARATOR . 'original_name.jpg';
  84. $this->createTmpFile($tmpPath);
  85. $this->storage->expects($this->atLeastOnce())
  86. ->method('getTempDir')
  87. ->with($this->equalTo('123456'))
  88. ->will($this->returnValue($tmpDir));
  89. $this->field->bind(array(
  90. 'file' => null,
  91. 'token' => '123456',
  92. 'name' => 'original_name.jpg',
  93. ));
  94. $this->assertTrue(file_exists($tmpPath));
  95. $file = new File($tmpPath);
  96. $this->assertEquals(array(
  97. 'file' => $file,
  98. 'token' => '123456',
  99. 'name' => 'original_name.jpg',
  100. ), $this->field->getClientData());
  101. $this->assertEquals($tmpPath, $this->field->getData());
  102. }
  103. public function testSubmitEmpty()
  104. {
  105. $this->storage->expects($this->never())
  106. ->method('getTempDir');
  107. $this->field->bind(array(
  108. 'file' => '',
  109. 'token' => '',
  110. 'name' => '',
  111. ));
  112. $this->assertEquals(array(
  113. 'file' => '',
  114. 'token' => '',
  115. 'name' => '',
  116. ), $this->field->getClientData());
  117. $this->assertEquals(null, $this->field->getData());
  118. }
  119. public function testSubmitEmptyKeepsExistingFiles()
  120. {
  121. $tmpPath = self::$tmpDir . DIRECTORY_SEPARATOR . 'original_name.jpg';
  122. $this->createTmpFile($tmpPath);
  123. $file = new File($tmpPath);
  124. $this->storage->expects($this->never())
  125. ->method('getTempDir');
  126. $this->field->setData($tmpPath);
  127. $this->field->bind(array(
  128. 'file' => '',
  129. 'token' => '',
  130. 'name' => '',
  131. ));
  132. $this->assertEquals(array(
  133. 'file' => $file,
  134. 'token' => '',
  135. 'name' => '',
  136. ), $this->field->getClientData());
  137. $this->assertEquals($tmpPath, $this->field->getData());
  138. }
  139. }