[DB][TESTS] Implement Doctrine event listener to update timestamps on modification, and related tests

This commit is contained in:
Diogo Machado 2020-10-13 23:45:48 +01:00 committed by Hugo Sales
parent c3aa2ae400
commit fbc85086fd
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
4 changed files with 106 additions and 1 deletions

View File

@ -37,6 +37,10 @@ services:
App\Core\Queue\MessageHandler:
tags: ['messenger.message_handler']
App\EventListener\UpdateListener:
tags:
- { name: doctrine.event_listener , event: preUpdate }
Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider: ~
Plugin\:

View File

@ -0,0 +1,44 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\EventListener;
use DateTime;
use Doctrine\ORM\Event\PreUpdateEventArgs;
class UpdateListener
{
public function preUpdate(PreUpdateEventArgs $args)
{
$entity = $args->getEntity();
if (!method_exists($entity, 'setModified')) {
return false;
}
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$md = $em->getClassMetadata(get_class($entity));
$entity->setModified(new DateTime());
$uow->recomputeSingleEntityChangeSet($md, $entity);
return true;
}
}

View File

@ -16,7 +16,7 @@
## References
- GNU social main site, https://gnu.io
- GNU social repository, https://git.gnu.io/gnu/gnu-social
- GNU social repository, https://notabug.org/gnu/gnu-social
{% endapply %}
{% endblock %}
</div>

View File

@ -0,0 +1,57 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace App\Tests\EventListener;
use App\Entity\GSActor;
use App\EventListener\UpdateListener;
use DateTime;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\UnitOfWork;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class UpdateListenerTest extends WebTestCase
{
public function testPreUpdate()
{
$actor = new GSActor();
$actor->setModified(new DateTime('1999-09-23'));
static::assertSame($actor->getModified(), new DateTime('1999-09-23'));
$em = $this->createMock(EntityManager::class);
$uow = $this->createMock(UnitOfWork::class);
$em->expects(static::once())
->method('getUnitOfWork')
->willReturn($uow);
$md = $this->createMock(ClassMetadata::class);
$em->expects(static::once())
->method('getClassMetadata')
->willReturn($md);
$change_set = [];
$args = new PreUpdateEventArgs($actor, $em, $change_set);
$ul = new UpdateListener();
$ul->preUpdate($args);
static::assertNotSame($actor->getModified(), new DateTime('1999-09-23'));
}
}