-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Timestampable] [SoftDeleteable] Add support date point type #3013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
IndraGunawan
wants to merge
5
commits into
doctrine-extensions:main
Choose a base branch
from
IndraGunawan:add-support-date-point
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f53f19f
[Timestampable] Add support for date_point type
IndraGunawan abd3966
[SoftDeleteable] add support for date_point type
IndraGunawan c03bf09
add changelog
IndraGunawan 1149f14
Merge branch 'main' into add-support-date-point
IndraGunawan 571f7ed
check date_point type availability
IndraGunawan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ class Validator | |
| 'datetimetz', | ||
| 'datetimetz_immutable', | ||
| 'timestamp', | ||
| 'date_point', | ||
| ]; | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,85 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Doctrine Behavioral Extensions package. | ||
| * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Gedmo\Tests\SoftDeleteable; | ||
|
|
||
| use Carbon\Doctrine\DateTimeType; | ||
| use Doctrine\Common\EventManager; | ||
| use Doctrine\DBAL\Types\Type as DoctrineType; | ||
| use Doctrine\DBAL\Types\Types; | ||
| use Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter; | ||
| use Gedmo\SoftDeleteable\SoftDeleteableListener; | ||
| use Gedmo\Tests\SoftDeleteable\Fixture\Entity\BookDatePoint; | ||
| use Gedmo\Tests\Tool\BaseTestCaseORM; | ||
| use Symfony\Component\Clock\DatePoint; | ||
|
|
||
| final class DatePointTest extends BaseTestCaseORM | ||
| { | ||
| private const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable'; | ||
|
|
||
| private SoftDeleteableListener $softDeleteableListener; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $evm = new EventManager(); | ||
| $this->softDeleteableListener = new SoftDeleteableListener(); | ||
| $evm->addEventSubscriber($this->softDeleteableListener); | ||
| $config = $this->getDefaultConfiguration(); | ||
| $config->addFilter(self::SOFT_DELETEABLE_FILTER_NAME, SoftDeleteableFilter::class); | ||
| $this->em = $this->getDefaultMockSqliteEntityManager($evm, $config); | ||
| $this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME); | ||
|
|
||
| DoctrineType::overrideType(Types::DATETIME_MUTABLE, DateTimeType::class); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| parent::tearDown(); | ||
|
|
||
| DoctrineType::overrideType(Types::DATETIME_MUTABLE, \Doctrine\DBAL\Types\DateTimeType::class); | ||
| } | ||
|
|
||
| public function testSoftDeleteable(): void | ||
| { | ||
| $repo = $this->em->getRepository(BookDatePoint::class); | ||
|
|
||
| $book0 = new BookDatePoint(); | ||
| $field = 'title'; | ||
| $value = 'Title 1'; | ||
| $book0->setTitle($value); | ||
|
|
||
| $this->em->persist($book0); | ||
| $this->em->flush(); | ||
|
|
||
| static::assertNull($book0->getDeletedAt()); | ||
|
|
||
| $book = $repo->findOneBy([$field => $value]); | ||
| $this->em->remove($book); | ||
| $this->em->flush(); | ||
|
|
||
| $book = $repo->findOneBy([$field => $value]); | ||
| static::assertNull($book); | ||
|
|
||
| // Now we deactivate the filter so we test if the entity appears in the result | ||
| $this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME); | ||
|
|
||
| $book = $repo->findOneBy([$field => $value]); | ||
| static::assertIsObject($book); | ||
| static::assertInstanceOf(DatePoint::class, $book->getDeletedAt()); | ||
| } | ||
|
|
||
| protected function getUsedEntityFixtures(): array | ||
| { | ||
| return [ | ||
| BookDatePoint::class, | ||
| ]; | ||
| } | ||
| } | ||
76 changes: 76 additions & 0 deletions
76
tests/Gedmo/SoftDeleteable/Fixture/Entity/BookDatePoint.php
This file contains hidden or 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,76 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of the Doctrine Behavioral Extensions package. | ||
| * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Gedmo\Tests\SoftDeleteable\Fixture\Entity; | ||
|
|
||
| use Doctrine\DBAL\Types\Types; | ||
| use Doctrine\ORM\Mapping as ORM; | ||
| use Gedmo\Mapping\Annotation as Gedmo; | ||
| use Symfony\Component\Clock\DatePoint; | ||
|
|
||
| /** | ||
| * @ORM\Entity | ||
| * | ||
| * @Gedmo\SoftDeleteable(fieldName="deletedAt") | ||
| */ | ||
| #[ORM\Entity] | ||
| #[Gedmo\SoftDeleteable(fieldName: 'deletedAt')] | ||
| class BookDatePoint | ||
| { | ||
| /** | ||
| * @var int|null | ||
| * | ||
| * @ORM\Id | ||
| * @ORM\GeneratedValue(strategy="IDENTITY") | ||
| * @ORM\Column(type="integer") | ||
| */ | ||
| #[ORM\Id] | ||
| #[ORM\GeneratedValue(strategy: 'IDENTITY')] | ||
| #[ORM\Column(type: Types::INTEGER)] | ||
| private $id; | ||
|
|
||
| /** | ||
| * @ORM\Column(name="title", type="string") | ||
| */ | ||
| #[ORM\Column(name: 'title', type: Types::STRING)] | ||
| private ?string $title = null; | ||
|
|
||
| /** | ||
| * @ORM\Column(name="deletedAt", type="date_point", nullable=true) | ||
| */ | ||
| #[ORM\Column(name: 'deletedAt', type: 'date_point', nullable: true)] | ||
| private ?DatePoint $deletedAt = null; | ||
|
|
||
| public function getId(): ?int | ||
| { | ||
| return $this->id; | ||
| } | ||
|
|
||
| public function setTitle(?string $title): void | ||
| { | ||
| $this->title = $title; | ||
| } | ||
|
|
||
| public function getTitle(): ?string | ||
| { | ||
| return $this->title; | ||
| } | ||
|
|
||
| public function setDeletedAt(?DatePoint $deletedAt): void | ||
| { | ||
| $this->deletedAt = $deletedAt; | ||
| } | ||
|
|
||
| public function getDeletedAt(): ?DatePoint | ||
| { | ||
| return $this->deletedAt; | ||
| } | ||
| } |
This file contains hidden or 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,135 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Doctrine Behavioral Extensions package. | ||
| * (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Gedmo\Tests\Timestampable; | ||
|
|
||
| use Doctrine\Common\EventManager; | ||
| use Gedmo\Tests\Timestampable\Fixture\ArticleDatePoint; | ||
| use Gedmo\Tests\Timestampable\Fixture\Author; | ||
| use Gedmo\Tests\Timestampable\Fixture\CommentDatePoint; | ||
| use Gedmo\Tests\Timestampable\Fixture\Type; | ||
| use Gedmo\Tests\Tool\BaseTestCaseORM; | ||
| use Gedmo\Timestampable\TimestampableListener; | ||
| use Symfony\Component\Clock\DatePoint; | ||
| use Symfony\Component\Clock\NativeClock; | ||
|
|
||
| final class DatePointTest extends BaseTestCaseORM | ||
|
IndraGunawan marked this conversation as resolved.
|
||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $evm = new EventManager(); | ||
| $evm->addEventSubscriber(new TimestampableListener()); | ||
|
|
||
| $this->getDefaultMockSqliteEntityManager($evm); | ||
| } | ||
|
|
||
| protected function tearDown(): void | ||
| { | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testShouldHandleStandardBehavior(): void | ||
| { | ||
| $sport = new ArticleDatePoint(); | ||
| $sport->setTitle('Sport'); | ||
| $sport->setBody('Sport article body.'); | ||
|
|
||
| $sportComment = new CommentDatePoint(); | ||
| $sportComment->setMessage('hello'); | ||
| $sportComment->setArticle($sport); | ||
| $sportComment->setStatus(0); | ||
|
|
||
| $author = new Author(); | ||
| $author->setName('Original author'); | ||
| $author->setEmail('original@author.dev'); | ||
|
|
||
| $sport->setAuthor($author); | ||
|
|
||
| $this->em->persist($sport); | ||
| $this->em->persist($sportComment); | ||
| $this->em->flush(); | ||
|
|
||
| /** @var ArticleDatePoint $sport */ | ||
| $sport = $this->em->getRepository(ArticleDatePoint::class)->findOneBy(['title' => 'Sport']); | ||
| static::assertInstanceOf(DatePoint::class, $su = $sport->getUpdated()); | ||
|
|
||
| static::assertNull($sport->getContentChanged()); | ||
| static::assertNull($sport->getPublished()); | ||
| static::assertNull($sport->getAuthorChanged()); | ||
|
|
||
| $author = $sport->getAuthor(); | ||
| $author->setName('New author'); | ||
| $sport->setAuthor($author); | ||
|
|
||
| /** @var CommentDatePoint $sportComment */ | ||
| $sportComment = $this->em->getRepository(CommentDatePoint::class)->findOneBy(['message' => 'hello']); | ||
| static::assertInstanceOf(DatePoint::class, $sc = $sportComment->getModified()); | ||
|
|
||
| static::assertNotNull($sportComment->getModified()); | ||
| static::assertNull($sportComment->getClosed()); | ||
|
|
||
| $sportComment->setStatus(1); | ||
| $published = new Type(); | ||
| $published->setTitle('Published'); | ||
|
|
||
| $sport->setType($published); | ||
| $this->em->persist($sport); | ||
| $this->em->persist($published); | ||
| $this->em->persist($sportComment); | ||
| $this->em->flush(); | ||
|
|
||
| $sportComment = $this->em->getRepository(CommentDatePoint::class)->findOneBy(['message' => 'hello']); | ||
| static::assertInstanceOf(DatePoint::class, $scc = $sportComment->getClosed()); | ||
| static::assertInstanceOf(DatePoint::class, $sp = $sport->getPublished()); | ||
| static::assertInstanceOf(DatePoint::class, $sa = $sport->getAuthorChanged()); | ||
|
|
||
| $sport->setTitle('Updated'); | ||
| $this->em->persist($sport); | ||
| $this->em->persist($published); | ||
| $this->em->persist($sportComment); | ||
| $this->em->flush(); | ||
|
|
||
| // prevent "Failed asserting that two variables reference the same object." error, hence compare unix epoch | ||
| static::assertEquals($sport->getCreated()->format('U'), $sc->format('U'), 'Date created should remain same after update'); | ||
| static::assertNotSame($su2 = $sport->getUpdated(), $su, 'Date updated should change after update'); | ||
| static::assertInstanceOf(DatePoint::class, $sport->getUpdated()); | ||
| static::assertSame($sport->getPublished(), $sp, 'Date published should remain the same after update'); | ||
| static::assertNotSame($scc2 = $sport->getContentChanged(), $scc, 'Content must have changed after update'); | ||
| static::assertInstanceOf(DatePoint::class, $sport->getContentChanged()); | ||
| static::assertSame($sport->getAuthorChanged(), $sa, 'Author should remain same after update'); | ||
|
|
||
| $author = $sport->getAuthor(); | ||
| $author->setName('Third author'); | ||
| $sport->setAuthor($author); | ||
|
|
||
| $sport->setBody('Body updated'); | ||
| $this->em->persist($sport); | ||
| $this->em->persist($published); | ||
| $this->em->persist($sportComment); | ||
| $this->em->flush(); | ||
|
|
||
| // prevent "Failed asserting that two variables reference the same object." error, hence compare unix epoch | ||
| static::assertEquals($sport->getCreated()->format('U'), $sc->format('U'), 'Date created should remain same after update'); | ||
| static::assertNotSame($sport->getUpdated(), $su2, 'Date updated should change after update'); | ||
| static::assertSame($sport->getPublished(), $sp, 'Date published should remain the same after update'); | ||
| static::assertNotSame($sport->getContentChanged(), $scc2, 'Content must have changed after update'); | ||
| static::assertNotSame($sport->getAuthorChanged(), $sa, 'Author must have changed after update'); | ||
| } | ||
|
|
||
| protected function getUsedEntityFixtures(): array | ||
| { | ||
| return [ | ||
| ArticleDatePoint::class, | ||
| CommentDatePoint::class, | ||
| Type::class, | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.