Skip to content

Commit a40ff24

Browse files
08 - Add a custom entity Location
1 parent f1c527c commit a40ff24

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

src/Entity/Location.php

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
* @ORM\Id()
16+
* @ORM\GeneratedValue()
17+
* @ORM\Column(type="integer")
18+
*/
19+
private $id;
20+
21+
/**
22+
* @ORM\Column(type="string", length=255)
23+
*/
24+
private $name;
25+
26+
/**
27+
* @ORM\Column(type="string", length=255)
28+
*/
29+
private $street;
30+
31+
/**
32+
* @ORM\Column(type="string", length=255)
33+
*/
34+
private $number;
35+
36+
/**
37+
* @ORM\Column(type="string", length=255)
38+
*/
39+
private $postalCode;
40+
41+
/**
42+
* @ORM\Column(type="string", length=255)
43+
*/
44+
private $city;
45+
46+
/**
47+
* @ORM\Column(type="string", length=255)
48+
*/
49+
private $countryCode;
50+
51+
public function getId(): ?int
52+
{
53+
return $this->id;
54+
}
55+
56+
public function getName(): ?string
57+
{
58+
return $this->name;
59+
}
60+
61+
public function setName(string $name): self
62+
{
63+
$this->name = $name;
64+
65+
return $this;
66+
}
67+
68+
public function getStreet(): ?string
69+
{
70+
return $this->street;
71+
}
72+
73+
public function setStreet(string $street): self
74+
{
75+
$this->street = $street;
76+
77+
return $this;
78+
}
79+
80+
public function getNumber(): ?string
81+
{
82+
return $this->number;
83+
}
84+
85+
public function setNumber(string $number): self
86+
{
87+
$this->number = $number;
88+
89+
return $this;
90+
}
91+
92+
public function getPostalCode(): ?string
93+
{
94+
return $this->postalCode;
95+
}
96+
97+
public function setPostalCode(string $postalCode): self
98+
{
99+
$this->postalCode = $postalCode;
100+
101+
return $this;
102+
}
103+
104+
public function getCity(): ?string
105+
{
106+
return $this->city;
107+
}
108+
109+
public function setCity(string $city): self
110+
{
111+
$this->city = $city;
112+
113+
return $this;
114+
}
115+
116+
public function getCountryCode(): ?string
117+
{
118+
return $this->countryCode;
119+
}
120+
121+
public function setCountryCode(string $countryCode): self
122+
{
123+
$this->countryCode = $countryCode;
124+
125+
return $this;
126+
}
127+
}

src/Repository/LocationRepository.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
class LocationRepository extends ServiceEntityRepository
18+
{
19+
public function __construct(ManagerRegistry $registry)
20+
{
21+
parent::__construct($registry, Location::class);
22+
}
23+
24+
public function create(): Location
25+
{
26+
$location = new Location();
27+
28+
$this->getEntityManager()->persist($location);
29+
30+
return $location;
31+
}
32+
}

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)