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

UUIDEntityTest::mock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
eloc 2
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 1/3/18
7
 * Time: 10:39 AM
8
 */
9
10
namespace Tfboe\FmLib\Tests\Unit\Entity\Helpers;
11
12
13
use PHPUnit\Framework\MockObject\MockObject;
14
use Tfboe\FmLib\Entity\Helpers\UUIDEntity;
15
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
16
17
18
/**
19
 * Class BaseEntityTest
20
 * @package Tfboe\FmLib\Tests\Unit\Entity\Helpers
21
 */
22
class UUIDEntityTest extends UnitTestCase
23
{
24
//<editor-fold desc="Public Methods">
25
  /**
26
   * @covers \Tfboe\FmLib\Entity\Helpers\UUIDEntity::getId
27
   * @uses   \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
28
   */
29
  public function testId()
30
  {
31
    $entity = $this->mock();
32
    /** @noinspection PhpUnhandledExceptionInspection */
33
    self::getProperty(get_class($entity), 'id')->setValue($entity, 'test-id');
34
    self::assertEquals('test-id', $entity->getId());
0 ignored issues
show
Bug introduced by
The method getId does only exist in Tfboe\FmLib\Entity\Helpers\UUIDEntity, 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...
35
  }
36
37
  /**
38
   * @covers \Tfboe\FmLib\Entity\Helpers\UUIDEntity::getEntityId
39
   * @uses   \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
40
   */
41 View Code Duplication
  public function testEntityId()
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...
42
  {
43
    $entity = $this->mock();
44
    /** @noinspection PhpUnhandledExceptionInspection */
45
    self::getProperty(get_class($entity), 'id')->setValue($entity, 'test-id');
46
    self::assertEquals('test-id', $entity->getEntityId());
0 ignored issues
show
Bug introduced by
The method getEntityId does only exist in Tfboe\FmLib\Entity\Helpers\UUIDEntity, 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
  }
48
//</editor-fold desc="Public Methods">
49
50
//<editor-fold desc="Private Methods">
51
  /**
52
   * @return MockObject|UUIDEntity
53
   */
54
  private function mock(): MockObject
55
  {
56
    return $this->getMockForTrait(UUIDEntity::class);
57
  }
58
//</editor-fold desc="Private Methods">
59
}