Passed
Branch merging-leagues-tournaments (1b2f12)
by Benedikt
06:40
created

PlayerServiceTest::testMergePlayer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 38
c 2
b 0
f 1
dl 0
loc 62
rs 9.312
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 DateTime;
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\ORM\EntityManagerInterface;
15
use PHPUnit\Framework\MockObject\MockObject;
16
use ReflectionException;
17
use Tfboe\FmLib\Entity\CompetitionInterface;
18
use Tfboe\FmLib\Entity\GameInterface;
19
use Tfboe\FmLib\Entity\MatchInterface;
20
use Tfboe\FmLib\Entity\PhaseInterface;
21
use Tfboe\FmLib\Entity\PlayerInterface;
22
use Tfboe\FmLib\Entity\TeamInterface;
23
use Tfboe\FmLib\Entity\TeamMembershipInterface;
24
use Tfboe\FmLib\Entity\TournamentInterface;
25
use Tfboe\FmLib\Service\LoadingServiceInterface;
26
use Tfboe\FmLib\Service\PlayerService;
27
use Tfboe\FmLib\Service\PlayerServiceInterface;
28
use Tfboe\FmLib\Service\RankingSystemServiceInterface;
29
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
30
31
32
/**
33
 * Class PlayerServiceTest
34
 * @package Tfboe\FmLib\Tests\Unit\Service
35
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
 */
37
class PlayerServiceTest extends UnitTestCase
38
{
39
//<editor-fold desc="Public Methods">
40
  /**
41
   * @covers \Tfboe\FmLib\Service\PlayerService::__construct
42
   */
43
  public function testConstruct()
44
  {
45
    /** @var EntityManagerInterface $em */
46
    $em = $this->createMock(EntityManagerInterface::class);
47
    /** @var LoadingServiceInterface $ls */
48
    $ls = $this->createMock(LoadingServiceInterface::class);
49
    /** @var RankingSystemServiceInterface $rankingSystemService */
50
    $rankingSystemService = $this->createMock(RankingSystemServiceInterface::class);
51
    $service = new PlayerService($em, $ls, $rankingSystemService);
52
    self::assertInstanceOf(PlayerService::class, $service);
53
  }
54
55
  /**
56
   * @covers \Tfboe\FmLib\Service\PlayerService::mergePlayers
57
   * @throws ReflectionException
58
   * @uses   \Tfboe\FmLib\Service\PlayerService::__construct
59
   */
60
  public function testMergeBothPlayersInSameTournament()
61
  {
62
    /** @var PlayerInterface $player1 */
63
    $player1 = $this->createStubWithId(PlayerInterface::class, "p1");
64
    /** @var PlayerInterface $player2 */
65
    $player2 = $this->createStubWithId(PlayerInterface::class, "p2");
66
    $team1 = $this->createStub(TeamInterface::class, [
67
      'getMemberships' => new ArrayCollection([$this->createStub(TeamMembershipInterface::class, [
68
        'getPlayer' => $player1
69
      ])])
70
    ]);
71
    $team2 = $this->createStub(TeamInterface::class, [
72
      'getMemberships' => new ArrayCollection([$this->createStub(TeamMembershipInterface::class, [
73
        'getPlayer' => $player2
74
      ])])
75
    ]);
76
77
    $competition = $this->createStub(CompetitionInterface::class, [
78
      'getTeams' => new ArrayCollection([$team1, $team2])
79
    ]);
80
81
    $tournament = $this->createStub(TournamentInterface::class, [
82
      'getCompetitions' => new ArrayCollection([$competition]),
83
      'getName' => 'Tournament',
84
      'getId' => 't',
85
      'getStartTime' => new DateTime("2000-01-01")
86
    ]);
87
88
    /** @var EntityManagerInterface $em */
89
    $em = $this->getEntityManagerMockForQuery([$tournament], /** @lang DQL */
90
      "SELECT t FROM Tfboe\FmLib\Entity\TournamentInterface t INNER JOIN t.competitions c INNER JOIN c.teams te " .
91
      "INNER JOIN te.memberships m WHERE m.player = (:id)");
92
93
    /** @var LoadingServiceInterface $loadingService */
94
    $loadingService = $this->createStub(LoadingServiceInterface::class);
95
    /** @var RankingSystemServiceInterface $rankingSystemService */
96
    $rankingSystemService = $this->createStub(RankingSystemServiceInterface::class);
97
    /** @var PlayerServiceInterface $service */
98
    $service = new PlayerService($em,
99
      $loadingService,
100
      $rankingSystemService
101
    );
102
103
    self::assertEquals('Player 1 and player 2 both attended the tournament Tournament(01.01.2000 00:00, id=\'t\')',
104
      $service->mergePlayers($player1, $player2));
105
  }
106
107
  /**
108
   * @covers \Tfboe\FmLib\Service\PlayerService::mergePlayers
109
   * @uses   \Tfboe\FmLib\Service\PlayerService::__construct
110
   */
111
  public function testMergeIdenticalPlayer()
112
  {
113
    $player1 = $this->createStubWithId(PlayerInterface::class, "p1");
114
    $player2 = $this->createStubWithId(PlayerInterface::class, "p1");
115
116
    $service = new PlayerService(
117
      $this->createStub(EntityManagerInterface::class),
118
      $this->createStub(LoadingServiceInterface::class),
119
      $this->createStub(RankingSystemServiceInterface::class)
120
    );
121
122
    self::assertEquals('Players are identical!', $service->mergePlayers($player1, $player2));
123
  }
124
125
  /**
126
   * @covers \Tfboe\FmLib\Service\PlayerService::mergePlayers
127
   * @throws ReflectionException
128
   * @uses   \Tfboe\FmLib\Service\PlayerService::__construct
129
   */
130
  public function testMergePlayer()
131
  {
132
    $tournament = $this->createStub(TournamentInterface::class);
133
    $player1 = $this->createStubWithId(PlayerInterface::class, "p1");
134
    $player2 = $this->createStubWithId(PlayerInterface::class, "p2");
135
    $otherPlayer = $this->createStubWithId(PlayerInterface::class, "oP");
136
    $team1Membership = $this->createStub(TeamMembershipInterface::class, ['getPlayer' => $player2]);
137
    $team1Membership->method('setPlayer')->with($player1);
138
    $team1 = $this->createStub(TeamInterface::class, [
139
      'getMemberships' => new ArrayCollection([$team1Membership])
140
    ]);
141
    $otherTeam = $this->createStub(TeamInterface::class, [
142
      'getMemberships' => new ArrayCollection([$this->createStub(TeamMembershipInterface::class, [
143
        'getPlayer' => $otherPlayer
144
      ])])
145
    ]);
146
147
    $game1PlayersB = new ArrayCollection([$player2->getId() => $player2]);
148
    $game1 = $this->createStub(GameInterface::class, [
149
      'getPlayersA' => new ArrayCollection([$otherPlayer->getId() => $otherPlayer]),
150
      'getPlayersB' => $game1PlayersB
151
    ]);
152
153
    $game2PlayersA = new ArrayCollection([$player2->getId() => $player2]);
154
    $game2 = $this->createStub(GameInterface::class, [
155
      'getPlayersA' => $game2PlayersA,
156
      'getPlayersB' => new ArrayCollection([$otherPlayer->getId() => $otherPlayer])
157
    ]);
158
159
    $competition = $this->createStub(CompetitionInterface::class, [
160
      'getTeams' => new ArrayCollection([$team1, $otherTeam]),
161
      'getPhases' => new ArrayCollection([$this->createStub(PhaseInterface::class, [
162
        'getMatches' => new ArrayCollection([$this->createStub(MatchInterface::class, [
163
          'getGames' => new ArrayCollection([$game1, $game2])
164
        ])])
165
      ])])
166
    ]);
167
168
169
    /** @var EntityManagerInterface $em */
170
    $em = $this->getEntityManagerMockForQuery([$tournament], /** @lang DQL */
171
      "SELECT t FROM Tfboe\FmLib\Entity\TournamentInterface t INNER JOIN t.competitions c INNER JOIN c.teams te " .
172
      "INNER JOIN te.memberships m WHERE m.player = (:id)");
173
    /** @var LoadingServiceInterface|MockObject $ls */
174
    $ls = $this->createMock(LoadingServiceInterface::class);
175
    $ls->expects(self::once())->method('loadEntities')->with([$tournament])->willReturnCallback(
176
      function ($a) use ($competition) {
177
        /** @var TournamentInterface|MockObject $t */
178
        $t = $a[0];
179
        $t->method('getCompetitions')->willReturn(new ArrayCollection([$competition]));
0 ignored issues
show
Bug introduced by
The method method() does not exist on Tfboe\FmLib\Entity\TournamentInterface. Did you maybe mean methodExists()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        $t->/** @scrutinizer ignore-call */ 
180
            method('getCompetitions')->willReturn(new ArrayCollection([$competition]));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
180
      });
181
182
    /** @var RankingSystemServiceInterface|MockObject $rankingSystemService */
183
    $rankingSystemService = $this->createMock(RankingSystemServiceInterface::class);
184
    $rankingSystemService->method('adaptOpenSyncFromValues')->with($tournament, []);
185
186
    $service = new PlayerService($em, $ls, $rankingSystemService);
187
188
    self::assertEquals(true, $service->mergePlayers($player1, $player2));
189
190
    self::assertEquals($game1PlayersB, new ArrayCollection([$player1->getId() => $player1]));
191
    self::assertEquals($game2PlayersA, new ArrayCollection([$player1->getId() => $player1]));
192
  }
193
//</editor-fold desc="Public Methods">
194
}