Passed
Pull Request — master (#41)
by Thomas
03:10
created

MockTrait::emExpectInsert()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
ccs 20
cts 20
cp 1
rs 8.439
cc 5
eloc 19
nc 2
nop 3
crap 5
1
<?php
2
3
namespace ORM;
4
5
use Mockery as m;
6
use Mockery\Mock;
7
use ORM\Exception\IncompletePrimaryKey;
8
9
trait MockTrait
10
{
11
12
    /**
13
     * Initialize an EntityManager mock object
14
     *
15
     * The mock is partial and you can map and act with it as usual. You should overwrite your dependency injector
16
     * with the returned mock object. You can also call `defineFor*()` on this mock to use this mock for specific
17
     * classes.
18
     *
19
     * The PDO object is mocked too. This object should not receive any calls except for quoting. By default it
20
     * accepts `quote(string)`, `setAttribute(*)` and `getAttribute(ATTR_DRIVER_NAME)`. To retrieve and expect other
21
     * calls you can use `getConnection()` from EntityManager mock object.
22
     *
23
     * @param array  $options Options passed to EntityManager constructor
24
     * @param string $driver  Database driver you are using (results in different dbal instance)
25
     * @return Mock|EntityManager
26
     */
27 18
    public function emInitMock($options = [], $driver = 'mysql')
28
    {
29
        /** @var EntityManager|Mock $em */
30 18
        $em = m::mock(EntityManager::class, [$options])->makePartial();
31
        /** @var \PDO|Mock $pdo */
32 18
        $pdo = m::mock(\PDO::class);
33
34 18
        $pdo->shouldReceive('setAttribute')->andReturn(true)->byDefault();
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\Mock, but not in PDO.

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 18
        $pdo->shouldReceive('getAttribute')->with(\PDO::ATTR_DRIVER_NAME)->andReturn($driver)->byDefault();
36
        $pdo->shouldReceive('quote')->with(stringValue())->andReturnUsing(function ($str) {
37 1
            return '\'' . addcslashes($str, '\'') . '\'';
38 18
        })->byDefault();
39
40 18
        $em->setConnection($pdo);
0 ignored issues
show
Bug introduced by
The method setConnection does only exist in ORM\EntityManager, but not in Mockery\Mock.

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 18
        return $em;
42
    }
43
44
    /**
45
     * Create a partial mock of Entity $class
46
     *
47
     * @param string        $class
48
     * @param array         $data
49
     * @param EntityManager $em
50
     * @return Mock|Entity
51
     */
52 2
    public function emCreateMockedEntity($class, $data = [], $em = null)
53
    {
54
        /** @var Entity|Mock $entity */
55 2
        $entity = \Mockery::mock($class)->makePartial();
56 2
        $entity->setEntityManager($em ?: EntityManager::getInstance($class));
0 ignored issues
show
Bug introduced by
The method setEntityManager does only exist in ORM\Entity, but not in Mockery\Mock.

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...
57 2
        $entity->setOriginalData($data);
0 ignored issues
show
Bug introduced by
The method setOriginalData does only exist in ORM\Entity, but not in Mockery\Mock.

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...
58 2
        $entity->reset();
0 ignored issues
show
Bug introduced by
The method reset does only exist in ORM\Entity, but not in Mockery\Mock.

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...
59 2
        return $entity;
60
    }
61
62
    /**
63
     * Expect an insert for $class
64
     *
65
     * Mocks and expects the calls to sync and insert as they came for `save()` method for a new Entity.
66
     *
67
     * If you omit the auto incremented id in defaultValues it is set to a random value between 1 and 2147483647.
68
     *
69
     * The EntityManager gets determined the same way as in Entity and can be overwritten by third parameter here.
70
     *
71
     * @param string        $class         The class that should get created
72
     * @param array         $defaultValues The default values that came from database (for example: the created column
73
     *                                     has by the default the current timestamp; the id is auto incremented...)
74
     * @param EntityManager $em
75
     * @throws Exception
76
     */
77 5
    public function emExpectInsert($class, $defaultValues = [], $em = null)
78
    {
79
        /** @var EntityManager|Mock $em */
80 5
        $em = $em ?: EntityManager::getInstance($class);
81
82 5
        $em->shouldReceive('sync')->with(m::type($class))->once()
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\Mock, but not in ORM\EntityManager.

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...
83
            ->andReturnUsing(function (Entity $entity, $reset = false) use ($class, $defaultValues, $em) {
1 ignored issue
show
Unused Code introduced by
The parameter $reset is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84 4
                $expectation = $em->shouldReceive('insert')->once()
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\Mock, but not in ORM\EntityManager.

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...
85 4
                    ->andReturnUsing(function (Entity $entity, $useAutoIncrement = true) use ($defaultValues, $em) {
86 4
                        if ($useAutoIncrement && !isset($defaultValues[$entity::getPrimaryKeyVars()[0]])) {
87 1
                            $defaultValues[$entity::getPrimaryKeyVars()[0]] = mt_rand(1, pow(2, 31) - 1);
88
                        }
89 4
                        $entity->setOriginalData(array_merge($defaultValues, $entity->getData()));
90 4
                        $entity->reset();
91 4
                        $em->map($entity);
0 ignored issues
show
Bug introduced by
The method map does only exist in ORM\EntityManager, but not in Mockery\Mock.

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...
92 4
                        return true;
93 4
                    });
94
95
                try {
96 4
                    $entity->getPrimaryKey();
97 1
                    $expectation->with(m::type($class), false);
98 1
                    return false;
99 3
                } catch (IncompletePrimaryKey $ex) {
100 3
                    $expectation->with(m::type($class));
101 3
                    throw $ex;
102
                }
103 5
            });
104 5
    }
105
106
    /**
107
     * Expect fetch for $class
108
     *
109
     * Mocks and expects an EntityFetcher with $entities as result.
110
     *
111
     * @param string        $class    The class that should be fetched
112
     * @param array         $entities The entities that get returned from fetcher
113
     * @param EntityManager $em
114
     * @return Mock
115
     * @throws Exception
116
     */
117 5
    public function emExpectFetch($class, $entities = [], $em = null)
118
    {
119
        /** @var EntityManager|Mock $em */
120 5
        $em = $em ?: EntityManager::getInstance($class);
121
122 5
        $fetcher = \Mockery::mock(EntityFetcher::class, [$em, $class])->makePartial();
123 5
        $em->shouldReceive('fetch')->with($class)->once()->andReturn($fetcher);
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\Mock, but not in ORM\EntityManager.

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...
124
125 5
        $fetcher->shouldReceive('count')->with()->andReturn(count($entities))->byDefault();
126 5
        array_push($entities, null);
127 5
        $fetcher->shouldReceive('one')->with()->andReturnValues($entities)->byDefault();
128
129 5
        return $fetcher;
130
    }
131
}
132