Completed
Branch merging-leagues-tournaments (46817d)
by Benedikt
01:48
created

testAddSubscribers()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 1
nop 0
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Created by PhpStorm.
6
 * User: benedikt
7
 * Date: 9/17/17
8
 * Time: 12:33 AM
9
 */
10
11
namespace Tfboe\FmLib\Tests\Unit\Listeners;
12
13
use Doctrine\Common\EventManager;
14
use Doctrine\ORM\EntityManagerInterface;
15
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
16
use Illuminate\Support\Facades\Config;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use Tfboe\FmLib\Listeners\ResolveTargetEntityExtension;
19
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
20
21
/**
22
 * Class BaseControllerTest
23
 * @package Tests\Unit\App\Http\Controllers
24
 */
25
class ResolveTargetEntityExtensionTest extends UnitTestCase
26
{
27
  //tests also private method disable this tests as soon as all are used in public interfaces
28
//<editor-fold desc="Public Methods">
29
  /**
30
   * @covers \Tfboe\FmLib\Listeners\ResolveTargetEntityExtension::addSubscribers
31
   */
32
  public function testAddSubscribers()
33
  {
34
    $mapping = ['EntityInterface1' => 'RealEntity1', 'EntityInterface2' => 'RealEntity2'];
35
36
    /** @var MockObject|EventManager $manager */
37
    $manager = $this->createMock(EventManager::class);
38
    /** @var MockObject|EntityManagerInterface $em */
39
    $em = $this->createMock(EntityManagerInterface::class);
40
    $manager->expects(self::once())->method('addEventSubscriber')->willReturnCallback(
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit\Framework\MockObject\MockObject, but not in Doctrine\Common\EventManager.

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...
41
      function (ResolveTargetEntityListener $listener) use ($mapping) {
42
        $prop = static::getProperty(ResolveTargetEntityListener::class, 'resolveTargetEntities');
43
        $entities = $prop->getValue($listener);
44
        foreach ($mapping as $key => $val) {
45
          self::assertArrayHasKey($key, $entities);
46
          self::assertArrayIsSubset(['targetEntity' => $val], $entities[$key]);
47
        }
48
      });
49
50
    Config::shouldReceive('get')
51
      ->with('fm-lib.entityMaps', [])
52
      ->andReturn($mapping);
53
54
    $listener = $this->listener();
55
    $listener->addSubscribers($manager, $em);
0 ignored issues
show
Bug introduced by
It seems like $manager defined by $this->createMock(\Doctr...on\EventManager::class) on line 37 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tfboe\FmLib\Listeners\Re...nsion::addSubscribers() does only seem to accept object<Doctrine\Common\EventManager>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
It seems like $em defined by $this->createMock(\Doctr...anagerInterface::class) on line 39 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, Tfboe\FmLib\Listeners\Re...nsion::addSubscribers() does only seem to accept object<Doctrine\ORM\EntityManagerInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
  }
57
58
  /**
59
   * @covers \Tfboe\FmLib\Listeners\ResolveTargetEntityExtension::getFilters
60
   */
61
  public function testGetFilters()
62
  {
63
    $listeners = $this->listener();
64
    self::assertEquals([], $listeners->getFilters());
65
  }
66
//</editor-fold desc="Public Methods">
67
68
//<editor-fold desc="Private Methods">
69
  /**
70
   * @return ResolveTargetEntityExtension
71
   */
72
  private function listener(): ResolveTargetEntityExtension
73
  {
74
    return new ResolveTargetEntityExtension();
75
  }
76
//</editor-fold desc="Private Methods">
77
}