Skip to content

Commit

Permalink
test: refactor: separate arrange, act, assert code
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 21, 2021
1 parent 3a7722c commit c0a8005
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions tests/system/HTTP/Files/FileMovingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ protected function tearDown(): void
public function testMove()
{
$finalFilename = 'fileA';

$_FILES = [
$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
Expand All @@ -79,7 +78,6 @@ public function testMove()
$this->assertTrue($collection->hasFile('userfile2'));

$destination = $this->destination;

// Create the destination if not exists
if (! is_dir($destination)) {
mkdir($destination, 0777, true);
Expand All @@ -97,8 +95,7 @@ public function testMove()
public function testMoveOverwriting()
{
$finalFilename = 'file_with_delimiters_underscore';

$_FILES = [
$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
Expand Down Expand Up @@ -129,7 +126,6 @@ public function testMoveOverwriting()
$this->assertTrue($collection->hasFile('userfile3'));

$destination = $this->destination;

// Create the destination if not exists
if (! is_dir($destination)) {
mkdir($destination, 0777, true);
Expand All @@ -149,8 +145,7 @@ public function testMoveOverwriting()
public function testMoved()
{
$finalFilename = 'fileA';

$_FILES = [
$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
Expand All @@ -165,7 +160,6 @@ public function testMoved()
$this->assertTrue($collection->hasFile('userfile1'));

$destination = $this->destination;

// Create the destination if not exists
if (! is_dir($destination)) {
mkdir($destination, 0777, true);
Expand All @@ -175,15 +169,16 @@ public function testMoved()

$this->assertInstanceOf(UploadedFile::class, $file);
$this->assertFalse($file->hasMoved());

$file->move($destination, $file->getName(), false);

$this->assertTrue($file->hasMoved());
}

public function testStore()
{
$finalFilename = 'fileA';

$_FILES = [
$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
Expand All @@ -198,7 +193,6 @@ public function testStore()
$this->assertTrue($collection->hasFile('userfile1'));

$destination = $this->destination;

// Create the destination if not exists
if (! is_dir($destination)) {
mkdir($destination, 0777, true);
Expand All @@ -207,15 +201,16 @@ public function testStore()
$file = $collection->getFile('userfile1');

$this->assertInstanceOf(UploadedFile::class, $file);

$path = $file->store($destination, $file->getName());

$this->assertSame($destination . '/fileA.txt', $path);
}

public function testAlreadyMoved()
{
$finalFilename = 'fileA';

$_FILES = [
$_FILES = [
'userfile1' => [
'name' => $finalFilename . '.txt',
'type' => 'text/plain',
Expand All @@ -230,7 +225,6 @@ public function testAlreadyMoved()
$this->assertTrue($collection->hasFile('userfile1'));

$destination = $this->destination;

// Create the destination if not exists
if (! is_dir($destination)) {
mkdir($destination, 0777, true);
Expand All @@ -257,11 +251,11 @@ public function testInvalidFile()
];

$destination = $this->destination;

$collection = new FileCollection();
$file = $collection->getFile('userfile');
$collection = new FileCollection();

$this->expectException(HTTPException::class);

$file = $collection->getFile('userfile');
$file->move($destination, $file->getName(), false);
}

Expand All @@ -278,16 +272,16 @@ public function testFailedMoveBecauseOfWarning()
];

$destination = $this->destination;

// Create the destination and make it read only
if (! is_dir($destination)) {
mkdir($destination, 0400, true);
}

$collection = new FileCollection();
$file = $collection->getFile('userfile');

$this->expectException(HTTPException::class);

$file = $collection->getFile('userfile');
$file->move($destination, $file->getName(), false);
}

Expand All @@ -308,20 +302,17 @@ public function testFailedMoveBecauseOfFalseReturned()
$this->assertTrue($collection->hasFile('userfile1'));

$destination = $this->destination;

// Create the destination if not exists
if (! is_dir($destination)) {
mkdir($destination, 0777, true);
}

$file = $collection->getFile('userfile1');

// Set the mock's return value to false
move_uploaded_file('', '', false);

$this->expectException(HTTPException::class);
$this->expectExceptionMessage('move_uploaded_file() returned false');

$file = $collection->getFile('userfile1');
$file->move($destination, $file->getName(), false);
}
}
Expand Down

0 comments on commit c0a8005

Please sign in to comment.