Test Failed
Push — master ( b194e9...e9cca5 )
by Valentin
03:22
created

FactoryTest::testEvalPromise()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Tests;
5
6
use Cycle\ORM\ORMInterface;
7
use Cycle\ORM\Promise\Factory;
8
use Cycle\ORM\Promise\MaterializerInterface;
9
use Cycle\ORM\Promise\Materizalizer\EvalMaterializer;
10
use Cycle\ORM\Promise\Materizalizer\FileMaterializer;
11
use Cycle\ORM\Promise\Tests\Fixtures\Entity;
12
use Cycle\ORM\SchemaInterface;
13
use PHPUnit\Framework\TestCase;
14
use Spiral\Core\Container;
15
16
class FactoryTest extends TestCase
17
{
18
    private const NS = 'Cycle\ORM\Promise\Tests\Promises';
19
20
    public function setUp()
21
    {
22
        $files = glob($this->filesDirectory() . DIRECTORY_SEPARATOR . '*');
23
        foreach ($files as $file) {
24
            if (is_file($file)) {
25
                unlink($file);
26
            }
27
        }
28
    }
29
30
    private function filesDirectory(): string
31
    {
32
        return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'promises';
33
    }
34
35
    public function testFilePromise(): void
36
    {
37
        $role = Entity::class;
38
39
        $orm = \Mockery::mock(ORMInterface::class);
40
        $schema = \Mockery::mock(SchemaInterface::class);
41
        $schema->shouldReceive('define')->andReturn($role);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
42
        $orm->shouldReceive('getSchema')->andReturn($schema);
43
44
        $container = new Container();
45
        $container->bind(ORMInterface::class, $orm);
46
47
        $materializer = $container->make(FileMaterializer::class, ['directory' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'promises']);
48
        $container->bind(MaterializerInterface::class, $materializer);
0 ignored issues
show
Documentation introduced by
$materializer is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
50
        /** @var Factory $factory */
51
        $factory = $container->get(Factory::class);
52
53
        /** @var Entity $promise */
54
        $promise = $factory->promise($orm, $role, []);
55
56
        $this->assertInstanceOf($role, $promise);
57
    }
58
59
    public function testEvalPromise(): void
60
    {
61
        $role = Entity::class;
62
63
        $orm = \Mockery::mock(ORMInterface::class);
64
        $schema = \Mockery::mock(SchemaInterface::class);
65
        $schema->shouldReceive('define')->andReturn($role);
0 ignored issues
show
Bug introduced by
The method andReturn does only exist in Mockery\ExpectationInterface, but not in Mockery\HigherOrderMessage.

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...
66
        $orm->shouldReceive('getSchema')->andReturn($schema);
67
68
        $container = new Container();
69
        $container->bind(ORMInterface::class, $orm);
70
71
        $materializer = $container->get(EvalMaterializer::class);
72
        $container->bind(MaterializerInterface::class, $materializer);
0 ignored issues
show
Documentation introduced by
$materializer is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
74
        /** @var Factory $factory */
75
        $factory = $container->get(Factory::class);
76
77
        /** @var Entity $promise */
78
        $promise = $factory->promise($orm, $role, []);
79
80
        $this->assertInstanceOf($role, $promise);
81
    }
82
}