FileFieldTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. use Symfony\Component\HttpFoundation\File\File;
  4. use Symfony\Component\Form\FileField;
  5. class FileFieldTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public static $tmpFiles = array();
  8. protected static $tmpDir;
  9. public static function setUpBeforeClass()
  10. {
  11. self::$tmpDir = sys_get_temp_dir();
  12. // we need a session ID
  13. @session_start();
  14. }
  15. protected function tearDown()
  16. {
  17. foreach (self::$tmpFiles as $key => $file) {
  18. @unlink($file);
  19. unset(self::$tmpFiles[$key]);
  20. }
  21. }
  22. public function createTmpFile($path)
  23. {
  24. self::$tmpFiles[] = $path;
  25. file_put_contents($path, 'foobar');
  26. }
  27. public function testBindUploadsNewFiles()
  28. {
  29. $field = new FileField('file', array(
  30. 'secret' => '$secret$',
  31. 'tmp_dir' => self::$tmpDir,
  32. ));
  33. $tmpPath = realpath(self::$tmpDir) . '/' . md5(session_id() . '$secret$' . '12345');
  34. $that = $this;
  35. $file = $this->getMock('Symfony\Component\HttpFoundation\File\UploadedFile', array(), array(), '', false);
  36. $file->expects($this->once())
  37. ->method('move')
  38. ->with($this->equalTo($tmpPath))
  39. ->will($this->returnCallback(function ($path) use ($that) {
  40. $that->createTmpFile($path);
  41. }));
  42. $file->expects($this->any())
  43. ->method('getOriginalName')
  44. ->will($this->returnValue('original_name.jpg'));
  45. $field->bind(array(
  46. 'file' => $file,
  47. 'token' => '12345',
  48. 'original_name' => '',
  49. ));
  50. $this->assertTrue(file_exists($tmpPath));
  51. $this->assertEquals(array(
  52. 'file' => '',
  53. 'token' => '12345',
  54. 'original_name' => 'original_name.jpg',
  55. ), $field->getDisplayedData());
  56. $this->assertEquals($tmpPath, $field->getData());
  57. }
  58. public function testBindKeepsUploadedFilesOnErrors()
  59. {
  60. $field = new FileField('file', array(
  61. 'secret' => '$secret$',
  62. 'tmp_dir' => self::$tmpDir,
  63. ));
  64. $tmpPath = self::$tmpDir . '/' . md5(session_id() . '$secret$' . '12345');
  65. $this->createTmpFile($tmpPath);
  66. $field->bind(array(
  67. 'file' => '',
  68. 'token' => '12345',
  69. 'original_name' => 'original_name.jpg',
  70. ));
  71. $this->assertTrue(file_exists($tmpPath));
  72. $this->assertEquals(array(
  73. 'file' => '',
  74. 'token' => '12345',
  75. 'original_name' => 'original_name.jpg',
  76. ), $field->getDisplayedData());
  77. $this->assertEquals(realpath($tmpPath), realpath($field->getData()));
  78. }
  79. public function testBindKeepsOldFileIfNotOverwritten()
  80. {
  81. $field = new FileField('file', array(
  82. 'secret' => '$secret$',
  83. 'tmp_dir' => self::$tmpDir,
  84. ));
  85. $oldPath = tempnam(sys_get_temp_dir(), 'FileFieldTest');
  86. $this->createTmpFile($oldPath);
  87. $field->setData($oldPath);
  88. $this->assertEquals($oldPath, $field->getData());
  89. $field->bind(array(
  90. 'file' => '',
  91. 'token' => '12345',
  92. 'original_name' => '',
  93. ));
  94. $this->assertTrue(file_exists($oldPath));
  95. $this->assertEquals(array(
  96. 'file' => '',
  97. 'token' => '12345',
  98. 'original_name' => '',
  99. ), $field->getDisplayedData());
  100. $this->assertEquals($oldPath, $field->getData());
  101. }
  102. }