-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix slug generation if the getRegenerateSlugOnUpdate method retu… (#472)
Fix slug generation if the getRegenerateSlugOnUpdate method return false
- Loading branch information
Showing
4 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
tests/Fixtures/Entity/SluggableWithoutRegenerateEntity.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Knp\DoctrineBehaviors\Tests\Fixtures\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface; | ||
use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait; | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
class SluggableWithoutRegenerateEntity implements SluggableInterface | ||
{ | ||
use SluggableTrait; | ||
|
||
/** | ||
* @ORM\Column(type="string") | ||
* @var string | ||
*/ | ||
private $name; | ||
|
||
/** | ||
* @ORM\Id | ||
* @ORM\Column(type="integer") | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
* @var int | ||
*/ | ||
private $id; | ||
|
||
public function getId(): int | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setName(string $name): void | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getSluggableFields(): array | ||
{ | ||
return ['name']; | ||
} | ||
|
||
private function getRegenerateSlugOnUpdate(): bool | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Knp\DoctrineBehaviors\Tests\ORM; | ||
|
||
use Doctrine\Common\Persistence\ObjectRepository; | ||
use Doctrine\ORM\EntityRepository; | ||
use Knp\DoctrineBehaviors\Tests\AbstractBehaviorTestCase; | ||
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\SluggableEntity; | ||
use Knp\DoctrineBehaviors\Tests\Fixtures\Entity\SluggableWithoutRegenerateEntity; | ||
|
||
final class SluggableWithoutRegenerateTest extends AbstractBehaviorTestCase | ||
{ | ||
/** | ||
* @var ObjectRepository|EntityRepository | ||
*/ | ||
private $sluggableWithoutRegenerateRepository; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->sluggableWithoutRegenerateRepository = $this->entityManager->getRepository( | ||
SluggableWithoutRegenerateEntity::class | ||
); | ||
} | ||
|
||
public function testSlugLoading(): void | ||
{ | ||
$entity = new SluggableWithoutRegenerateEntity(); | ||
$entity->setName('The name'); | ||
|
||
$this->entityManager->persist($entity); | ||
$this->entityManager->flush(); | ||
|
||
$id = $entity->getId(); | ||
$this->assertNotNull($id); | ||
|
||
$this->entityManager->clear(); | ||
|
||
/** @var SluggableEntity $entity */ | ||
$entity = $this->sluggableWithoutRegenerateRepository->find($id); | ||
|
||
$this->assertNotNull($entity); | ||
$this->assertSame('the-name', $entity->getSlug()); | ||
} | ||
|
||
public function testNotUpdatedSlug(): void | ||
{ | ||
$entity = new SluggableWithoutRegenerateEntity(); | ||
$entity->setName('The name'); | ||
|
||
$this->entityManager->persist($entity); | ||
$this->entityManager->flush(); | ||
|
||
$this->assertSame('the-name', $entity->getSlug()); | ||
|
||
$entity->setName('The name 2'); | ||
|
||
$this->entityManager->persist($entity); | ||
$this->entityManager->flush(); | ||
|
||
$this->assertSame('the-name', $entity->getSlug()); | ||
} | ||
} |