Skip to content

Commit

Permalink
Fix slug generation if the getRegenerateSlugOnUpdate method retu… (#472)
Browse files Browse the repository at this point in the history
Fix slug generation if the getRegenerateSlugOnUpdate method return false
  • Loading branch information
TomasVotruba authored Dec 28, 2019
2 parents c61202c + 657905a commit 6209178
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions ecs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ parameters:
# & bug
- "*Repository.php"

SlevomatCodingStandard\Sniffs\Classes\UnusedPrivateElementsSniff:
- "tests/Fixtures/Entity/SluggableWithoutRegenerateEntity.php"

services:
Symplify\CodingStandard\Sniffs\CleanCode\CognitiveComplexitySniff:
max_cognitive_complexity: 8
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Sluggable/SluggableMethodsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getSlug(): string
*/
public function generateSlug(): void
{
if (! $this->getRegenerateSlugOnUpdate()) {
if ($this->slug !== null && $this->getRegenerateSlugOnUpdate() === false) {
return;
}

Expand Down
59 changes: 59 additions & 0 deletions tests/Fixtures/Entity/SluggableWithoutRegenerateEntity.php
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;
}
}
66 changes: 66 additions & 0 deletions tests/ORM/SluggableWithoutRegenerateTest.php
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());
}
}

0 comments on commit 6209178

Please sign in to comment.