Completed
Push — master ( 7e6070...203e38 )
by Gerrit
02:38
created

shouldRemoveDBALForEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Tests\DataLoader;
12
13
use Addiks\RDMBundle\DataLoader\SimpleSelectDataLoader;
14
use Addiks\RDMBundle\Mapping\Drivers\MappingDriverInterface;
15
use Addiks\RDMBundle\Mapping\EntityMapping;
16
use Addiks\RDMBundle\Tests\Hydration\EntityExample;
17
use Addiks\RDMBundle\Mapping\ChoiceMapping;
18
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Doctrine\ORM\Mapping\ClassMetadata;
21
use Doctrine\DBAL\Connection;
22
use Doctrine\DBAL\Query\QueryBuilder;
23
use Doctrine\ORM\Query\Expr;
24
use Doctrine\DBAL\Driver\Statement;
25
use PHPUnit\Framework\TestCase;
26
use Addiks\RDMBundle\Mapping\ServiceMapping;
27
use Doctrine\ORM\UnitOfWork;
28
use Addiks\RDMBundle\Tests\Hydration\ServiceExample;
29
30
final class DataSimpleSelectLoaderTest extends TestCase
31
{
32
33
    /**
34
     * @var SimpleSelectDataLoader
35
     */
36
    private $dataLoader;
37
38
    /**
39
     * @var MappingDriverInterface
40
     */
41
    private $mappingDriver;
42
43
    /**
44
     * @var ValueResolverInterface
45
     */
46
    private $valueResolver;
47
48
    /**
49
     * @var ClassMetadata
50
     */
51
    private $classMetaData;
52
53
    /**
54
     * @var Connection
55
     */
56
    private $connection;
57
58
    /**
59
     * @var QueryBuilder
60
     */
61
    private $queryBuilder;
62
63
    /**
64
     * @var Expr
65
     */
66
    private $expr;
67
68
    /**
69
     * @var Statement
70
     */
71
    private $statement;
72
73
    /**
74
     * @var EntityManagerInterface
75
     */
76
    private $entityManager;
77
78
    /**
79
     * @var UnitOfWork
80
     */
81
    private $unitOfWork;
82
83
    /**
84
     * @var EntityMapping
85
     */
86
    private $entityMapping;
87
88
    /**
89
     * @var ChoiceMapping
90
     */
91
    private $mappings = array();
92
93
    public function setUp()
94
    {
95
        $this->mappingDriver = $this->createMock(MappingDriverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...DriverInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\RDMBundle\...MappingDriverInterface> of property $mappingDriver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
96
        $this->valueResolver = $this->createMock(ValueResolverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Addik...solverInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Addiks\RDMBundle\...ValueResolverInterface> of property $valueResolver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
        $this->connection = $this->createMock(Connection::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...DBAL\Connection::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\DBAL\Connection> of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
98
        $this->queryBuilder = $this->createMock(QueryBuilder::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...ry\QueryBuilder::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\DBAL\Query\QueryBuilder> of property $queryBuilder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
99
        $this->expr = $this->createMock(Expr::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...\ORM\Query\Expr::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\Query\Expr> of property $expr.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
        $this->statement = $this->createMock(Statement::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...river\Statement::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\DBAL\Driver\Statement> of property $statement.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
101
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...anagerInterface::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\EntityManagerInterface> of property $entityManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
102
        $this->unitOfWork = $this->createMock(UnitOfWork::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Doctr...\ORM\UnitOfWork::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<Doctrine\ORM\UnitOfWork> of property $unitOfWork.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
103
104
        $this->dataLoader = new SimpleSelectDataLoader(
105
            $this->mappingDriver,
0 ignored issues
show
Documentation introduced by
$this->mappingDriver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...MappingDriverInterface>.

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...
106
            $this->valueResolver,
0 ignored issues
show
Documentation introduced by
$this->valueResolver is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\RDMBundle\...ValueResolverInterface>.

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...
107
            1
108
        );
109
110
        $this->classMetaData = new ClassMetadata(EntityExample::class);
111
        $this->classMetaData->table = ['name' => 'some_table'];
112
        $this->classMetaData->identifier = ["id"];
113
        $this->classMetaData->fieldMappings = [
114
            "id" => [
115
                'columnName' => 'id'
116
            ]
117
        ];
118
119
        $this->mappings['foo'] = new ChoiceMapping('foo_column', []);
120
        $this->mappings['bar'] = new ChoiceMapping('bar_column', []);
121
122
        $this->entityMapping = new EntityMapping(EntityExample::class, $this->mappings);
0 ignored issues
show
Documentation introduced by
$this->mappings is of type object<Addiks\RDMBundle\Mapping\ChoiceMapping>, but the function expects a array.

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...
123
124
        $this->mappingDriver->method("loadRDMMetadataForClass")->willReturn($this->entityMapping);
125
        $this->entityManager->method("getClassMetadata")->willReturn($this->classMetaData);
126
        $this->entityManager->method("getConnection")->willReturn($this->connection);
127
        $this->entityManager->method("getUnitOfWork")->willReturn($this->unitOfWork);
128
        $this->connection->method('createQueryBuilder')->willReturn($this->queryBuilder);
129
        $this->queryBuilder->method('expr')->willReturn($this->expr);
130
        $this->queryBuilder->method('execute')->willReturn($this->statement);
131
    }
132
133
    /**
134
     * @test
135
     */
136
    public function loadsDataFromDatabase()
137
    {
138
        /** @var array $expectedData */
139
        $expectedData = array(
140
            'foo_column' => 'Lorem ipsum',
141
            'bar_column' => 'dolor sit amet'
142
        );
143
144
        $this->classMetaData->identifier = ["id", "faz"];
145
        $this->classMetaData->fieldMappings = [
146
            "id"  => ['columnName' => 'id'],
147
            "faz" => ['columnName' => 'faz']
148
        ];
149
150
        $this->queryBuilder->expects($this->once())->method('from')->with($this->equalTo('some_table'));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\DBAL\Query\QueryBuilder>.

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...
151
        $this->queryBuilder->expects($this->exactly(2))->method('andWhere')->with($this->equalTo("*eq-return*"));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\DBAL\Query\QueryBuilder>.

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...
152
        $this->queryBuilder->expects($this->exactly(2))->method('addSelect');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\DBAL\Query\QueryBuilder>.

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...
153
        $this->queryBuilder->expects($this->once())->method('setMaxResults')->with($this->equalTo(1));
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\DBAL\Query\QueryBuilder>.

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...
154
155
        $this->statement->method('fetch')->willReturn($expectedData);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\DBAL\Driver\Statement>.

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...
156
157
        /** @var ServiceExample $fazService */
158
        $fazService = $this->createMock(ServiceExample::class);
159
160
        $entity = new EntityExample(null, null, null, $fazService);
161
        $entity->id = "some_id";
162
163
        $this->expr->method("eq")->willReturn("*eq-return*");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\ORM\Query\Expr>.

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...
164
165
        /** @var array $actualData */
166
        $actualData = $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager);
167
168
        $this->assertEquals($expectedData, $actualData);
169
    }
170
171
    /**
172
     * @test
173
     */
174
    public function shouldNotLoadEntityWithoutId()
175
    {
176
        /** @var array $expectedData */
177
        $expectedData = array();
178
179
        $this->classMetaData->identifier = [];
180
181
        $this->queryBuilder->expects($this->never())->method('from');
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\DBAL\Query\QueryBuilder>.

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...
182
183
        $this->statement->method('fetch')->willReturn($expectedData);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\DBAL\Driver\Statement>.

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...
184
185
        $entity = new EntityExample();
186
        $entity->id = "some_id";
187
188
        $this->expr->method("eq")->willReturn("*eq-return*");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\ORM\Query\Expr>.

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...
189
190
        /** @var array $actualData */
191
        $actualData = $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager);
192
193
        $this->assertEquals($expectedData, $actualData);
194
    }
195
196
    /**
197
     * @test
198
     */
199
    public function storesDataInDatabase()
200
    {
201
        $this->mappings['faz'] = new ChoiceMapping('faz_column', []);
202
203
        $this->setUp();
204
205
        $this->classMetaData->identifier = ["id", "faz"];
206
        $this->classMetaData->fieldMappings = [
207
            "id"  => ['columnName' => 'id'],
208
            "faz" => ['columnName' => 'faz']
209
        ];
210
211
        /** @var ServiceExample $fazService */
212
        $fazService = $this->createMock(ServiceExample::class);
213
214
        $entity = new EntityExample(null, null, null, $fazService);
215
        $entity->id = "some_id";
216
        $entity->foo = "some_ipsum_service";
0 ignored issues
show
Documentation Bug introduced by
It seems like 'some_ipsum_service' of type string is incompatible with the declared type object<Addiks\RDMBundle\...dration\ServiceExample> of property $foo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
217
        $entity->bar = "some_dolor_service";
0 ignored issues
show
Documentation Bug introduced by
It seems like 'some_dolor_service' of type string is incompatible with the declared type object<Addiks\RDMBundle\...dration\ServiceExample> of property $bar.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
218
219
        $this->valueResolver->method("revertValue")->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ValueResolverInterface>.

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...
220
            [$this->mappings['foo'], $entity, "some_ipsum_service", ["foo_column" => "ipsum"]],
221
            [$this->mappings['bar'], $entity, "some_dolor_service", ["bar_column" => "dolor"]],
222
            [$this->mappings['faz'], $entity, $fazService,          ["faz_column" => "sit"]],
223
        ]));
224
225
        $this->connection->expects($this->once())->method("update")->with(
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\DBAL\Connection>.

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...
226
            $this->equalTo("some_table"),
227
            $this->equalTo([
228
                'foo_column' => 'ipsum',
229
                'bar_column' => 'dolor',
230
                'faz_column' => 'sit'
231
            ]),
232
            $this->equalTo([
233
                'id'  => "some_id",
234
                'faz' => $fazService
235
            ])
236
        );
237
238
        $this->dataLoader->storeDBALDataForEntity($entity, $this->entityManager);
239
    }
240
241
    /**
242
     * @test
243
     */
244
    public function shouldNotUpdateIfDataDidNotChange()
245
    {
246
        $entity = new EntityExample();
247
        $entity->id = "some_id";
248
        $entity->foo = "some_ipsum_service";
0 ignored issues
show
Documentation Bug introduced by
It seems like 'some_ipsum_service' of type string is incompatible with the declared type object<Addiks\RDMBundle\...dration\ServiceExample> of property $foo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
249
        $entity->bar = "some_dolor_service";
0 ignored issues
show
Documentation Bug introduced by
It seems like 'some_dolor_service' of type string is incompatible with the declared type object<Addiks\RDMBundle\...dration\ServiceExample> of property $bar.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
250
251
        $this->valueResolver->method("revertValue")->will($this->returnValueMap([
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Addiks\RDMBundle\...ValueResolverInterface>.

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...
252
            [$this->mappings['foo'], $entity, "some_ipsum_service", ["foo_column" => "ipsum"]],
253
            [$this->mappings['bar'], $entity, "some_dolor_service", ["bar_column" => "dolor"]],
254
        ]));
255
256
        $this->connection->expects($this->never())->method("update");
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<Doctrine\DBAL\Connection>.

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...
257
258
        /** @var array $expectedData */
259
        $expectedData = [
260
            'foo_column' => 'ipsum',
261
            'bar_column' => 'dolor'
262
        ];
263
264
        $this->statement->method('fetch')->willReturn($expectedData);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Doctrine\DBAL\Driver\Statement>.

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...
265
266
        $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager);
267
        $this->dataLoader->storeDBALDataForEntity($entity, $this->entityManager);
268
    }
269
270
    /**
271
     * @test
272
     */
273
    public function shouldRemoveDBALForEntity()
274
    {
275
        $this->dataLoader->removeDBALDataForEntity(new EntityExample(), $this->entityManager);
276
        $this->assertTrue(true);
277
    }
278
279
    /**
280
     * @test
281
     */
282
    public function shouldPrepareOnMetadataLoad()
283
    {
284
        $this->dataLoader->prepareOnMetadataLoad($this->entityManager, $this->classMetaData);
285
        $this->assertTrue(true);
286
    }
287
288
}
289