-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert DateTime to PHP value using Doctrine converter
- Loading branch information
Showing
8 changed files
with
685 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
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
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,107 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <[email protected]> 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\Carbon; | ||
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\Article; | ||
use Gedmo\Tests\SoftDeleteable\Fixture\Entity\Comment; | ||
use Gedmo\Tests\Tool\BaseTestCaseORM; | ||
|
||
final class CarbonTest extends BaseTestCaseORM | ||
{ | ||
public const ARTICLE_CLASS = Article::class; | ||
public const COMMENT_CLASS = Comment::class; | ||
public const SOFT_DELETEABLE_FILTER_NAME = 'soft-deleteable'; | ||
|
||
/** | ||
* @var SoftDeleteableListener | ||
*/ | ||
private $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(self::ARTICLE_CLASS); | ||
$commentRepo = $this->em->getRepository(self::COMMENT_CLASS); | ||
|
||
$comment = new Comment(); | ||
$commentField = 'comment'; | ||
$commentValue = 'Comment 1'; | ||
$comment->setComment($commentValue); | ||
$art0 = new Article(); | ||
$field = 'title'; | ||
$value = 'Title 1'; | ||
$art0->setTitle($value); | ||
$art0->addComment($comment); | ||
|
||
$this->em->persist($art0); | ||
$this->em->flush(); | ||
|
||
$art = $repo->findOneBy([$field => $value]); | ||
|
||
static::assertNull($art->getDeletedAt()); | ||
static::assertNull($comment->getDeletedAt()); | ||
|
||
$this->em->remove($art); | ||
$this->em->flush(); | ||
|
||
$art = $repo->findOneBy([$field => $value]); | ||
static::assertNull($art); | ||
$comment = $commentRepo->findOneBy([$commentField => $commentValue]); | ||
static::assertNull($comment); | ||
|
||
// Now we deactivate the filter so we test if the entity appears in the result | ||
$this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME); | ||
|
||
$art = $repo->findOneBy([$field => $value]); | ||
static::assertIsObject($art); | ||
static::assertIsObject($art->getDeletedAt()); | ||
static::assertInstanceOf(Carbon::class, $art->getDeletedAt()); | ||
$comment = $commentRepo->findOneBy([$commentField => $commentValue]); | ||
static::assertIsObject($comment); | ||
static::assertIsObject($comment->getDeletedAt()); | ||
static::assertInstanceOf(Carbon::class, $comment->getDeletedAt()); | ||
} | ||
|
||
protected function getUsedEntityFixtures(): array | ||
{ | ||
return [ | ||
self::ARTICLE_CLASS, | ||
self::COMMENT_CLASS, | ||
]; | ||
} | ||
} |
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,161 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Doctrine Behavioral Extensions package. | ||
* (c) Gediminas Morkevicius <[email protected]> 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 Carbon\Carbon; | ||
use Carbon\CarbonImmutable; | ||
use Carbon\Doctrine\DateTimeImmutableType; | ||
use Carbon\Doctrine\DateTimeType; | ||
use DateTime; | ||
use Doctrine\Common\EventManager; | ||
use Doctrine\DBAL\Types\DateType; | ||
use Doctrine\DBAL\Types\Type as DoctrineType; | ||
use Doctrine\DBAL\Types\Types; | ||
use Gedmo\Tests\Timestampable\Fixture\ArticleCarbon; | ||
use Gedmo\Tests\Timestampable\Fixture\Author; | ||
use Gedmo\Tests\Timestampable\Fixture\CommentCarbon; | ||
use Gedmo\Tests\Timestampable\Fixture\Type; | ||
use Gedmo\Tests\Tool\BaseTestCaseORM; | ||
use Gedmo\Timestampable\TimestampableListener; | ||
|
||
final class CarbonTest extends BaseTestCaseORM | ||
{ | ||
public const ARTICLE = ArticleCarbon::class; | ||
public const COMMENT = CommentCarbon::class; | ||
public const TYPE = Type::class; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$evm = new EventManager(); | ||
$evm->addEventSubscriber(new TimestampableListener()); | ||
|
||
$this->getDefaultMockSqliteEntityManager($evm); | ||
|
||
/** | ||
* DATE_MUTABLE => Carbon | ||
* DATETIME_MUTABLE => CarbonImmutable | ||
* TIME_MUTABLE => DateTime | ||
*/ | ||
DoctrineType::overrideType(Types::DATE_MUTABLE, DateTimeType::class); | ||
DoctrineType::overrideType(Types::DATETIME_MUTABLE, DateTimeImmutableType::class); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
parent::tearDown(); | ||
|
||
DoctrineType::overrideType(Types::DATE_MUTABLE, DateType::class); | ||
DoctrineType::overrideType(Types::DATETIME_MUTABLE, \Doctrine\DBAL\Types\DateTimeType::class); | ||
} | ||
|
||
public function testShouldHandleStandardBehavior(): void | ||
{ | ||
$sport = new ArticleCarbon(); | ||
$sport->setTitle('Sport'); | ||
$sport->setBody('Sport article body.'); | ||
|
||
$sportComment = new CommentCarbon(); | ||
$sportComment->setMessage('hello'); | ||
$sportComment->setArticle($sport); | ||
$sportComment->setStatus(0); | ||
|
||
$author = new Author(); | ||
$author->setName('Original author'); | ||
$author->setEmail('[email protected]'); | ||
|
||
$sport->setAuthor($author); | ||
|
||
$this->em->persist($sport); | ||
$this->em->persist($sportComment); | ||
$this->em->flush(); | ||
|
||
/** @var ArticleCarbon $sport */ | ||
$sport = $this->em->getRepository(self::ARTICLE)->findOneBy(['title' => 'Sport']); | ||
static::assertInstanceOf(CarbonImmutable::class, $sport->getUpdated(), 'Type DATETIME_MUTABLE should become CarbonImmutable'); | ||
static::assertInstanceOf(Carbon::class, $sport->getCreated(), 'Type DATE_MUTABLE should become Carbon'); | ||
|
||
static::assertNotNull($sc = $sport->getCreated()); | ||
static::assertNotNull($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 \Gedmo\Tests\Timestampable\Fixture\CommentCarbon $sportComment */ | ||
$sportComment = $this->em->getRepository(self::COMMENT)->findOneBy(['message' => 'hello']); | ||
static::assertInstanceOf(DateTime::class, $sportComment->getModified(), 'Type TIME_MUTABLE should stay DateTime'); | ||
|
||
static::assertNotNull($scm = $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(self::COMMENT)->findOneBy(['message' => 'hello']); | ||
static::assertInstanceOf(CarbonImmutable::class, $sportComment->getClosed(), 'Type DATETIME_MUTABLE should become CarbonImmutable'); | ||
static::assertInstanceOf(CarbonImmutable::class, $sport->getPublished(), 'Type DATETIME_MUTABLE should become CarbonImmutable'); | ||
static::assertInstanceOf(CarbonImmutable::class, $sport->getAuthorChanged(), 'Type DATETIME_MUTABLE should become CarbonImmutable'); | ||
|
||
static::assertNotNull($scc = $sportComment->getClosed()); | ||
static::assertNotNull($sp = $sport->getPublished()); | ||
static::assertNotNull($sa = $sport->getAuthorChanged()); | ||
|
||
$sport->setTitle('Updated'); | ||
$this->em->persist($sport); | ||
$this->em->persist($published); | ||
$this->em->persist($sportComment); | ||
$this->em->flush(); | ||
|
||
static::assertSame($sport->getCreated(), $sc, 'Date created should remain same after update'); | ||
static::assertNotSame($su2 = $sport->getUpdated(), $su, 'Date updated should change after update'); | ||
static::assertInstanceOf(CarbonImmutable::class, $sport->getUpdated(), 'Type DATETIME_MUTABLE should become CarbonImmutable'); | ||
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(CarbonImmutable::class, $sport->getContentChanged(), 'Type DATETIME_MUTABLE should become CarbonImmutable'); | ||
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(); | ||
|
||
static::assertSame($sport->getCreated(), $sc, '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 [ | ||
self::ARTICLE, | ||
self::COMMENT, | ||
self::TYPE, | ||
]; | ||
} | ||
} |
Oops, something went wrong.