Skip to content

Commit f4309d8

Browse files
wachterjohannesluca-rath
authored andcommitted
08 - Add a custom entity Location
1 parent df929f6 commit f4309d8

File tree

3 files changed

+244
-0
lines changed

3 files changed

+244
-0
lines changed

src/Entity/Location.php

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

src/Repository/LocationRepository.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\Common\Persistence\ManagerRegistry;
10+
11+
/**
12+
* @method Location|null find($id, $lockMode = null, $lockVersion = null)
13+
* @method Location|null findOneBy(array $criteria, array $orderBy = null)
14+
* @method Location[] findAll()
15+
* @method Location[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
16+
*
17+
* @extends ServiceEntityRepository<Location>
18+
*/
19+
class LocationRepository extends ServiceEntityRepository
20+
{
21+
public function __construct(ManagerRegistry $registry)
22+
{
23+
parent::__construct($registry, Location::class);
24+
}
25+
26+
public function create(): Location
27+
{
28+
$location = new Location();
29+
30+
$this->getEntityManager()->persist($location);
31+
32+
return $location;
33+
}
34+
}

tests/Unit/Entity/LocationTest.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
/**
13+
* @var Location
14+
*/
15+
private $location;
16+
17+
public function setUp(): void
18+
{
19+
$this->location = new Location();
20+
}
21+
22+
public function testName(): void
23+
{
24+
$this->assertNull($this->location->getName());
25+
$this->assertSame($this->location, $this->location->setName('Sulu GmbH'));
26+
$this->assertNotNull($this->location->getName());
27+
$this->assertSame('Sulu GmbH', $this->location->getName());
28+
}
29+
30+
public function testStreet(): void
31+
{
32+
$this->assertNull($this->location->getStreet());
33+
$this->assertSame($this->location, $this->location->setStreet('Teststreet'));
34+
$this->assertNotNull($this->location->getStreet());
35+
$this->assertSame('Teststreet', $this->location->getStreet());
36+
}
37+
38+
public function testNumber(): void
39+
{
40+
$this->assertNull($this->location->getNumber());
41+
$this->assertSame($this->location, $this->location->setNumber('42'));
42+
$this->assertNotNull($this->location->getNumber());
43+
$this->assertSame('42', $this->location->getNumber());
44+
}
45+
46+
public function testPostalCode(): void
47+
{
48+
$this->assertNull($this->location->getPostalCode());
49+
$this->assertSame($this->location, $this->location->setPostalCode('6850'));
50+
$this->assertNotNull($this->location->getPostalCode());
51+
$this->assertSame('6850', $this->location->getPostalCode());
52+
}
53+
54+
public function testCity(): void
55+
{
56+
$this->assertNull($this->location->getCity());
57+
$this->assertSame($this->location, $this->location->setCity('Dornbirn'));
58+
$this->assertNotNull($this->location->getCity());
59+
$this->assertSame('Dornbirn', $this->location->getCity());
60+
}
61+
62+
public function testCountryCode(): void
63+
{
64+
$this->assertNull($this->location->getCountryCode());
65+
$this->assertSame($this->location, $this->location->setCountryCode('AT'));
66+
$this->assertNotNull($this->location->getCountryCode());
67+
$this->assertSame('AT', $this->location->getCountryCode());
68+
}
69+
}

0 commit comments

Comments
 (0)