Skip to content

Commit f635af0

Browse files
wachterjohannesniklasnatter
authored andcommitted
08 - Add a custom entity Location
1 parent 1f369f7 commit f635af0

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

src/Entity/Location.php

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Entity;
6+
7+
use App\Repository\LocationRepository;
8+
use Doctrine\DBAL\Types\Types;
9+
use Doctrine\ORM\Mapping as ORM;
10+
11+
#[ORM\Entity(repositoryClass: LocationRepository::class)]
12+
class Location
13+
{
14+
#[ORM\Id]
15+
#[ORM\GeneratedValue]
16+
#[ORM\Column(type: Types::INTEGER)]
17+
private ?int $id = null;
18+
19+
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
20+
private ?string $name = null;
21+
22+
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
23+
private ?string $street = null;
24+
25+
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
26+
private ?string $number = null;
27+
28+
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
29+
private ?string $postalCode = null;
30+
31+
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
32+
private ?string $city = null;
33+
34+
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
35+
private ?string $countryCode = null;
36+
37+
public function getId(): ?int
38+
{
39+
return $this->id;
40+
}
41+
42+
public function getName(): ?string
43+
{
44+
return $this->name;
45+
}
46+
47+
public function setName(string $name): self
48+
{
49+
$this->name = $name;
50+
51+
return $this;
52+
}
53+
54+
public function getStreet(): ?string
55+
{
56+
return $this->street;
57+
}
58+
59+
public function setStreet(string $street): self
60+
{
61+
$this->street = $street;
62+
63+
return $this;
64+
}
65+
66+
public function getNumber(): ?string
67+
{
68+
return $this->number;
69+
}
70+
71+
public function setNumber(string $number): self
72+
{
73+
$this->number = $number;
74+
75+
return $this;
76+
}
77+
78+
public function getPostalCode(): ?string
79+
{
80+
return $this->postalCode;
81+
}
82+
83+
public function setPostalCode(string $postalCode): self
84+
{
85+
$this->postalCode = $postalCode;
86+
87+
return $this;
88+
}
89+
90+
public function getCity(): ?string
91+
{
92+
return $this->city;
93+
}
94+
95+
public function setCity(string $city): self
96+
{
97+
$this->city = $city;
98+
99+
return $this;
100+
}
101+
102+
public function getCountryCode(): ?string
103+
{
104+
return $this->countryCode;
105+
}
106+
107+
public function setCountryCode(string $countryCode): self
108+
{
109+
$this->countryCode = $countryCode;
110+
111+
return $this;
112+
}
113+
}

src/Repository/LocationRepository.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Repository;
6+
7+
use App\Entity\Location;
8+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
9+
use Doctrine\Persistence\ManagerRegistry;
10+
11+
/**
12+
* @extends ServiceEntityRepository<Location>
13+
*/
14+
class LocationRepository extends ServiceEntityRepository
15+
{
16+
public function __construct(ManagerRegistry $registry)
17+
{
18+
parent::__construct($registry, Location::class);
19+
}
20+
21+
public function create(): Location
22+
{
23+
$location = new Location();
24+
25+
$this->getEntityManager()->persist($location);
26+
27+
return $location;
28+
}
29+
}

tests/Unit/Entity/LocationTest.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Tests\Unit\Entity;
6+
7+
use App\Entity\Location;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class LocationTest extends TestCase
11+
{
12+
private Location $location;
13+
14+
protected function setUp(): void
15+
{
16+
$this->location = new Location();
17+
}
18+
19+
public function testName(): void
20+
{
21+
$this->assertNull($this->location->getName());
22+
$this->assertSame($this->location, $this->location->setName('Sulu GmbH'));
23+
$this->assertNotNull($this->location->getName());
24+
$this->assertSame('Sulu GmbH', $this->location->getName());
25+
}
26+
27+
public function testStreet(): void
28+
{
29+
$this->assertNull($this->location->getStreet());
30+
$this->assertSame($this->location, $this->location->setStreet('Teststreet'));
31+
$this->assertNotNull($this->location->getStreet());
32+
$this->assertSame('Teststreet', $this->location->getStreet());
33+
}
34+
35+
public function testNumber(): void
36+
{
37+
$this->assertNull($this->location->getNumber());
38+
$this->assertSame($this->location, $this->location->setNumber('42'));
39+
$this->assertNotNull($this->location->getNumber());
40+
$this->assertSame('42', $this->location->getNumber());
41+
}
42+
43+
public function testPostalCode(): void
44+
{
45+
$this->assertNull($this->location->getPostalCode());
46+
$this->assertSame($this->location, $this->location->setPostalCode('6850'));
47+
$this->assertNotNull($this->location->getPostalCode());
48+
$this->assertSame('6850', $this->location->getPostalCode());
49+
}
50+
51+
public function testCity(): void
52+
{
53+
$this->assertNull($this->location->getCity());
54+
$this->assertSame($this->location, $this->location->setCity('Dornbirn'));
55+
$this->assertNotNull($this->location->getCity());
56+
$this->assertSame('Dornbirn', $this->location->getCity());
57+
}
58+
59+
public function testCountryCode(): void
60+
{
61+
$this->assertNull($this->location->getCountryCode());
62+
$this->assertSame($this->location, $this->location->setCountryCode('AT'));
63+
$this->assertNotNull($this->location->getCountryCode());
64+
$this->assertSame('AT', $this->location->getCountryCode());
65+
}
66+
}

0 commit comments

Comments
 (0)