BlameableEntitiesTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 10
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A tearDown() 0 4 1
A testPost() 0 19 1
A loginAs() 0 15 1
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']);
0 ignored issues
show
Unused Code introduced by
The call to ObjectRepository::findOneBy() has too many arguments starting with array('id' => 'DESC').

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.

Loading history...
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(
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated with message: since Symfony 4.3, use RequestEvent instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
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