Skip to content

Commit

Permalink
update test for UploadedFile::moveTo
Browse files Browse the repository at this point in the history
Signed-off-by: katsuren <katsuren.houken@gmail.com>
  • Loading branch information
k2rn authored and Ocramius committed Jul 6, 2022
1 parent 6837922 commit d04b1ff
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions test/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,25 @@

class UploadedFileTest extends TestCase
{
protected $orgFile;

protected $tmpFile;

protected function setUp() : void
{
$this->tmpFile = null;
$this->orgFile = null;
}

protected function tearDown() : void
{
if (is_string($this->tmpFile) && file_exists($this->tmpFile)) {
unlink($this->tmpFile);
}

if (is_string($this->orgFile) && file_exists($this->orgFile)) {
unlink($this->orgFile);
}
}

public function invalidStreams()
Expand Down Expand Up @@ -144,9 +151,9 @@ public function testGetStreamReturnsStreamForFile()

public function testMovesFileToDesignatedPath()
{
$originalContents = 'Foo bar!';
$stream = new Stream('php://temp', 'wb+');
$stream->write('Foo bar!');
$originalContents = $stream->__toString();
$stream->write($originalContents);
$upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK);

$this->tmpFile = $to = tempnam(sys_get_temp_dir(), 'diac');
Expand Down Expand Up @@ -275,12 +282,13 @@ public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status)
*/
public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided()
{
$orgFile = tempnam(sys_get_temp_dir(), 'ORG');
$this->orgFile = tempnam(sys_get_temp_dir(), 'ORG');
$this->tmpFile = tempnam(sys_get_temp_dir(), 'DIA');
file_put_contents($this->orgFile, 'Hello');

$original = file_get_contents($orgFile);
$original = file_get_contents($this->orgFile);

$uploadedFile = new UploadedFile($orgFile, 100, UPLOAD_ERR_OK, basename($orgFile), 'text/plain');
$uploadedFile = new UploadedFile($this->orgFile, 100, UPLOAD_ERR_OK, basename($this->orgFile), 'text/plain');
$uploadedFile->moveTo($this->tmpFile);

$contents = file_get_contents($this->tmpFile);
Expand Down Expand Up @@ -326,13 +334,13 @@ public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDe

public function testMoveToInCLIShouldRemoveOriginalFile()
{
$orgFile = tempnam(sys_get_temp_dir(), 'ORG');
file_put_contents($orgFile, 'Foo bar!');
$upload = new UploadedFile($orgFile, 0, UPLOAD_ERR_OK);
$this->orgFile = tempnam(sys_get_temp_dir(), 'ORG');
file_put_contents($this->orgFile, 'Hello');
$upload = new UploadedFile($this->orgFile, 0, UPLOAD_ERR_OK);

$this->tmpFile = $to = tempnam(sys_get_temp_dir(), 'diac');
$upload->moveTo($to);
$this->assertFalse(file_exists($orgFile));
$this->assertFalse(file_exists($this->orgFile));
$this->assertTrue(file_exists($to));
}
}

0 comments on commit d04b1ff

Please sign in to comment.