-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day 15: added unit test for entities
- Loading branch information
1 parent
163c402
commit 1d29e5f
Showing
10 changed files
with
312 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.