Completed
Push — master ( 7f572e...bf04a1 )
by Benedikt
02:39
created

LoadingServiceTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 138
Duplicated Lines 20.29 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 28
loc 138
c 0
b 0
f 0
rs 10
wmc 6
lcom 2
cbo 5

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruct() 9 9 1
A testLoadEntitiesAlreadyLoaded() 0 11 1
A testLoadEntitiesDefaultPropertiesGame() 0 17 1
B testLoadEntitiesMultipleLevels() 0 40 1
A testLoadEntitiesNullProperty() 10 10 1
A testLoadEntitiesSimpleProperty() 9 9 1

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
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 1/3/18
7
 * Time: 3:53 PM
8
 */
9
10
namespace Tfboe\FmLib\Tests\Unit\Service;
11
12
use Doctrine\Common\Collections\AbstractLazyCollection;
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Tfboe\FmLib\Entity\CompetitionInterface;
16
use Tfboe\FmLib\Entity\GameInterface;
17
use Tfboe\FmLib\Entity\PhaseInterface;
18
use Tfboe\FmLib\Entity\TournamentInterface;
19
use Tfboe\FmLib\Service\LoadingService;
20
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
21
22
23
/**
24
 * Class LoadingServiceTest
25
 * @package Tfboe\FmLib\Tests\Unit\Service
26
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
27
 */
28
class LoadingServiceTest extends UnitTestCase
29
{
30
//<editor-fold desc="Public Methods">
31
  /**
32
   * @covers \Tfboe\FmLib\Service\LoadingService::__construct
33
   */
34 View Code Duplication
  public function testConstruct()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
35
  {
36
    /** @var EntityManagerInterface $entityManager */
37
    $entityManager = $this->getMockForAbstractClass(EntityManagerInterface::class);
38
    $service = new LoadingService($entityManager);
39
    self::assertInstanceOf(LoadingService::class, $service);
40
    /** @noinspection PhpUnhandledExceptionInspection */
41
    self::assertEquals($entityManager, self::getProperty(get_class($service), 'em')->getValue($service));
42
  }
43
44
  /**
45
   * @covers \Tfboe\FmLib\Service\LoadingService::loadEntities
46
   * @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap
47
   * @uses   \Tfboe\FmLib\Service\LoadingService::__construct
48
   */
49
  public function testLoadEntitiesAlreadyLoaded()
50
  {
51
    $service = new LoadingService($this->getEntityManagerMockForQueries([]));
0 ignored issues
show
Documentation introduced by
$this->getEntityManagerMockForQueries(array()) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
53
    $tournament = $this->createMock(TournamentInterface::class);
54
    $tournament->method('getEntityId')->willReturn('t');
55
    $initializedCollection = new ArrayCollection();
56
    $initializedCollection->__isInitialized__ = true;
0 ignored issues
show
Bug introduced by
The property __isInitialized__ does not seem to exist in Doctrine\Common\Collections\ArrayCollection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57
    $tournament->method('getCompetitions')->willReturn($initializedCollection);
58
    $service->loadEntities([$tournament], [TournamentInterface::class => [["competitions"]]]);
59
  }
60
61
  /**
62
   * @covers \Tfboe\FmLib\Service\LoadingService::loadEntities
63
   * @covers \Tfboe\FmLib\Service\LoadingService::loadProperties
64
   * @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap
65
   * @uses   \Tfboe\FmLib\Service\LoadingService::__construct
66
   */
67
  public function testLoadEntitiesDefaultPropertiesGame()
68
  {
69
    $service = new LoadingService($this->getEntityManagerMockForQuery([],
0 ignored issues
show
Documentation introduced by
$this->getEntityManagerM...WHERE t1.id IN(\'g\')') is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
      'SELECT t1, t2, t3 FROM Tfboe\FmLib\Entity\GameInterface t1 LEFT JOIN t1.playersA t2 LEFT JOIN '
71
      . 't1.playersB t3 WHERE t1.id IN(\'g\')'
72
    ));
73
    $game = $this->createMock(GameInterface::class);
74
    $game->method('getEntityId')->willReturn('g');
75
76
    $uninitializedLazyCollection = $this->createMock(AbstractLazyCollection::class);
77
    $uninitializedLazyCollection->method('isInitialized')->willReturn(false);
78
79
    $game->expects(self::exactly(2))->method('getPlayersA')->willReturnOnConsecutiveCalls($uninitializedLazyCollection,
80
      new ArrayCollection());
81
    $game->expects(self::once())->method('getPlayersB')->willReturn(new ArrayCollection());
82
    $service->loadEntities([$game]);
83
  }
84
85
  /**
86
   * @covers \Tfboe\FmLib\Service\LoadingService::loadEntities
87
   * @covers \Tfboe\FmLib\Service\LoadingService::loadProperties
88
   * @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap
89
   * @uses   \Tfboe\FmLib\Service\LoadingService::__construct
90
   */
91
  public function testLoadEntitiesMultipleLevels()
92
  {
93
    $tournament = $this->createMock(TournamentInterface::class);
94
    $tournament->method('getEntityId')->willReturn('t');
95
96
    $competition1 = $this->createMock(CompetitionInterface::class);
97
    $competition1->method('getEntityId')->willReturn('c1');
98
99
    $competition2 = $this->createMock(CompetitionInterface::class);
100
    $competition2->method('getEntityId')->willReturn('c1');
101
102
    $uninitializedLazyCollection = $this->createMock(AbstractLazyCollection::class);
103
    $uninitializedLazyCollection->method('isInitialized')->willReturn(false);
104
105
    $tournament->method('getCompetitions')->willReturnOnConsecutiveCalls(
106
      $uninitializedLazyCollection,
107
      new ArrayCollection([$competition1, $competition2])
108
    );
109
110
    $phase1 = $this->createMock(PhaseInterface::class);
111
    $phase1->method('getEntityId')->willReturn('p1');
112
113
    $competition1->method('getPhases')->willReturn(new ArrayCollection([$phase1]));
114
115
    $phase2 = $this->createMock(PhaseInterface::class);
116
    $phase2->method('getEntityId')->willReturn('p2');
117
118
    $competition2->method('getPhases')->willReturn(new ArrayCollection([$phase2]));
119
120
    $entityManager = $this->getEntityManagerMockForQueries([[], []], [
121
      'SELECT t1, t2 FROM Tfboe\FmLib\Entity\TournamentInterface t1 LEFT JOIN t1.competitions t2 WHERE t1.id IN(\'t\')',
122
      'SELECT t1, t2 FROM Tfboe\FmLib\Entity\CompetitionInterface t1 LEFT JOIN t1.phases t2 WHERE t1.id IN(\'c1\')'
123
    ]);
124
125
    $service = new LoadingService($entityManager);
0 ignored issues
show
Documentation introduced by
$entityManager is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
    $service->loadEntities([$tournament], [
127
      TournamentInterface::class => [["competitions"]],
128
      CompetitionInterface::class => [["phases"]]
129
    ]);
130
  }
131
132
  /**
133
   * @covers \Tfboe\FmLib\Service\LoadingService::loadEntities
134
   * @covers \Tfboe\FmLib\Service\LoadingService::loadProperties
135
   * @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap
136
   * @uses   \Tfboe\FmLib\Service\LoadingService::__construct
137
   */
138 View Code Duplication
  public function testLoadEntitiesNullProperty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
139
  {
140
    $service = new LoadingService($this->getEntityManagerMockForQuery([],
0 ignored issues
show
Documentation introduced by
$this->getEntityManagerM...WHERE t1.id IN(\'t\')') is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
141
      'SELECT t1, t2 FROM Tfboe\FmLib\Entity\TournamentInterface t1 LEFT JOIN t1.competitions t2 WHERE t1.id IN(\'t\')'
142
    ));
143
    $tournament = $this->createMock(TournamentInterface::class);
144
    $tournament->method('getEntityId')->willReturn('t');
145
    $tournament->method('getCompetitions')->willReturn(null);
146
    $service->loadEntities([$tournament], [TournamentInterface::class => [["competitions"]]]);
147
  }
148
149
  /**
150
   * @covers \Tfboe\FmLib\Service\LoadingService::loadEntities
151
   * @covers \Tfboe\FmLib\Service\LoadingService::loadProperties
152
   * @covers \Tfboe\FmLib\Service\LoadingService::keyOfPropertyMap
153
   * @uses   \Tfboe\FmLib\Service\LoadingService::__construct
154
   */
155 View Code Duplication
  public function testLoadEntitiesSimpleProperty()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
156
  {
157
    $service = new LoadingService($this->getEntityManagerMockForQuery([],
0 ignored issues
show
Documentation introduced by
$this->getEntityManagerM...WHERE t1.id IN(\'c\')') is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\ORM\EntityManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
      'SELECT t1, t2 FROM Tfboe\FmLib\Entity\CompetitionInterface t1 LEFT JOIN t1.tournament t2 WHERE t1.id IN(\'c\')'
159
    ));
160
    $competition = $this->createMock(CompetitionInterface::class);
161
    $competition->method('getEntityId')->willReturn('c');
162
    $service->loadEntities([$competition], [CompetitionInterface::class => [["tournament"]]]);
163
  }
164
//</editor-fold desc="Public Methods">
165
}