Skip to content

Commit

Permalink
Day 15: removed unused method, added tests for hitting missing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-bleih authored and gregurco committed Oct 8, 2019
1 parent 1d29e5f commit 67c5d1f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/Entity/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ public function setLogo($logo) : self
*/
public function getLogoPath(): ?string
{
return $this->getLogo() ? 'uploads/jobs/' . $this->getLogo()->getFilename() : null;
$logo = $this->getLogo();
if ($logo instanceof UploadedFile) {
return 'uploads/jobs/' . $logo->getFilename();
}

return $logo;
}

/**
Expand Down
17 changes: 0 additions & 17 deletions src/EventListener/JobUploadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function preUpdate(PreUpdateEventArgs $args)
$entity = $args->getEntity();

$this->uploadFile($entity);
$this->fileToString($entity);
}

/**
Expand Down Expand Up @@ -86,20 +85,4 @@ private function stringToFile($entity)
$entity->setLogo(new File($this->uploader->getTargetDirectory() . '/' . $fileName));
}
}

/**
* @param $entity
*/
private function fileToString($entity)
{
if (!$entity instanceof Job) {
return;
}

$logoFile = $entity->getLogo();

if ($logoFile instanceof File) {
$entity->setLogo($logoFile->getFilename());
}
}
}
21 changes: 21 additions & 0 deletions tests/Entity/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Entity\Job;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class JobTest extends TestCase
{
Expand Down Expand Up @@ -50,6 +51,26 @@ public function testGettersSettersAndDefaultValues(): void
$this->assertSame($category, $job->getCategory());
}

public function testLogoPathReturnsPathToFileOrNull(): void
{
$job = new Job();

$this->assertNull($job->getLogoPath());

$file = $this->getMockBuilder(UploadedFile::class)
->disableOriginalConstructor()
->getMock();
$file->expects($this->once())
->method('getFilename')
->willReturn('_FILE_NAME_');

$job->setLogo($file);
$this->assertEquals('uploads/jobs/_FILE_NAME_', $job->getLogoPath());

$job->setLogo('uploads/jobs/_OTHER_FILE_NAME_');
$this->assertEquals('uploads/jobs/_OTHER_FILE_NAME_', $job->getLogoPath());
}

/**
* @throws \Exception
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Service/FileUploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public function testCreateCategory(): void
$fileName = $uploader->upload($file);

$this->assertRegExp('/.+?\._EXT_/', $fileName);
$this->assertEquals('_DIR_', $uploader->getTargetDirectory());
}
}

0 comments on commit 67c5d1f

Please sign in to comment.