Skip to content

Commit

Permalink
Day 15: added unit test for entities
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 163c402 commit 1d29e5f
Show file tree
Hide file tree
Showing 10 changed files with 312 additions and 16 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"friendsofsymfony/user-bundle": "~2.1",
"jms/serializer-bundle": "^3.1",
"knplabs/knp-paginator-bundle": "^3.0",
"mikey179/vfsStream": "^1.6",
"sensio/framework-extra-bundle": "^5.1",
"stof/doctrine-extensions-bundle": "^1.3",
"symfony/asset": "4.2.*",
Expand Down Expand Up @@ -38,7 +37,8 @@
"symfony/maker-bundle": "^1.0",
"symfony/profiler-pack": "*",
"symfony/test-pack": "*",
"symfony/web-server-bundle": "4.2.*"
"symfony/web-server-bundle": "4.2.*",
"mikey179/vfsStream": "^1.6"
},
"config": {
"preferred-install": {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:
- .:/application
- ./docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- 82:80
- 80:80

php-fpm:
build: docker/php-fpm
Expand Down
5 changes: 4 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@
<filter>
<whitelist>
<directory>./src/</directory>
<exclude>
<directory suffix=".php">src/DataFixtures</directory>
<directory suffix=".php">src/Migrations</directory>
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
</listeners>
Expand Down
8 changes: 6 additions & 2 deletions src/Entity/Affiliate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Entity;

use App\Entity\Traits\DateTimeAwareTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

Expand All @@ -12,6 +13,8 @@
*/
class Affiliate
{
use DateTimeAwareTrait;

/**
* @var int
*
Expand Down Expand Up @@ -201,9 +204,10 @@ public function removeCategory(Category $category) : self

/**
* @ORM\PrePersist
* @throws \Exception
*/
public function prePersist()
public function prePersist() : void
{
$this->createdAt = new \DateTime();
$this->createdAt = $this->getCurrentDateTime();
}
}
10 changes: 8 additions & 2 deletions src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Entity;

use App\Entity\Traits\DateTimeAwareTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
Expand All @@ -12,6 +13,8 @@
*/
class Category
{
use DateTimeAwareTrait;

/**
* @var int
*
Expand Down Expand Up @@ -95,10 +98,13 @@ public function getSlug() : ?string

/**
* @param string $slug
* @return self
*/
public function setSlug(string $slug): void
public function setSlug(string $slug): self
{
$this->slug = $slug;

return $this;
}

/**
Expand All @@ -115,7 +121,7 @@ public function getJobs()
public function getActiveJobs()
{
return $this->jobs->filter(function(Job $job) {
return $job->getExpiresAt() > new \DateTime() && $job->isActivated();
return $job->getExpiresAt() > $this->getCurrentDateTime() && $job->isActivated();
});
}

Expand Down
25 changes: 17 additions & 8 deletions src/Entity/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Entity;

use App\Entity\Traits\DateTimeAwareTrait;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use JMS\Serializer\Annotation as JMS;
Expand All @@ -15,6 +16,8 @@
*/
class Job
{
use DateTimeAwareTrait;

public const FULL_TIME_TYPE = 'full-time';
public const PART_TIME_TYPE = 'part-time';
public const FREELANCE_TYPE = 'freelance';
Expand Down Expand Up @@ -248,7 +251,7 @@ public function setLogo($logo) : self
*
* @return string
*/
public function getLogoPath()
public function getLogoPath(): ?string
{
return $this->getLogo() ? 'uploads/jobs/' . $this->getLogo()->getFilename() : null;
}
Expand Down Expand Up @@ -493,20 +496,25 @@ public function setCategory(Category $category) : self
* @JMS\VirtualProperty
* @JMS\SerializedName("category_name")
*
* @return string
* @return null|string
*/
public function getCategoryName()
public function getCategoryName(): ?string
{
if ($this->getCategory() === null) {
return null;
}

return $this->getCategory()->getName();
}

/**
* @ORM\PrePersist()
* @throws \Exception
*/
public function prePersist()
public function prePersist(): void
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
$this->createdAt = $this->getCurrentDateTime();
$this->updatedAt = $this->getCurrentDateTime();

if (!$this->expiresAt) {
$this->expiresAt = (clone $this->createdAt)->modify('+30 days');
Expand All @@ -515,9 +523,10 @@ public function prePersist()

/**
* @ORM\PreUpdate()
* @throws \Exception
*/
public function preUpdate()
public function preUpdate(): void
{
$this->updatedAt = new \DateTime();
$this->updatedAt = $this->getCurrentDateTime();
}
}
15 changes: 15 additions & 0 deletions src/Entity/Traits/DateTimeAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Entity\Traits;

trait DateTimeAwareTrait
{
/**
* @return \DateTime
* @throws \Exception
*/
public function getCurrentDateTime(): \DateTime
{
return new \DateTime();
}
}
69 changes: 69 additions & 0 deletions tests/Entity/AffiliateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Tests\Entity;

use App\Entity\Affiliate;
use App\Entity\Category;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;

class AffiliateTest extends TestCase
{
public function testGettersSettersAndDefaultValues(): void
{
$firstCategory = (new Category())
->setName('_FIRST_');
$secondCategory = (new Category())
->setName('_SECOND_');

$collection = new ArrayCollection([$firstCategory, $secondCategory]);

$affiliate = (new Affiliate())
->setToken('_TOKEN_')
->setActive(true)
->setEmail('[email protected]')
->setUrl('https://server.local')
->addCategory($firstCategory)
->addCategory($secondCategory);

$this->assertTrue($affiliate->isActive());
$this->assertEquals('_TOKEN_', $affiliate->getToken());
$this->assertEquals('https://server.local', $affiliate->getUrl());
$this->assertEquals('[email protected]', $affiliate->getEmail());
$this->assertNull($affiliate->getCreatedAt());
$this->assertNull($affiliate->getId());

$this->assertEquals($collection, $affiliate->getCategories());
}

public function testRemoveCategories(): void
{
$firstCategory = (new Category())
->setName('_FIRST_');
$secondCategory = (new Category())
->setName('_SECOND_');

$affiliate = (new Affiliate())
->addCategory($firstCategory)
->addCategory($secondCategory);

$affiliate
->removeCategory($firstCategory)
->removeCategory($secondCategory);

$this->assertTrue($affiliate->getCategories()->isEmpty());
}

/**
* @throws \Exception
*/
public function testPrePersistSetsCreatedAt(): void
{
$affiliate = new Affiliate();

$this->assertNull($affiliate->getCreatedAt());
$affiliate->prePersist();

$this->assertInstanceOf(\DateTime::class, $affiliate->getCreatedAt());
}
}
84 changes: 84 additions & 0 deletions tests/Entity/CategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Tests\Entity;

use App\Entity\Affiliate;
use App\Entity\Category;
use App\Entity\Job;
use Doctrine\Common\Collections\ArrayCollection;
use PHPUnit\Framework\TestCase;

class CategoryTest extends TestCase
{
public function testGettersSettersAndDefaultValues(): void
{
$category = (new Category())
->setName('_NAME_')
->setSlug('_SLUG_');

$this->assertEquals('_NAME_', $category->getName());
$this->assertEquals('_SLUG_', $category->getSlug());
$this->assertNull($category->getId());
}

public function testAffiliateCollectionManipulations(): void
{
$category = new Category();

$emptyCollection = new ArrayCollection();
$this->assertEquals($emptyCollection, $category->getAffiliates());

$first = (new Affiliate())
->setEmail('[email protected]');
$second = (new Affiliate())
->setEmail('[email protected]');
$category
->addAffiliate($first)
->addAffiliate($second);

$this->assertEquals(new ArrayCollection([$first, $second]), $category->getAffiliates());

$category
->removeAffiliate($first)
->removeAffiliate($second);

$this->assertEquals($emptyCollection, $category->getAffiliates());

}
public function testJobCollectionManipulations(): void
{
$category = new Category();

$emptyCollection = new ArrayCollection();
$this->assertEquals($emptyCollection, $category->getActiveJobs());
$this->assertEquals($emptyCollection, $category->getJobs());

$first = (new Job())
->setDescription('_FIRST_')
->setActivated(false)
->setExpiresAt(new \DateTime('+3 days'));
$second = (new Job())
->setDescription('_SECOND_')
->setActivated(true)
->setExpiresAt(new \DateTime('+3 days'));
$third = (new Job())
->setDescription('_THIRD_')
->setActivated(true)
->setExpiresAt(new \DateTime('-3 days'));
$category
->addJob($first)
->addJob($second)
->addJob($third);

$this->assertEquals(new ArrayCollection([$first, $second, $third]), $category->getJobs());
$this->assertEquals($second, $category->getActiveJobs()->first());
$this->assertEquals(1, $category->getActiveJobs()->count());

$category
->removeJob($first)
->removeJob($second)
->removeJob($third);

$this->assertEquals(new ArrayCollection(), $category->getAffiliates());
}
}
Loading

0 comments on commit 1d29e5f

Please sign in to comment.