1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Tests\Functional\EventListener; |
4
|
|
|
|
5
|
|
|
use App\Entity\Post; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
13
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
14
|
|
|
use Symfony\Component\Security\Core\User\User; |
15
|
|
|
|
16
|
|
|
class BlameableEntitiesTest extends WebTestCase |
17
|
|
|
{ |
18
|
|
|
protected $user; |
19
|
|
|
/** @var EntityManager */ |
20
|
|
|
protected $em; |
21
|
|
|
|
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
static::bootKernel(); |
25
|
|
|
$container = self::$container; |
26
|
|
|
$this->em = $container->get(EntityManagerInterface::class); |
27
|
|
|
$this->em->getConnection()->beginTransaction(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function tearDown() |
31
|
|
|
{ |
32
|
|
|
$this->em->getConnection()->rollBack(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testPost() |
36
|
|
|
{ |
37
|
|
|
$this->loginAs('admin'); |
38
|
|
|
|
39
|
|
|
$post = (new Post()) |
40
|
|
|
->setText('Random text') |
41
|
|
|
->setTitle('Hello world'); |
42
|
|
|
|
43
|
|
|
$this->assertNull($post->getCreatedBy()); |
44
|
|
|
$this->assertNull($post->getUpdatedBy()); |
45
|
|
|
|
46
|
|
|
$this->em->persist($post); |
47
|
|
|
$this->em->flush(); |
48
|
|
|
|
49
|
|
|
/** @var Post $post */ |
50
|
|
|
$post = $this->em->getRepository(Post::class)->findOneBy([], ['id' => 'DESC']); |
|
|
|
|
51
|
|
|
$this->assertEquals('admin', $post->getCreatedBy()); |
52
|
|
|
$this->assertEquals('admin', $post->getUpdatedBy()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function loginAs(string $username) |
56
|
|
|
{ |
57
|
|
|
$tokenStorage = self::$container->get(TokenStorageInterface::class); |
58
|
|
|
$firewallName = 'secure_area'; |
59
|
|
|
$this->user = new User($username, '123456'); |
60
|
|
|
$token = new UsernamePasswordToken($this->user, null, $firewallName, ['ROLE_ADMIN']); |
61
|
|
|
$tokenStorage->setToken($token); |
62
|
|
|
|
63
|
|
|
$event = new GetResponseEvent( |
|
|
|
|
64
|
|
|
$this->createMock(HttpKernelInterface::class), |
65
|
|
|
new Request(), |
66
|
|
|
HttpKernelInterface::MASTER_REQUEST |
67
|
|
|
); |
68
|
|
|
self::$container->get('stof_doctrine_extensions.event_listener.blame')->onKernelRequest($event); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.