Completed
Push — master ( ffb817...d11e08 )
by Christian
03:09
created

StatementRepositoryTest::testFindStatementById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace XApi\Repository\Doctrine\Tests\Unit\Repository;
13
14
use Xabbuh\XApi\DataFixtures\StatementFixtures;
15
use Xabbuh\XApi\DataFixtures\VerbFixtures;
16
use Xabbuh\XApi\Model\StatementsFilter;
17
use XApi\Repository\Api\Mapping\MappedStatement;
18
use XApi\Repository\Doctrine\Repository\StatementRepository;
19
20
/**
21
 * @author Christian Flothmann <[email protected]>
22
 */
23
class StatementRepositoryTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var \PHPUnit_Framework_MockObject_MockObject|\XApi\Repository\Doctrine\Repository\MappedStatementRepository
27
     */
28
    private $mappedStatementRepository;
29
30
    /**
31
     * @var StatementRepository
32
     */
33
    private $statementRepository;
34
35
    protected function setUp()
36
    {
37
        $this->mappedStatementRepository = $this->createMappedStatementRepositoryMock();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMappedStatementRepositoryMock() can also be of type object<XApi\Repository\D...pedStatementRepository>. However, the property $mappedStatementRepository is declared as type object<PHPUnit_Framework_MockObject_MockObject>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
38
        $this->statementRepository = new StatementRepository($this->mappedStatementRepository);
39
    }
40
41
    public function testFindStatementById()
42
    {
43
        $statementId = md5(uniqid());
44
        $this
45
            ->mappedStatementRepository
46
            ->expects($this->once())
47
            ->method('findMappedStatement')
48
            ->with(array('id' => $statementId))
49
            ->will($this->returnValue(MappedStatement::createFromModel(StatementFixtures::getMinimalStatement())));
50
51
        $this->statementRepository->findStatementById($statementId);
52
    }
53
54
    public function testFindStatementsByCriteria()
55
    {
56
        $verb = VerbFixtures::getVerb();
57
58
        $this
59
            ->mappedStatementRepository
60
            ->expects($this->once())
61
            ->method('findMappedStatements')
62
            ->with($this->equalTo(array('verb' => $verb->getId())))
63
            ->will($this->returnValue(array()));
64
65
        $filter = new StatementsFilter();
66
        $filter->byVerb($verb);
67
        $this->statementRepository->findStatementsBy($filter);
68
    }
69
70 View Code Duplication
    public function testSave()
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...
71
    {
72
        $statement = StatementFixtures::getMinimalStatement();
73
        $self = $this;
74
        $this
75
            ->mappedStatementRepository
76
            ->expects($this->once())
77
            ->method('storeMappedStatement')
78
            ->with(
79
                $this->callback(function (MappedStatement $mappedStatement) use ($self, $statement) {
80
                    return $self->assertMappedStatement(
81
                        MappedStatement::createFromModel($statement),
82
                        $mappedStatement
83
                    );
84
                }),
85
                true
86
            );
87
88
        $this->statementRepository->storeStatement($statement);
89
    }
90
91 View Code Duplication
    public function testSaveWithoutFlush()
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...
92
    {
93
        $statement = StatementFixtures::getMinimalStatement();
94
        $self = $this;
95
        $this
96
            ->mappedStatementRepository
97
            ->expects($this->once())
98
            ->method('storeMappedStatement')
99
            ->with(
100
                $this->callback(function (MappedStatement $mappedStatement) use ($self, $statement) {
101
                    return $self->assertMappedStatement(
102
                        MappedStatement::createFromModel($statement),
103
                        $mappedStatement
104
                    );
105
                }),
106
                false
107
            );
108
109
        $this->statementRepository->storeStatement($statement, false);
110
    }
111
112
    /**
113
     * @return \PHPUnit_Framework_MockObject_MockObject|\XApi\Repository\Doctrine\Repository\MappedStatementRepository
114
     */
115
    protected function createMappedStatementRepositoryMock()
116
    {
117
        return $this->getMock('\XApi\Repository\Doctrine\Repository\MappedStatementRepository');
118
    }
119
120
    /**
121
     * Ths method is only public to support PHP 5.3.
122
     */
123
    public function assertMappedStatement(MappedStatement $expected, MappedStatement $actual)
124
    {
125
        if ($expected->id !== $actual->id) {
126
            return false;
127
        }
128
129
        if (!$expected->actor->equals($actual->actor)) {
130
            return false;
131
        }
132
133
        if (!$expected->verb->equals($actual->verb)) {
134
            return false;
135
        }
136
137
        if (!$expected->object->equals($actual->object)) {
138
            return false;
139
        }
140
141
        if (null === $expected->result && null !== $actual->result) {
142
            return false;
143
        }
144
145
        if (null !== $expected->result && null === $actual->result) {
146
            return false;
147
        }
148
149
        if (null !== $expected->result && !$expected->result->equals($actual->result)) {
150
            return false;
151
        }
152
153
        if (null === $expected->authority && null !== $actual->authority) {
154
            return false;
155
        }
156
157
        if (null !== $expected->authority && null === $actual->authority) {
158
            return false;
159
        }
160
161
        if (null !== $expected->authority && !$expected->authority->equals($actual->authority)) {
162
            return false;
163
        }
164
165
        if ($expected->created != $actual->created) {
166
            return false;
167
        }
168
169
        if (null === $actual->stored) {
170
            return false;
171
        }
172
173
        return true;
174
    }
175
}
176