Skip to content

Commit f33018c

Browse files
committed
Add sluggable test with embeddable
1 parent 5986b34 commit f33018c

3 files changed

Lines changed: 237 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\Sluggable\Fixture\Embeddable;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
17+
/**
18+
* @ORM\Embeddable
19+
*/
20+
#[ORM\Embeddable]
21+
class Address
22+
{
23+
/**
24+
* @ORM\Column(name="street", type="string", length=64)
25+
*/
26+
#[ORM\Column(name: 'street', type: Types::STRING, length: 64)]
27+
private ?string $street = null;
28+
29+
/**
30+
* @ORM\Column(name="postalCode", type="string", length=64)
31+
*/
32+
#[ORM\Column(name: 'postalCode', type: Types::STRING, length: 64)]
33+
private ?string $postalCode = null;
34+
35+
/**
36+
* @ORM\Column(name="city", type="string", length=64)
37+
*/
38+
#[ORM\Column(name: 'city', type: Types::STRING, length: 64)]
39+
private ?string $city = null;
40+
41+
/**
42+
* @ORM\Column(name="country", type="string", length=64)
43+
*/
44+
#[ORM\Column(name: 'country', type: Types::STRING, length: 64)]
45+
private ?string $country = null;
46+
47+
public function getStreet(): ?string
48+
{
49+
return $this->street;
50+
}
51+
52+
public function setStreet(?string $street): void
53+
{
54+
$this->street = $street;
55+
}
56+
57+
public function getPostalCode(): ?string
58+
{
59+
return $this->postalCode;
60+
}
61+
62+
public function setPostalCode(?string $postalCode): void
63+
{
64+
$this->postalCode = $postalCode;
65+
}
66+
67+
public function getCity(): ?string
68+
{
69+
return $this->city;
70+
}
71+
72+
public function setCity(?string $city): void
73+
{
74+
$this->city = $city;
75+
}
76+
77+
public function getCountry(): ?string
78+
{
79+
return $this->country;
80+
}
81+
82+
public function setCountry(?string $country): void
83+
{
84+
$this->country = $country;
85+
}
86+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Tests\Sluggable\Fixture\Embeddable;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Doctrine\ORM\Mapping\Embedded;
17+
use Gedmo\Mapping\Annotation as Gedmo;
18+
use Gedmo\Sluggable\Sluggable;
19+
20+
/**
21+
* @ORM\Entity
22+
*/
23+
#[ORM\Entity]
24+
class User implements Sluggable
25+
{
26+
/**
27+
* @ORM\Id
28+
* @ORM\GeneratedValue
29+
* @ORM\Column(type="integer")
30+
*/
31+
#[ORM\Id]
32+
#[ORM\GeneratedValue]
33+
#[ORM\Column(type: Types::INTEGER)]
34+
private ?int $id = null;
35+
36+
/**
37+
* @ORM\Column(name="username", type="string", length=64)
38+
*/
39+
#[ORM\Column(name: 'username', type: Types::STRING, length: 64)]
40+
private ?string $username = null;
41+
42+
/**
43+
* @Gedmo\Slug(separator="-", updatable=true, fields={"username", "address.city", "address.country"})
44+
*
45+
* @ORM\Column(name="slug", type="string", length=64, unique=true)
46+
*/
47+
#[Gedmo\Slug(separator: '-', updatable: true, fields: ['username', 'address.city', 'address.country'])]
48+
#[ORM\Column(name: 'slug', type: Types::STRING, length: 64, unique: true)]
49+
private ?string $slug = null;
50+
51+
/**
52+
* @ORM\Embeddable(class=Address::class)
53+
*/
54+
#[Embedded(class: Address::class)]
55+
private Address $address;
56+
57+
public function __construct()
58+
{
59+
$this->address = new Address();
60+
}
61+
62+
public function getId(): ?int
63+
{
64+
return $this->id;
65+
}
66+
67+
public function getUsername(): ?string
68+
{
69+
return $this->username;
70+
}
71+
72+
public function setUsername(?string $username): void
73+
{
74+
$this->username = $username;
75+
}
76+
77+
public function setSlug(?string $slug): void
78+
{
79+
$this->slug = $slug;
80+
}
81+
82+
public function getSlug(): ?string
83+
{
84+
return $this->slug;
85+
}
86+
87+
public function getAddress(): Address
88+
{
89+
return $this->address;
90+
}
91+
92+
public function setAddress(Address $address): void
93+
{
94+
$this->address = $address;
95+
}
96+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Doctrine\Common\EventManager;
13+
use Gedmo\Sluggable\SluggableListener;
14+
use Gedmo\Tests\Sluggable\Fixture\Embeddable\Address;
15+
use Gedmo\Tests\Sluggable\Fixture\Embeddable\User;
16+
use Gedmo\Tests\Tool\BaseTestCaseORM;
17+
18+
final class SluggableEmbeddableTest extends BaseTestCaseORM
19+
{
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$evm = new EventManager();
25+
$evm->addEventSubscriber(new SluggableListener());
26+
27+
$this->getDefaultMockSqliteEntityManager($evm);
28+
}
29+
30+
public function testShouldHandleSlugWithEmbeddable(): void
31+
{
32+
$address = new Address();
33+
$address->setStreet('street');
34+
$address->setCity('city');
35+
$address->setPostalCode('postal code');
36+
$address->setCountry('country');
37+
38+
$user = new User();
39+
$user->setUsername('username');
40+
$user->setAddress($address);
41+
42+
$this->em->persist($user);
43+
$this->em->flush();
44+
$this->em->clear();
45+
46+
static::assertSame('username-city-country', $user->getSlug());
47+
}
48+
49+
protected function getUsedEntityFixtures(): array
50+
{
51+
return [
52+
User::class,
53+
];
54+
}
55+
}

0 commit comments

Comments
 (0)