StatementRepositoryTest::testSave()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 20
Ratio 100 %

Importance

Changes 0
Metric Value
dl 20
loc 20
rs 9.6
c 0
b 0
f 0
cc 1
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 PHPUnit\Framework\TestCase;
15
use Rhumsaa\Uuid\Uuid as RhumsaUuid;
16
use Xabbuh\XApi\DataFixtures\StatementFixtures;
17
use Xabbuh\XApi\DataFixtures\VerbFixtures;
18
use Xabbuh\XApi\Model\StatementId;
19
use Xabbuh\XApi\Model\StatementsFilter;
20
use Xabbuh\XApi\Model\Uuid as ModelUuid;
21
use XApi\Repository\Doctrine\Mapping\Statement as MappedStatement;
22
use XApi\Repository\Doctrine\Repository\StatementRepository;
23
24
/**
25
 * @author Christian Flothmann <[email protected]>
26
 */
27
class StatementRepositoryTest extends TestCase
28
{
29
    /**
30
     * @var \PHPUnit_Framework_MockObject_MockObject|\XApi\Repository\Doctrine\Repository\Mapping\StatementRepository
31
     */
32
    private $mappedStatementRepository;
33
34
    /**
35
     * @var StatementRepository
36
     */
37
    private $statementRepository;
38
39
    protected function setUp()
40
    {
41
        $this->mappedStatementRepository = $this->createMappedStatementRepositoryMock();
42
        $this->statementRepository = new StatementRepository($this->mappedStatementRepository);
43
    }
44
45
    public function testFindStatementById()
46
    {
47
        if (class_exists('Xabbuh\XApi\Model\Uuid')) {
48
            $statementId = StatementId::fromUuid(ModelUuid::uuid4());
49
        } else {
50
            $statementId = StatementId::fromUuid(RhumsaUuid::uuid4());
51
        }
52
53
        $this
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<XApi\Repository\D...ng\StatementRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            ->mappedStatementRepository
55
            ->expects($this->once())
56
            ->method('findStatement')
57
            ->with(array('id' => $statementId->getValue()))
58
            ->will($this->returnValue(MappedStatement::fromModel(StatementFixtures::getMinimalStatement())));
59
60
        $this->statementRepository->findStatementById($statementId);
61
    }
62
63
    public function testFindStatementsByCriteria()
64
    {
65
        $verb = VerbFixtures::getTypicalVerb();
66
67
        $this
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<XApi\Repository\D...ng\StatementRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
            ->mappedStatementRepository
69
            ->expects($this->once())
70
            ->method('findStatements')
71
            ->with($this->equalTo(array('verb' => $verb->getId()->getValue())))
72
            ->will($this->returnValue(array()));
73
74
        $filter = new StatementsFilter();
75
        $filter->byVerb($verb);
76
        $this->statementRepository->findStatementsBy($filter);
77
    }
78
79 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...
80
    {
81
        $statement = StatementFixtures::getMinimalStatement();
82
        $this
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<XApi\Repository\D...ng\StatementRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
83
            ->mappedStatementRepository
84
            ->expects($this->once())
85
            ->method('storeStatement')
86
            ->with(
87
                $this->callback(function (MappedStatement $mappedStatement) use ($statement) {
88
                    $expected = MappedStatement::fromModel($statement);
89
                    $actual = clone $mappedStatement;
90
                    $actual->stored = null;
91
92
                    return $expected == $actual;
93
                }),
94
                true
95
            );
96
97
        $this->statementRepository->storeStatement($statement);
98
    }
99
100 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...
101
    {
102
        $statement = StatementFixtures::getMinimalStatement();
103
        $this
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<XApi\Repository\D...ng\StatementRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
            ->mappedStatementRepository
105
            ->expects($this->once())
106
            ->method('storeStatement')
107
            ->with(
108
                $this->callback(function (MappedStatement $mappedStatement) use ($statement) {
109
                    $expected = MappedStatement::fromModel($statement);
110
                    $actual = clone $mappedStatement;
111
                    $actual->stored = null;
112
113
                    return $expected == $actual;
114
                }),
115
                false
116
            );
117
118
        $this->statementRepository->storeStatement($statement, false);
119
    }
120
121
    /**
122
     * @return \PHPUnit_Framework_MockObject_MockObject|\XApi\Repository\Doctrine\Repository\Mapping\StatementRepository
123
     */
124
    protected function createMappedStatementRepositoryMock()
125
    {
126
        return $this
127
            ->getMockBuilder('\XApi\Repository\Doctrine\Repository\Mapping\StatementRepository')
128
            ->getMock();
129
    }
130
}
131