AttributeCreatorListenerTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 90
Duplicated Lines 15.56 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 1
cbo 12
dl 14
loc 90
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 6 1
A entityShouldHaveAttributes() 14 71 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Padam87\AttributeBundle\Tests\EventListener;
4
5
use Doctrine\DBAL\Logging\DebugStack;
6
use \Mockery as m;
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
use Doctrine\ORM\EntityManager;
9
use Doctrine\ORM\Tools\SchemaTool;
10
use Padam87\AttributeBundle\Entity\Definition;
11
use Padam87\AttributeBundle\Entity\Schema;
12
use Padam87\AttributeBundle\Tests\Model\Subscriber;
13
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
14
15
class AttributeCreatorListenerTest extends WebTestCase
16
{
17
    protected function setUp()
18
    {
19
        static::bootKernel();
20
    }
21
22
    protected function tearDown()
23
    {
24
        m::close();
25
26
        parent::tearDown();
27
    }
28
29
    /**
30
     * @test
31
     * @group functional
32
     */
33
    public function entityShouldHaveAttributes()
34
    {
35
        $container = static::$kernel->getContainer();
36
        /** @var ManagerRegistry $doctrine */
37
        $doctrine = $container->get('doctrine');
38
        /** @var EntityManager $em */
39
        $em = $doctrine->getManager();
40
        $metadata = $em->getMetadataFactory()->getAllMetadata();
41
42
        $schemaTool = new SchemaTool($em);
43
        $schemaTool->dropSchema($metadata);
44
        $schemaTool->createSchema($metadata);
45
46
        $schema = new Schema();
47
        $schema->setClassName('Padam87\AttributeBundle\Tests\Model\Subscriber');
48
49
        /** @var DebugStack $profiler */
50
        $profiler = $container->get('doctrine.dbal.logger.profiling.default');
51
52
        $subscriber = new Subscriber();
53
54
        // FIRST PASS
55 View Code Duplication
        for ($i = 0; $i < 5; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $definition = new Definition();
57
            $definition->setName($i);
58
            $definition->setType('text');
59
60
            $schema->addDefinition($definition);
61
        }
62
63
        $em->persist($schema);
64
        $em->flush($schema);
65
        $em->refresh($schema);
66
67
        $this->assertCount(5, $schema->getDefinitions());
68
69
        $em->persist($subscriber);
70
        $em->flush($subscriber);
71
72
        $profiler->queries = []; // reset the profiler to show oly relevant queries
73
74
        $em->refresh($subscriber);
75
        
76
        $this->assertCount(5, $subscriber->getAttributes());
77
        $this->assertCount(16, $profiler->queries);
78
79
        // SECOND PASS
80 View Code Duplication
        for ($i = 5; $i < 10; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
            $definition = new Definition();
82
            $definition->setName($i);
83
            $definition->setType('text');
84
85
            $schema->addDefinition($definition);
86
        }
87
88
        $em->persist($schema);
89
        $em->flush($schema);
90
        $em->refresh($schema);
91
92
        $this->assertCount(10, $schema->getDefinitions());
93
94
        $subscriber->setName('test');
95
        $em->flush($subscriber);
96
97
        $profiler->queries = []; // reset the profiler to show oly relevant queries
98
99
        $em->refresh($subscriber);
100
101
        $this->assertCount(10, $subscriber->getAttributes());
102
        $this->assertCount(16, $profiler->queries);
103
    }
104
}
105