-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) * Create entity class with association mappings and generate migration file * Define relationships between CourtOrder and Deputy * Add unit test for Deputy->CourtOrder association * Add integration test for Deputy->CourtOrder association * Update documentation on how tests interact with local databases * Set default value for discharged property and make deputy and court order properties not nullable --------- Co-authored-by: Elliot Smith <[email protected]>
- Loading branch information
1 parent
b1fd3da
commit 3facd11
Showing
7 changed files
with
278 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* Link table between court orders and deputies. | ||
* | ||
* @ORM\Table(name="court_order_deputy") | ||
* | ||
* @ORM\Entity() | ||
* | ||
* @ORM\HasLifecycleCallbacks() | ||
*/ | ||
class CourtOrderDeputy | ||
{ | ||
/** | ||
* @ORM\Id | ||
* | ||
* @ORM\ManyToOne(targetEntity="App\Entity\CourtOrder", inversedBy="courtOrderDeputyRelationships", cascade={"persist"}) | ||
* | ||
* @ORM\JoinColumn(name="court_order_id", referencedColumnName="id", nullable=false) | ||
*/ | ||
private CourtOrder $courtOrder; | ||
|
||
/** | ||
* @ORM\Id | ||
* | ||
* @ORM\ManyToOne(targetEntity="App\Entity\Deputy", inversedBy="courtOrderDeputyRelationships", cascade={"persist"}) | ||
* | ||
* @ORM\JoinColumn(name="deputy_id", referencedColumnName="id", nullable=false) | ||
*/ | ||
private Deputy $deputy; | ||
|
||
/** | ||
* @ORM\Column(name="discharged", type="boolean", nullable=false) | ||
*/ | ||
private bool $discharged; | ||
|
||
public function __construct() | ||
{ | ||
$this->discharged = false; | ||
} | ||
|
||
public function getDeputy(): Deputy | ||
{ | ||
return $this->deputy; | ||
} | ||
|
||
public function setDeputy(Deputy $deputy): CourtOrderDeputy | ||
{ | ||
$this->deputy = $deputy; | ||
|
||
return $this; | ||
} | ||
|
||
public function getCourtOrder(): CourtOrder | ||
{ | ||
return $this->courtOrder; | ||
} | ||
|
||
public function setCourtOrder(CourtOrder $courtOrder): CourtOrderDeputy | ||
{ | ||
$this->courtOrder = $courtOrder; | ||
|
||
return $this; | ||
} | ||
|
||
public function isDischarged(): bool | ||
{ | ||
return $this->discharged; | ||
} | ||
|
||
public function setDischarged(bool $discharged): CourtOrderDeputy | ||
{ | ||
$this->discharged = $discharged; | ||
|
||
return $this; | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineMigrations; | ||
|
||
use Doctrine\DBAL\Schema\Schema; | ||
use Doctrine\Migrations\AbstractMigration; | ||
|
||
/** | ||
* Auto-generated Migration: Please modify to your needs! | ||
*/ | ||
final class Version289 extends AbstractMigration | ||
{ | ||
public function getDescription(): string | ||
{ | ||
return 'Creates link table between court orders and deputies'; | ||
} | ||
|
||
public function up(Schema $schema): void | ||
{ | ||
// this up() migration is auto-generated, please modify it to your needs | ||
$this->addSql('CREATE TABLE court_order_deputy (court_order_id INT NOT NULL, deputy_id INT NOT NULL, discharged BOOLEAN NOT NULL, PRIMARY KEY(court_order_id, deputy_id))'); | ||
$this->addSql('CREATE INDEX IDX_994DD8A9A8D7D89C ON court_order_deputy (court_order_id)'); | ||
$this->addSql('CREATE INDEX IDX_994DD8A94B6F93BB ON court_order_deputy (deputy_id)'); | ||
$this->addSql('ALTER TABLE court_order_deputy ADD CONSTRAINT FK_994DD8A9A8D7D89C FOREIGN KEY (court_order_id) REFERENCES court_order (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); | ||
$this->addSql('ALTER TABLE court_order_deputy ADD CONSTRAINT FK_994DD8A94B6F93BB FOREIGN KEY (deputy_id) REFERENCES deputy (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); | ||
} | ||
|
||
public function down(Schema $schema): void | ||
{ | ||
// this down() migration is auto-generated, please modify it to your needs | ||
$this->addSql('ALTER TABLE court_order_deputy DROP CONSTRAINT FK_994DD8A9A8D7D89C'); | ||
$this->addSql('ALTER TABLE court_order_deputy DROP CONSTRAINT FK_994DD8A94B6F93BB'); | ||
$this->addSql('DROP TABLE court_order_deputy'); | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
namespace App\Tests\Unit\Entity; | ||
|
||
use App\Entity\CourtOrder; | ||
use App\Entity\Deputy; | ||
use App\TestHelpers\DeputyTestHelper; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Faker\Factory; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
class DeputyIntegrationTest extends KernelTestCase | ||
{ | ||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $em; | ||
|
||
protected function setUp(): void | ||
{ | ||
$kernel = self::bootKernel(); | ||
$this->em = $kernel->getContainer()->get('doctrine')->getManager(); | ||
} | ||
|
||
public function testGetCourtOrdersWithStatus() | ||
{ | ||
$faker = Factory::create(); | ||
$fakeUid = $faker->unique()->randomNumber(8); | ||
|
||
$deputyHelper = new DeputyTestHelper(); | ||
$deputy = $deputyHelper->generateDeputy(); | ||
$deputy->setLastname('MONK'); | ||
|
||
$courtOrder = new CourtOrder(); | ||
$courtOrder | ||
->setCourtOrderUid($fakeUid) | ||
->setType('hybrid') | ||
->setActive(true); | ||
|
||
$deputy->associateWithCourtOrder($courtOrder); | ||
|
||
$this->em->persist($courtOrder); | ||
$this->em->persist($deputy); | ||
$this->em->flush(); | ||
|
||
// retrieve the deputy from the db and check the association is populated correctly | ||
$repo = $this->em->getRepository(Deputy::class); | ||
$retrievedDeputy = $repo->findOneBy(['deputyUid' => $deputy->getDeputyUid()]); | ||
|
||
$actual = $retrievedDeputy->getCourtOrdersWithStatus(); | ||
$actualDischarged = $actual[0]['discharged']; | ||
$actualCourtOrder = $actual[0]['courtOrder']; | ||
|
||
$this->assertEquals(1, count($actual)); | ||
$this->assertEquals(false, $actualDischarged); | ||
$this->assertEquals($fakeUid, $actualCourtOrder->getCourtOrderUid()); | ||
$this->assertEquals('hybrid', $actualCourtOrder->getType()); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace App\Tests\Unit\Entity; | ||
|
||
use App\Entity\CourtOrder; | ||
use App\TestHelpers\DeputyTestHelper; | ||
use Faker\Factory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class DeputyTest extends TestCase | ||
{ | ||
public function testGetCourtOrdersWithStatus() | ||
{ | ||
$faker = Factory::create(); | ||
$fakeUid = $faker->unique()->randomNumber(8); | ||
|
||
$deputyHelper = new DeputyTestHelper(); | ||
$deputy = $deputyHelper->generateDeputy(); | ||
$deputy->setLastname('MONK'); | ||
|
||
$courtOrder = new CourtOrder(); | ||
$courtOrder | ||
->setCourtOrderUid($fakeUid) | ||
->setType('hybrid') | ||
->setActive(true); | ||
|
||
$deputy->associateWithCourtOrder($courtOrder); | ||
|
||
$actual = $deputy->getCourtOrdersWithStatus(); | ||
$actualDischarged = $actual[0]['discharged']; | ||
$actualCourtOrder = $actual[0]['courtOrder']; | ||
|
||
$this->assertEquals(1, count($actual)); | ||
$this->assertEquals(false, $actualDischarged); | ||
$this->assertEquals($fakeUid, $actualCourtOrder->getCourtOrderUid()); | ||
$this->assertEquals('hybrid', $actualCourtOrder->getType()); | ||
} | ||
} |
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