TournamentHierarchyEntityTest::testConstructor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 10/1/17
7
 * Time: 12:52 PM
8
 */
9
10
namespace Tfboe\FmLib\Tests\Unit\Entity\Helpers;
11
12
use Doctrine\Common\Collections\Collection;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity;
15
use Tfboe\FmLib\Entity\RankingSystemInterface;
16
use Tfboe\FmLib\Tests\Entity\RankingSystem;
17
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
18
19
/**
20
 * Class BaseEntityTest
21
 * @package Tfboe\FmLib\Tests\Unit\Entity\Helpers
22
 */
23
class TournamentHierarchyEntityTest extends UnitTestCase
24
{
25
//<editor-fold desc="Public Methods">
26
  /**
27
   * @covers \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct
28
   * @uses   \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems
29
   */
30
  public function testConstructor()
31
  {
32
    $entity = $this->entity();
33
    self::assertInstanceOf(Collection::class, $entity->getRankingSystems());
0 ignored issues
show
Bug introduced by
The method getRankingSystems does only exist in Tfboe\FmLib\Entity\Helpe...urnamentHierarchyEntity, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
34
    self::assertEquals(0, $entity->getRankingSystems()->count());
35
  }
36
37
  /**
38
   * @covers \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::getRankingSystems
39
   * @uses   \Tfboe\FmLib\Entity\Helpers\TournamentHierarchyEntity::__construct
40
   */
41
  public function testRankingSystems()
42
  {
43
    $entity = $this->entity();
44
    /** @var $system RankingSystemInterface */
45
    $system = $this->createStubWithId(RankingSystem::class);
46
    $entity->getRankingSystems()->set($system->getId(), $system);
0 ignored issues
show
Bug introduced by
The method getRankingSystems does only exist in Tfboe\FmLib\Entity\Helpe...urnamentHierarchyEntity, but not in PHPUnit\Framework\MockObject\MockObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
47
    self::assertEquals(1, $entity->getRankingSystems()->count());
48
    self::assertEquals($system, $entity->getRankingSystems()[$system->getId()]);
49
  }
50
//</editor-fold desc="Public Methods">
51
52
//<editor-fold desc="Private Methods">
53
  /**
54
   * @return MockObject|TournamentHierarchyEntity
55
   */
56
  private function entity(): MockObject
57
  {
58
    return $this->getMockForAbstractClass(TournamentHierarchyEntity::class);
59
  }
60
//</editor-fold desc="Private Methods">
61
}