FileTest

The Gravity PDF FileTest class.

Defined (1)

The class is defined in the following location(s).

/vendor/codeguy/upload/tests/FileTest.php  
  1. class FileTest extends PHPUnit_Framework_TestCase 
  2. protected $assetsDirectory; 
  3.  
  4. protected $storage; 
  5.  
  6. /******************************************************************************** 
  7. * Setup 
  8. *******************************************************************************/ 
  9.  
  10. public function setUp() 
  11. $this->assetsDirectory = dirname(__FILE__) . '/assets'; 
  12. $_FILES['foo'] = array( 
  13. 'name' => 'foo.txt',  
  14. 'tmp_name' => $this->assetsDirectory . '/foo.txt',  
  15. 'error' => UPLOAD_ERR_OK 
  16. ); 
  17.  
  18. public function getNewFile() 
  19. if (is_null($this->storage)) { 
  20. // Prepare storage 
  21. $this->storage = $this->getMock( 
  22. '\Upload\Storage\FileSystem',  
  23. array('upload'),  
  24. array($this->assetsDirectory) 
  25. ); 
  26. $this->storage->expects($this->any()) 
  27. ->method('upload') 
  28. ->will($this->returnValue(true)); 
  29.  
  30. // Prepare file 
  31. $file = $this->getMock( 
  32. '\Upload\File',  
  33. array('isUploadedFile'),  
  34. array('foo', $this->storage) 
  35. ); 
  36. $file->expects($this->any()) 
  37. ->method('isUploadedFile') 
  38. ->will($this->returnValue(true)); 
  39.  
  40. return $file; 
  41.  
  42. /******************************************************************************** 
  43. * Tests 
  44. *******************************************************************************/ 
  45.  
  46. /** 
  47. * @expectedException \InvalidArgumentException 
  48. */ 
  49. public function testConstructionWithInvalidKey() 
  50. $file = new \Upload\File('bar', new \Upload\Storage\FileSystem($this->assetsDirectory)); 
  51.  
  52. public function testGetName() 
  53. $file = $this->getNewFile(); 
  54. $this->assertEquals('foo', $file->getName()); 
  55.  
  56. public function testGetNameWithExtension() 
  57. $file = $this->getNewFile(); 
  58. $this->assertEquals('foo.txt', $file->getNameWithExtension()); 
  59.  
  60. public function testGetNameWithExtensionUsingCustomName() 
  61. $file = $this->getNewFile(); 
  62. $file->setName('bar'); 
  63. $this->assertEquals('bar.txt', $file->getNameWithExtension()); 
  64.  
  65. public function testGetMimetype() 
  66. $file = $this->getNewFile(); 
  67. $this->assertEquals('text/plain', $file->getMimetype()); 
  68.  
  69. public function testAddValidationErrors() 
  70. $file = $this->getNewFile(); 
  71. $file->addError('Error'); 
  72. $this->assertEquals(1, count($file->getErrors())); 
  73.  
  74. public function testIsValidIfNoValidations() 
  75. $file = $this->getNewFile(); 
  76. $this->assertEmpty($file->getErrors()); 
  77.  
  78. public function testWillUploadIfNoValidations() 
  79. $file = $this->getNewFile(); 
  80. $this->assertTrue($file->upload()); 
  81.  
  82. public function testAddValidations() 
  83. $file = $this->getNewFile(); 
  84. $file->addValidations(new \Upload\Validation\Mimetype(array( 
  85. 'text/plain' 
  86. ))); 
  87. $this->assertEquals(1, count($file->getValidations())); 
  88.  
  89. public function testWillUploadWithPassingValidations() 
  90. $file = $this->getNewFile(); 
  91. $file->addValidations(new \Upload\Validation\Mimetype(array( 
  92. 'text/plain' 
  93. ))); 
  94. $this->assertTrue($file->upload()); 
  95.  
  96. /** 
  97. * @expectedException \Upload\Exception\UploadException 
  98. */ 
  99. public function testWillNotUploadWithFailingValidations() 
  100. $file = $this->getNewFile(); 
  101. $file->addValidations(new \Upload\Validation\Mimetype(array( 
  102. 'image/png' 
  103. ))); 
  104. $file->upload(); 
  105.  
  106. public function testPopulatesErrorsWithFailingValidations() 
  107. $file = $this->getNewFile(); 
  108. $file->addValidations(new \Upload\Validation\Mimetype(array( 
  109. 'image/png' 
  110. ))); 
  111. try { 
  112. $file->upload(); 
  113. } catch(\Upload\Exception\UploadException $e) { 
  114. $this->assertEquals(1, count($file->getErrors())); 
  115.  
  116. public function testValidationFailsIfUploadErrorCode() 
  117. $_FILES['foo']['error'] = 4; 
  118. $file = $this->getNewFile(); 
  119. $this->assertFalse($file->validate()); 
  120.  
  121. public function testValidationFailsIfNotUploadedFile() 
  122. $file = $this->getMock( 
  123. '\Upload\File',  
  124. array('isUploadedFile'),  
  125. array('foo', new \Upload\Storage\FileSystem($this->assetsDirectory)) 
  126. ); 
  127. $file->expects($this->any()) 
  128. ->method('isUploadedFile') 
  129. ->will($this->returnValue(false)); 
  130. $this->assertFalse($file->validate()); 
  131.  
  132. public function testParsesHumanFriendlyFileSizes() 
  133. $this->assertEquals(100, \Upload\File::humanReadableToBytes('100')); 
  134. $this->assertEquals(102400, \Upload\File::humanReadableToBytes('100K')); 
  135. $this->assertEquals(104857600, \Upload\File::humanReadableToBytes('100M')); 
  136. $this->assertEquals(107374182400, \Upload\File::humanReadableToBytes('100G')); 
  137. $this->assertEquals(100, \Upload\File::humanReadableToBytes('100F')); // <-- Unrecognized. Assume bytes.