From 67c5d1f430362e8609081db700f20d6c58aade1d Mon Sep 17 00:00:00 2001 From: Sergey Bleih Date: Thu, 30 May 2019 18:12:19 +0300 Subject: [PATCH] Day 15: removed unused method, added tests for hitting missing coverage --- src/Entity/Job.php | 7 ++++++- src/EventListener/JobUploadListener.php | 17 ----------------- tests/Entity/JobTest.php | 21 +++++++++++++++++++++ tests/Service/FileUploaderTest.php | 1 + 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/Entity/Job.php b/src/Entity/Job.php index ce92b6e..9783d2a 100644 --- a/src/Entity/Job.php +++ b/src/Entity/Job.php @@ -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; } /** diff --git a/src/EventListener/JobUploadListener.php b/src/EventListener/JobUploadListener.php index a0c5541..ec14a4b 100644 --- a/src/EventListener/JobUploadListener.php +++ b/src/EventListener/JobUploadListener.php @@ -40,7 +40,6 @@ public function preUpdate(PreUpdateEventArgs $args) $entity = $args->getEntity(); $this->uploadFile($entity); - $this->fileToString($entity); } /** @@ -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()); - } - } } diff --git a/tests/Entity/JobTest.php b/tests/Entity/JobTest.php index 562585e..e7a4e58 100644 --- a/tests/Entity/JobTest.php +++ b/tests/Entity/JobTest.php @@ -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 { @@ -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 */ diff --git a/tests/Service/FileUploaderTest.php b/tests/Service/FileUploaderTest.php index f3bea36..f023027 100644 --- a/tests/Service/FileUploaderTest.php +++ b/tests/Service/FileUploaderTest.php @@ -31,5 +31,6 @@ public function testCreateCategory(): void $fileName = $uploader->upload($file); $this->assertRegExp('/.+?\._EXT_/', $fileName); + $this->assertEquals('_DIR_', $uploader->getTargetDirectory()); } }