Skip to content

Commit 1709572

Browse files
authored
Merge pull request #1872 from phansys/datetimeinterface
Add support for `DateTimeInterface`
2 parents 3c9f53c + 866e280 commit 1709572

File tree

9 files changed

+190
-20
lines changed

9 files changed

+190
-20
lines changed

.travis.yml

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,37 @@ language: php
22

33
sudo: false
44

5-
php:
6-
- 5.4
7-
- 5.5
8-
- 5.6
9-
- 7.0
5+
matrix:
6+
include:
7+
- php: 5.4
8+
env: phpunit_exclude_groups=datetimeinterface
9+
- php: 5.5
10+
- php: 5.6
11+
- php: 7.0
12+
- php: 7.1
13+
- php: 7.2
1014

1115
services: mongodb
1216

1317
before_install:
14-
- if [[ "$TRAVIS_PHP_VERSION" = 5.* ]]; then echo 'extension=mongo.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi
15-
- if [[ "$TRAVIS_PHP_VERSION" != 5.* ]]; then echo 'extension=mongodb.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi
16-
- if [[ "$TRAVIS_PHP_VERSION" != 5.* ]]; then cp composer7.json composer.json; fi
18+
- if [[ "$TRAVIS_PHP_VERSION" = 5.* ]]; then echo 'extension=mongo.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi
19+
- if [[ "$TRAVIS_PHP_VERSION" != 5.* ]]; then echo 'extension=mongodb.so' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi
20+
- if [[ "$TRAVIS_PHP_VERSION" != 5.* ]]; then cp composer7.json composer.json; fi
1721

1822
install:
19-
- composer install --prefer-dist
23+
- composer install --prefer-dist
2024

2125
script:
22-
- bin/phpunit -c tests/
26+
- |
27+
if [[ ! $phpunit_exclude_groups ]]; then
28+
echo "Debug: 'bin/phpunit -c tests/'"
29+
bin/phpunit -c tests/
30+
else
31+
echo "Debug: 'bin/phpunit -c tests/ --exclude-group $phpunit_exclude_groups'"
32+
bin/phpunit -c tests/ --exclude-group $phpunit_exclude_groups
33+
fi
2334
2435
notifications:
25-
email:
26-
27-
36+
email:
37+
38+

lib/Gedmo/Sluggable/SluggableListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ private function generateSlug(SluggableAdapter $ea, $object)
298298
$needToChangeSlug = true;
299299
}
300300
$value = $meta->getReflectionProperty($sluggableField)->getValue($object);
301-
$slug .= ($value instanceof \DateTime) ? $value->format($options['dateFormat']) : $value;
301+
// Remove `$value instanceof \DateTime` check when PHP version is bumped to >=5.5
302+
$slug .= ($value instanceof \DateTime || $value instanceof \DateTimeInterface) ? $value->format($options['dateFormat']) : $value;
302303
$slug .= ' ';
303304
}
304305
// trim generated slug as it will have unnecessary trailing space

lib/Gedmo/SoftDeleteable/SoftDeleteableListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public function onFlush(EventArgs $args)
7272
$reflProp = $meta->getReflectionProperty($config['fieldName']);
7373
$oldValue = $reflProp->getValue($object);
7474

75-
if (isset($config['hardDelete']) && $config['hardDelete'] && $oldValue instanceof \Datetime) {
75+
// Remove `$oldValue instanceof \DateTime` check when PHP version is bumped to >=5.5
76+
if (isset($config['hardDelete']) && $config['hardDelete'] && ($oldValue instanceof \DateTime || $oldValue instanceof \DateTimeInterface)) {
7677
continue; // want to hard delete
7778
}
7879

lib/Gedmo/SoftDeleteable/Traits/SoftDeleteable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait SoftDeleteable
1818
/**
1919
* Sets deletedAt.
2020
*
21-
* @param \Datetime|null $deletedAt
21+
* @param \DateTime|null $deletedAt
2222
*
2323
* @return $this
2424
*/

lib/Gedmo/SoftDeleteable/Traits/SoftDeleteableDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait SoftDeleteableDocument
2121
/**
2222
* Sets deletedAt.
2323
*
24-
* @param \Datetime|null $deletedAt
24+
* @param \DateTime|null $deletedAt
2525
*
2626
* @return $this
2727
*/

lib/Gedmo/SoftDeleteable/Traits/SoftDeleteableEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ trait SoftDeleteableEntity
2121
/**
2222
* Sets deletedAt.
2323
*
24-
* @param \Datetime|null $deletedAt
24+
* @param \DateTime|null $deletedAt
2525
*
2626
* @return $this
2727
*/

lib/Gedmo/Timestampable/Traits/TimestampableDocument.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ trait TimestampableDocument
3030
/**
3131
* Sets createdAt.
3232
*
33-
* @param \Datetime $createdAt
33+
* @param \DateTime $createdAt
3434
* @return $this
3535
*/
3636
public function setCreatedAt(\DateTime $createdAt)

lib/Gedmo/Tool/Logging/DBAL/QueryAnalyzer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ private function getConvertedParams($params, $types)
227227
$value = $type->convertToDatabaseValue($value, $this->platform);
228228
}
229229
} else {
230-
if (is_object($value) && $value instanceof \DateTime) {
230+
// Remove `$value instanceof \DateTime` check when PHP version is bumped to >=5.5
231+
if (is_object($value) && ($value instanceof \DateTime || $value instanceof \DateTimeInterface)) {
231232
$value = $value->format($this->platform->getDateTimeFormatString());
232233
} elseif (!is_null($value)) {
233234
$type = Type::getType(gettype($value));

tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,162 @@ public function testSoftDeleteable()
290290
$this->assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
291291
}
292292

293+
/**
294+
* @group datetimeinterface
295+
*/
296+
public function testSoftDeleteableWithDateTimeInterface()
297+
{
298+
$repo = $this->em->getRepository(self::ARTICLE_CLASS);
299+
$commentRepo = $this->em->getRepository(self::COMMENT_CLASS);
300+
301+
$comment = new Comment();
302+
$commentField = 'comment';
303+
$commentValue = 'Comment 1';
304+
$comment->setComment($commentValue);
305+
$art0 = new Article();
306+
$field = 'title';
307+
$value = 'Title 1';
308+
$art0->setTitle($value);
309+
$art0->addComment($comment);
310+
311+
$this->em->persist($art0);
312+
$this->em->flush();
313+
314+
$art = $repo->findOneBy(array($field => $value));
315+
316+
$this->assertNull($art->getDeletedAt());
317+
$this->assertNull($comment->getDeletedAt());
318+
319+
$art->setDeletedAt(new \DateTimeImmutable());
320+
$this->em->flush();
321+
322+
$art = $repo->findOneBy(array($field => $value));
323+
$this->assertNull($art);
324+
325+
// Now we deactivate the filter so we test if the entity appears in the result
326+
$this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
327+
328+
$art = $repo->findOneBy(array($field => $value));
329+
$this->assertInternalType('object', $art);
330+
$this->assertInternalType('object', $art->getDeletedAt());
331+
$this->assertInstanceOf('DateTimeInterface', $art->getDeletedAt());
332+
$comment = $commentRepo->findOneBy(array($commentField => $commentValue));
333+
$this->assertInternalType('object', $comment);
334+
$this->assertNull($comment->getDeletedAt());
335+
336+
$this->em->createQuery('UPDATE '.self::ARTICLE_CLASS.' a SET a.deletedAt = NULL')->execute();
337+
338+
$this->em->refresh($art);
339+
$this->em->refresh($comment);
340+
341+
// Now we try with a DQL Delete query
342+
$this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
343+
$dql = sprintf('DELETE FROM %s a WHERE a.%s = :%s',
344+
self::ARTICLE_CLASS, $field, $field);
345+
$query = $this->em->createQuery($dql);
346+
$query->setParameter($field, $value);
347+
$query->setHint(
348+
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
349+
'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
350+
);
351+
352+
$query->execute();
353+
354+
$art = $repo->findOneBy(array($field => $value));
355+
$this->assertNull($art);
356+
357+
// Now we deactivate the filter so we test if the entity appears in the result
358+
$this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
359+
$this->em->clear();
360+
361+
$art = $repo->findOneBy(array($field => $value));
362+
363+
$this->assertInternalType('object', $art);
364+
$this->assertInternalType('object', $art->getDeletedAt());
365+
$this->assertInstanceOf('DateTimeInterface', $art->getDeletedAt());
366+
367+
// Inheritance tree DELETE DQL
368+
$this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
369+
370+
$megaPageRepo = $this->em->getRepository(self::MEGA_PAGE_CLASS);
371+
$module = new Module();
372+
$module->setTitle('Module 1');
373+
$page = new MegaPage();
374+
$page->setTitle('Page 1');
375+
$page->addModule($module);
376+
$module->setPage($page);
377+
378+
$this->em->persist($page);
379+
$this->em->persist($module);
380+
$this->em->flush();
381+
382+
$dql = sprintf('DELETE FROM %s p',
383+
self::PAGE_CLASS);
384+
$query = $this->em->createQuery($dql);
385+
$query->setHint(
386+
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
387+
'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
388+
);
389+
390+
$query->execute();
391+
392+
$p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
393+
$this->assertNull($p);
394+
395+
// Now we deactivate the filter so we test if the entity appears in the result
396+
$this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
397+
$this->em->clear();
398+
399+
$p = $megaPageRepo->findOneBy(array('title' => 'Page 1'));
400+
401+
$this->assertInternalType('object', $p);
402+
$this->assertInternalType('object', $p->getDeletedAt());
403+
$this->assertInstanceOf('DateTimeInterface', $p->getDeletedAt());
404+
405+
// Test of #301
406+
$this->em->getFilters()->enable(self::SOFT_DELETEABLE_FILTER_NAME);
407+
408+
$otherArticleRepo = $this->em->getRepository(self::OTHER_ARTICLE_CLASS);
409+
$otherCommentRepo = $this->em->getRepository(self::OTHER_COMMENT_CLASS);
410+
$otherArt = new OtherArticle();
411+
$otherComment = new OtherComment();
412+
$otherArt->setTitle('Page 1');
413+
$otherComment->setComment('Comment');
414+
$otherArt->addComment($otherComment);
415+
$otherComment->setArticle($otherArt);
416+
417+
$this->em->persist($otherArt);
418+
$this->em->persist($otherComment);
419+
$this->em->flush();
420+
421+
$this->em->refresh($otherArt);
422+
$this->em->refresh($otherComment);
423+
424+
$artId = $otherArt->getId();
425+
$commentId = $otherComment->getId();
426+
427+
$otherArt->setDeletedAt(new \DateTimeImmutable());
428+
$this->em->flush();
429+
430+
$foundArt = $otherArticleRepo->findOneBy(array('id' => $artId));
431+
$foundComment = $otherCommentRepo->findOneBy(array('id' => $commentId));
432+
433+
$this->assertNull($foundArt);
434+
$this->assertInternalType('object', $foundComment);
435+
$this->assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
436+
437+
$this->em->getFilters()->disable(self::SOFT_DELETEABLE_FILTER_NAME);
438+
439+
$foundArt = $otherArticleRepo->findOneById($artId);
440+
$foundComment = $otherCommentRepo->findOneById($commentId);
441+
442+
$this->assertInternalType('object', $foundArt);
443+
$this->assertInternalType('object', $foundArt->getDeletedAt());
444+
$this->assertInstanceOf('DateTimeInterface', $foundArt->getDeletedAt());
445+
$this->assertInternalType('object', $foundComment);
446+
$this->assertInstanceOf(self::OTHER_COMMENT_CLASS, $foundComment);
447+
}
448+
293449
/**
294450
* Make sure that soft delete also works when configured on a mapped superclass
295451
*/

0 commit comments

Comments
 (0)