[BUGFIX] Unit tests call parent::tearDown()
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Http / UploadedFileTest.php
index 36aa7cb..bf5794b 100644 (file)
@@ -23,236 +23,262 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
  *
  * Adapted from https://github.com/phly/http/
  */
-class UploadedFileTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
+class UploadedFileTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+    protected $tmpFile;
 
-       protected $tmpFile;
+    protected function setUp()
+    {
+        $this->tmpFile = null;
+    }
 
-       protected function setUp() {
-               $this->tmpfile = NULL;
-       }
+    protected function tearDown()
+    {
+        if (is_scalar($this->tmpFile) && file_exists($this->tmpFile)) {
+            unlink($this->tmpFile);
+        }
+        parent::tearDown();
+    }
 
-       protected function tearDown() {
-               if (is_scalar($this->tmpFile) && file_exists($this->tmpFile)) {
-                       unlink($this->tmpFile);
-               }
-       }
+    /**
+     * @return array
+     */
+    public function invalidStreamsDataProvider()
+    {
+        return [
+            'null'   => [null],
+            'true'   => [true],
+            'false'  => [false],
+            'int'    => [1],
+            'float'  => [1.1],
+            /* Have not figured out a valid way to test an invalid path yet; null byte injection
+             * appears to get caught by fopen()
+            'invalid-path' => [ ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) ? '[:]' : 'foo' . chr(0) ],
+             */
+            'array'  => [['filename']],
+            'object' => [(object) ['filename']],
+        ];
+    }
 
-       /**
-        * @return array
-        */
-       public function invalidStreamsDataProvider() {
-               return [
-                       'null'   => [NULL],
-                       'true'   => [TRUE],
-                       'false'  => [FALSE],
-                       'int'    => [1],
-                       'float'  => [1.1],
-                       /* Have not figured out a valid way to test an invalid path yet; null byte injection
-                        * appears to get caught by fopen()
-                       'invalid-path' => [ ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) ? '[:]' : 'foo' . chr(0) ],
-                        */
-                       'array'  => [['filename']],
-                       'object' => [(object) ['filename']],
-               ];
-       }
+    /**
+     * @dataProvider invalidStreamsDataProvider
+     * @test
+     */
+    public function constructorRaisesExceptionOnInvalidStreamOrFile($streamOrFile)
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
+    }
 
-       /**
-        * @dataProvider invalidStreamsDataProvider
-        * @test
-        */
-       public function constructorRaisesExceptionOnInvalidStreamOrFile($streamOrFile) {
-               $this->setExpectedException('InvalidArgumentException');
-               new UploadedFile($streamOrFile, 0, UPLOAD_ERR_OK);
-       }
+    /**
+     * @return array
+     */
+    public function invalidSizesDataProvider()
+    {
+        return [
+            'null'   => [null],
+            'true'   => [true],
+            'false'  => [false],
+            'float'  => [1.1],
+            'string' => ['1'],
+            'array'  => [[1]],
+            'object' => [(object) [1]],
+        ];
+    }
 
-       /**
-        * @return array
-        */
-       public function invalidSizesDataProvider() {
-               return [
-                       'null'   => [NULL],
-                       'true'   => [TRUE],
-                       'false'  => [FALSE],
-                       'float'  => [1.1],
-                       'string' => ['1'],
-                       'array'  => [[1]],
-                       'object' => [(object) [1]],
-               ];
-       }
+    /**
+     * @dataProvider invalidSizesDataProvider
+     * @test
+     */
+    public function constructorRaisesExceptionOnInvalidSize($size)
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1436717302);
+        new UploadedFile(fopen('php://temp', 'wb+'), $size, UPLOAD_ERR_OK);
+    }
 
-       /**
-        * @dataProvider invalidSizesDataProvider
-        * @test
-        */
-       public function constructorRaisesExceptionOnInvalidSize($size) {
-               $this->setExpectedException('InvalidArgumentException', 'size');
-               new UploadedFile(fopen('php://temp', 'wb+'), $size, UPLOAD_ERR_OK);
-       }
+    /**
+     * @return array
+     */
+    public function invalidErrorStatusesDataProvider()
+    {
+        return [
+            'null'     => [null],
+            'true'     => [true],
+            'false'    => [false],
+            'float'    => [1.1],
+            'string'   => ['1'],
+            'array'    => [[1]],
+            'object'   => [(object) [1]],
+            'negative' => [-1],
+            'too-big'  => [9],
+        ];
+    }
 
-       /**
-        * @return array
-        */
-       public function invalidErrorStatusesDataProvider() {
-               return [
-                       'null'     => [NULL],
-                       'true'     => [TRUE],
-                       'false'    => [FALSE],
-                       'float'    => [1.1],
-                       'string'   => ['1'],
-                       'array'    => [[1]],
-                       'object'   => [(object) [1]],
-                       'negative' => [-1],
-                       'too-big'  => [9],
-               ];
-       }
+    /**
+     * @dataProvider invalidErrorStatusesDataProvider
+     * @test
+     */
+    public function constructorRaisesExceptionOnInvalidErrorStatus($status)
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1436717303);
+        new UploadedFile(fopen('php://temp', 'wb+'), 0, $status);
+    }
 
-       /**
-        * @dataProvider invalidErrorStatusesDataProvider
-        * @test
-        */
-       public function constructorRaisesExceptionOnInvalidErrorStatus($status) {
-               $this->setExpectedException('InvalidArgumentException', 'status');
-               new UploadedFile(fopen('php://temp', 'wb+'), 0, $status);
-       }
+    /**
+     * @return array
+     */
+    public function invalidFilenamesAndMediaTypesDataProvider()
+    {
+        return [
+            'true'   => [true],
+            'false'  => [false],
+            'int'    => [1],
+            'float'  => [1.1],
+            'array'  => [['string']],
+            'object' => [(object) ['string']],
+        ];
+    }
 
-       /**
-        * @return array
-        */
-       public function invalidFilenamesAndMediaTypesDataProvider() {
-               return [
-                       'true'   => [TRUE],
-                       'false'  => [FALSE],
-                       'int'    => [1],
-                       'float'  => [1.1],
-                       'array'  => [['string']],
-                       'object' => [(object) ['string']],
-               ];
-       }
+    /**
+     * @dataProvider invalidFilenamesAndMediaTypesDataProvider
+     * @test
+     */
+    public function constructorRaisesExceptionOnInvalidClientFilename($filename)
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1436717304);
+        new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, $filename);
+    }
 
-       /**
-        * @dataProvider invalidFilenamesAndMediaTypesDataProvider
-        * @test
-        */
-       public function constructorRaisesExceptionOnInvalidClientFilename($filename) {
-               $this->setExpectedException('InvalidArgumentException', 'filename');
-               new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, $filename);
-       }
+    /**
+     * @dataProvider invalidFilenamesAndMediaTypesDataProvider
+     * @test
+     */
+    public function constructorRaisesExceptionOnInvalidClientMediaType($mediaType)
+    {
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1436717305);
+        new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'foobar.baz', $mediaType);
+    }
 
-       /**
-        * @dataProvider invalidFilenamesAndMediaTypesDataProvider
-        * @test
-        */
-       public function constructorRaisesExceptionOnInvalidClientMediaType($mediaType) {
-               $this->setExpectedException('InvalidArgumentException', 'media type');
-               new UploadedFile(fopen('php://temp', 'wb+'), 0, UPLOAD_ERR_OK, 'foobar.baz', $mediaType);
-       }
+    /**
+     * @test
+     */
+    public function getStreamReturnsOriginalStreamObject()
+    {
+        $stream = new Stream('php://temp');
+        $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
+        $this->assertSame($stream, $upload->getStream());
+    }
 
-       /**
-        * @test
-        */
-       public function getStreamReturnsOriginalStreamObject() {
-               $stream = new Stream('php://temp');
-               $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
-               $this->assertSame($stream, $upload->getStream());
-       }
+    /**
+     * @test
+     */
+    public function getStreamReturnsWrappedPhpStream()
+    {
+        $stream = fopen('php://temp', 'wb+');
+        $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
+        $uploadStream = $upload->getStream()->detach();
+        $this->assertSame($stream, $uploadStream);
+    }
 
-       /**
-        * @test
-        */
-       public function getStreamReturnsWrappedPhpStream() {
-               $stream = fopen('php://temp', 'wb+');
-               $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
-               $uploadStream = $upload->getStream()->detach();
-               $this->assertSame($stream, $uploadStream);
-       }
+    /**
+     * @test
+     */
+    public function getStreamReturnsStreamForFile()
+    {
+        $this->tmpFile = $stream = tempnam(sys_get_temp_dir(), 'phly');
+        $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
+        $uploadStream = $upload->getStream();
+        $r = new \ReflectionProperty($uploadStream, 'stream');
+        $r->setAccessible(true);
+        $this->assertSame($stream, $r->getValue($uploadStream));
+    }
 
-       /**
-        * @test
-        */
-       public function getStreamReturnsStreamForFile() {
-               $this->tmpFile = $stream = tempnam(sys_get_temp_dir(), 'phly');
-               $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
-               $uploadStream = $upload->getStream();
-               $r = new \ReflectionProperty($uploadStream, 'stream');
-               $r->setAccessible(TRUE);
-               $this->assertSame($stream, $r->getValue($uploadStream));
-       }
+    /**
+     * @test
+     */
+    public function moveToMovesFileToDesignatedPath()
+    {
+        $stream = new Stream('php://temp', 'wb+');
+        $stream->write('Foo bar!');
+        $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
 
-       /**
-        * @test
-        */
-       public function moveToMovesFileToDesignatedPath() {
-               $stream = new Stream('php://temp', 'wb+');
-               $stream->write('Foo bar!');
-               $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
+        $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
+        $upload->moveTo($to);
+        $this->assertTrue(file_exists($to));
+        $contents = file_get_contents($to);
+        $this->assertEquals($stream->__toString(), $contents);
+    }
 
-               $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
-               $upload->moveTo($to);
-               $this->assertTrue(file_exists($to));
-               $contents = file_get_contents($to);
-               $this->assertEquals($stream->__toString(), $contents);
-       }
+    /**
+     * @return array
+     */
+    public function invalidMovePathsDataProvider()
+    {
+        return [
+            'null'   => [null],
+            'true'   => [true],
+            'false'  => [false],
+            'int'    => [1],
+            'float'  => [1.1],
+            'empty'  => [''],
+            'array'  => [['filename']],
+            'object' => [(object) ['filename']],
+        ];
+    }
 
-       /**
-        * @return array
-        */
-       public function invalidMovePathsDataProvider() {
-               return [
-                       'null'   => [NULL],
-                       'true'   => [TRUE],
-                       'false'  => [FALSE],
-                       'int'    => [1],
-                       'float'  => [1.1],
-                       'empty'  => [''],
-                       'array'  => [['filename']],
-                       'object' => [(object) ['filename']],
-               ];
-       }
+    /**
+     * @dataProvider invalidMovePathsDataProvider
+     * @test
+     */
+    public function moveToRaisesExceptionForInvalidPath($path)
+    {
+        $stream = new Stream('php://temp', 'wb+');
+        $stream->write('Foo bar!');
+        $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
 
-       /**
-        * @dataProvider invalidMovePathsDataProvider
-        * @test
-        */
-       public function moveToRaisesExceptionForInvalidPath($path) {
-               $stream = new Stream('php://temp', 'wb+');
-               $stream->write('Foo bar!');
-               $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
+        $this->tmpFile = $path;
+        $this->expectException(\InvalidArgumentException::class);
+        $this->expectExceptionCode(1436717307);
+        $upload->moveTo($path);
+    }
 
-               $this->tmpFile = $path;
-               $this->setExpectedException('InvalidArgumentException', 'path');
-               $upload->moveTo($path);
-       }
+    /**
+     * @test
+     */
+    public function moveToCannotBeCalledMoreThanOnce()
+    {
+        $stream = new Stream('php://temp', 'wb+');
+        $stream->write('Foo bar!');
+        $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
 
-       /**
-        * @test
-        */
-       public function moveToCannotBeCalledMoreThanOnce() {
-               $stream = new Stream('php://temp', 'wb+');
-               $stream->write('Foo bar!');
-               $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
+        $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
+        $upload->moveTo($to);
+        $this->assertTrue(file_exists($to));
 
-               $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
-               $upload->moveTo($to);
-               $this->assertTrue(file_exists($to));
+        $this->expectException(\RuntimeException::class);
+        $this->expectExceptionCode(1436717308);
+        $upload->moveTo($to);
+    }
 
-               $this->setExpectedException('RuntimeException', 'moved');
-               $upload->moveTo($to);
-       }
+    /**
+     * @test
+     */
+    public function getGetStreamRaisesExceptionAfterMove()
+    {
+        $stream = new Stream('php://temp', 'wb+');
+        $stream->write('Foo bar!');
+        $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
 
-       /**
-        * @test
-        */
-       public function getGetStreamRaisesExceptionAfterMove() {
-               $stream = new Stream('php://temp', 'wb+');
-               $stream->write('Foo bar!');
-               $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);
-
-               $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
-               $upload->moveTo($to);
-               $this->assertTrue(file_exists($to));
-
-               $this->setExpectedException('RuntimeException', 'moved');
-               $upload->getStream();
-       }
+        $this->tmpFile = $to = GeneralUtility::tempnam('psr7');
+        $upload->moveTo($to);
+        $this->assertTrue(file_exists($to));
 
+        $this->expectException(\RuntimeException::class);
+        $this->expectExceptionCode(1436717306);
+        $upload->getStream();
+    }
 }