Skip to content

Commit 9268dc0

Browse files
committed
implement test and fix for #2990
1 parent eb53dfc commit 9268dc0

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

src/Sluggable/Handler/RelativeSlugHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ public function transliterate($text, $separator, $object)
126126
$this->originalTransliterator,
127127
[$slug, $separator, $object]
128128
);
129+
$slug = call_user_func_array(
130+
$this->sluggable->getUrlizer(),
131+
[$slug, $separator, $object]
132+
);
129133
}
130134

131135
$result = $slug.$this->usedOptions['separator'].$result;
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 <[email protected]> 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\Issue2990;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Gedmo\Mapping\Annotation as Gedmo;
17+
use Gedmo\Sluggable\Handler\InversedRelativeSlugHandler;
18+
use Gedmo\Sluggable\Sluggable;
19+
20+
/**
21+
* @ORM\Entity
22+
*/
23+
#[ORM\Entity]
24+
class Article implements Sluggable
25+
{
26+
/**
27+
* @var int|null
28+
*
29+
* @ORM\Id
30+
* @ORM\GeneratedValue
31+
* @ORM\Column(type="integer")
32+
*/
33+
#[ORM\Id]
34+
#[ORM\GeneratedValue]
35+
#[ORM\Column(type: Types::INTEGER)]
36+
private $id;
37+
38+
/**
39+
* @ORM\Column(name="title", type="string", length=64)
40+
*/
41+
#[ORM\Column(name: 'title', type: Types::STRING, length: 64)]
42+
private ?string $title = null;
43+
44+
/**
45+
* @var string|null
46+
*
47+
* @Gedmo\Slug(handlers={
48+
* @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\InversedRelativeSlugHandler", options={
49+
* @Gedmo\SlugHandlerOption(name="relationClass", value="Gedmo\Tests\Sluggable\Fixture\Handler\ArticleRelativeSlug"),
50+
* @Gedmo\SlugHandlerOption(name="mappedBy", value="article"),
51+
* @Gedmo\SlugHandlerOption(name="inverseSlugField", value="slug")
52+
* })
53+
* }, separator="-", updatable=true, fields={"title"})
54+
*
55+
* @ORM\Column(name="slug", type="string", length=64, unique=true)
56+
*/
57+
#[Gedmo\Slug(separator: '-', updatable: true, fields: ['title'])]
58+
#[Gedmo\SlugHandler(class: InversedRelativeSlugHandler::class, options: ['relationClass' => ArticleRelativeSlug::class, 'mappedBy' => 'article', 'inverseSlugField' => 'slug'])]
59+
#[ORM\Column(name: 'slug', type: Types::STRING, length: 64, unique: true)]
60+
private $slug;
61+
62+
public function getId(): ?int
63+
{
64+
return $this->id;
65+
}
66+
67+
public function setTitle(?string $title): void
68+
{
69+
$this->title = $title;
70+
}
71+
72+
public function getTitle(): ?string
73+
{
74+
return $this->title;
75+
}
76+
77+
public function setCode(?string $code): void
78+
{
79+
$this->code = $code;
80+
}
81+
82+
public function getCode(): ?string
83+
{
84+
return $this->code;
85+
}
86+
87+
public function getSlug(): ?string
88+
{
89+
return $this->slug;
90+
}
91+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 <[email protected]> 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\Issue2990;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Gedmo\Mapping\Annotation as Gedmo;
17+
use Gedmo\Sluggable\Handler\RelativeSlugHandler;
18+
19+
/**
20+
* @ORM\Entity
21+
*/
22+
#[ORM\Entity]
23+
class ArticleRelativeSlug
24+
{
25+
/**
26+
* @var int|null
27+
*
28+
* @ORM\Id
29+
* @ORM\GeneratedValue
30+
* @ORM\Column(type="integer")
31+
*/
32+
#[ORM\Id]
33+
#[ORM\GeneratedValue]
34+
#[ORM\Column(type: Types::INTEGER)]
35+
private $id;
36+
37+
/**
38+
* @ORM\Column(length=64)
39+
*/
40+
#[ORM\Column(length: 64)]
41+
private ?string $title = null;
42+
43+
/**
44+
* @var string|null
45+
*
46+
* @Gedmo\Slug(handlers={
47+
* @Gedmo\SlugHandler(class="Gedmo\Sluggable\Handler\RelativeSlugHandler", options={
48+
* @Gedmo\SlugHandlerOption(name="relationField", value="article"),
49+
* @Gedmo\SlugHandlerOption(name="relationSlugField", value="slug"),
50+
* @Gedmo\SlugHandlerOption(name="separator", value="/"),
51+
* @Gedmo\SlugHandlerOption(name="urilize", value=true)
52+
* })
53+
* }, separator="-", updatable=true, fields={"title"})
54+
*
55+
* @ORM\Column(name="slug", type="string", length=64, unique=true)
56+
*/
57+
#[Gedmo\Slug(separator: '-', updatable: true, fields: ['title'])]
58+
#[Gedmo\SlugHandler(class: RelativeSlugHandler::class, options: ['relationField' => 'article', 'relationSlugField' => 'title', 'separator' => '/', 'urilize' => true])]
59+
#[ORM\Column(name: 'slug', type: Types::STRING, length: 64, unique: true)]
60+
private $slug;
61+
62+
/**
63+
* @ORM\ManyToOne(targetEntity="Article")
64+
*/
65+
#[ORM\ManyToOne(targetEntity: Article::class)]
66+
private ?Article $article = null;
67+
68+
public function setArticle(?Article $article = null): void
69+
{
70+
$this->article = $article;
71+
}
72+
73+
public function getArticle(): ?Article
74+
{
75+
return $this->article;
76+
}
77+
78+
public function getId(): ?int
79+
{
80+
return $this->id;
81+
}
82+
83+
public function setTitle(?string $title): void
84+
{
85+
$this->title = $title;
86+
}
87+
88+
public function getTitle(): ?string
89+
{
90+
return $this->title;
91+
}
92+
93+
public function getSlug(): ?string
94+
{
95+
return $this->slug;
96+
}
97+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 <[email protected]> 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\Issue;
13+
14+
use Doctrine\Common\EventManager;
15+
use Gedmo\Sluggable\SluggableListener;
16+
use Gedmo\Tests\Sluggable\Fixture\Issue2990\Article;
17+
use Gedmo\Tests\Sluggable\Fixture\Issue2990\ArticleRelativeSlug;
18+
use Gedmo\Tests\Tool\BaseTestCaseORM;
19+
20+
/**
21+
* These are tests for sluggable behavior
22+
*
23+
* @author Gediminas Morkevicius <[email protected]>
24+
*/
25+
final class Issue2990Test extends BaseTestCaseORM
26+
{
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
31+
$evm = new EventManager();
32+
$evm->addEventSubscriber(new SluggableListener());
33+
34+
$this->getDefaultMockSqliteEntityManager($evm);
35+
}
36+
37+
/**
38+
* @group issue2990
39+
*/
40+
public function testShouldHandleUrilizeProperly(): void
41+
{
42+
$article = new Article();
43+
$article->setTitle('My Title');
44+
45+
$this->em->persist($article);
46+
$this->em->flush();
47+
48+
static::assertSame('my-title', $article->getSlug());
49+
50+
$relative = new ArticleRelativeSlug();
51+
$relative->setTitle('The Title');
52+
$relative->setArticle($article);
53+
54+
$this->em->persist($relative);
55+
$this->em->flush();
56+
57+
static::assertSame('my-title/the-title', $relative->getSlug());
58+
}
59+
60+
protected function getUsedEntityFixtures(): array
61+
{
62+
return [
63+
Article::class,
64+
ArticleRelativeSlug::class,
65+
];
66+
}
67+
}

0 commit comments

Comments
 (0)