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

ServiceProviderTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testRegister() 0 16 1
A provider() 0 7 2
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\Providers;
12
13
use Illuminate\Contracts\Foundation\Application;
14
use PHPUnit\Framework\MockObject\MockObject;
15
use ReflectionException;
16
use Tfboe\FmLib\Providers\ServiceProvider;
17
use Tfboe\FmLib\TestHelpers\UnitTestCase;
18
19
/**
20
 * Class BaseControllerTest
21
 * @package Tests\Unit\App\Http\Controllers
22
 */
23
class ServiceProviderTest extends UnitTestCase
24
{
25
  //tests also private method disable this tests as soon as all are used in public interfaces
26
//<editor-fold desc="Public Methods">
27
28
  /**
29
   * @covers \Tfboe\FmLib\Providers\ServiceProvider::register
30
   * @throws ReflectionException
31
   * @throws ReflectionException
32
   */
33
  public function testRegister()
34
  {
35
    $app = $this->createMock(Application::class);
36
37
    $app->expects(self::exactly(3))->method('singleton')->withConsecutive(
38
      ['Interface', 'Implementation'],
39
      ['ClassInterface', 'Class'],
40
      ['OtherClass']
41
    );
42
43
    $provider = $this->provider($app);
44
    $prop = static::getProperty(ServiceProvider::class, 'singletons');
45
    $prop->setValue($provider, ['Interface' => 'Implementation', 'ClassInterface', 'OtherClass']);
46
47
    $provider->register();
0 ignored issues
show
Bug introduced by
The method register does only exist in Tfboe\FmLib\Providers\ServiceProvider, 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...
48
  }
49
//</editor-fold desc="Public Methods">
50
51
//<editor-fold desc="Private Methods">
52
  /**
53
   * @param null|Application|MockObject $app
54
   * @return MockObject|ServiceProvider
55
   * @throws ReflectionException
56
   */
57
  private function provider($app = null): MockObject
58
  {
59
    if ($app === null) {
60
      $app = $this->createMock(Application::class);
61
    }
62
    return $this->getMockForAbstractClass(ServiceProvider::class, [$app]);
63
  }
64
//</editor-fold desc="Private Methods">
65
}