shouldPrepareOnMetadataLoad()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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\Tests\Hydration\EntityExample;
16
use Addiks\RDMBundle\Mapping\ChoiceMapping;
17
use Addiks\RDMBundle\ValueResolver\ValueResolverInterface;
0 ignored issues
show
Bug introduced by
The type Addiks\RDMBundle\ValueRe...\ValueResolverInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Doctrine\ORM\EntityManagerInterface;
19
use Doctrine\ORM\Mapping\ClassMetadata;
20
use Doctrine\DBAL\Connection;
21
use Doctrine\DBAL\Query\QueryBuilder;
22
use Doctrine\ORM\Query\Expr;
23
use Doctrine\DBAL\Driver\Statement;
24
use PHPUnit\Framework\TestCase;
25
use Doctrine\ORM\UnitOfWork;
26
use Addiks\RDMBundle\Tests\Hydration\ServiceExample;
27
use Addiks\RDMBundle\Mapping\MappingInterface;
28
use Addiks\RDMBundle\Mapping\EntityMappingInterface;
29
use Doctrine\DBAL\Schema\Column;
30
use Doctrine\ORM\Mapping\ClassMetadataFactory;
31
use Doctrine\DBAL\Types\Type;
32
33
final class SimpleSelectLoaderTest extends TestCase
34
{
35
36
    /**
37
     * @var SimpleSelectDataLoader
38
     */
39
    private $dataLoader;
40
41
    /**
42
     * @var MappingDriverInterface
43
     */
44
    private $mappingDriver;
45
46
    /**
47
     * @var ValueResolverInterface
48
     */
49
    private $valueResolver;
0 ignored issues
show
introduced by
The private property $valueResolver is not used, and could be removed.
Loading history...
50
51
    /**
52
     * @var ClassMetadata
53
     */
54
    private $classMetaData;
55
56
    /**
57
     * @var Connection
58
     */
59
    private $connection;
60
61
    /**
62
     * @var QueryBuilder
63
     */
64
    private $queryBuilder;
65
66
    /**
67
     * @var Expr
68
     */
69
    private $expr;
70
71
    /**
72
     * @var Statement
73
     */
74
    private $statement;
75
76
    /**
77
     * @var EntityManagerInterface
78
     */
79
    private $entityManager;
80
81
    /**
82
     * @var UnitOfWork
83
     */
84
    private $unitOfWork;
85
86
    /**
87
     * @var EntityMappingInterface
88
     */
89
    private $entityMapping;
90
91
    /**
92
     * @var ChoiceMapping
93
     */
94
    private $mappings = array();
95
96
    public function setUp(): void
97
    {
98
        $this->mappingDriver = $this->createMock(MappingDriverInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...DriverInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping...\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...
99
        $this->connection = $this->createMock(Connection::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...DBAL\Connection::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type 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...
100
        $this->queryBuilder = $this->createMock(QueryBuilder::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...ry\QueryBuilder::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type 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...
101
        $this->expr = $this->createMock(Expr::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctrine\ORM\Query\Expr::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type 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...
102
        $this->statement = $this->createMock(Statement::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...river\Statement::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type 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...
103
        $this->entityManager = $this->createMock(EntityManagerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctri...anagerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type 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...
104
        $this->unitOfWork = $this->createMock(UnitOfWork::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Doctrine\ORM\UnitOfWork::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type 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...
105
        $this->entityMapping = $this->createMock(EntityMappingInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Addiks...appingInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Addiks\RDMBundle\Mapping\EntityMappingInterface of property $entityMapping.

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...
106
107
        $this->dataLoader = new SimpleSelectDataLoader(
108
            $this->mappingDriver,
109
            1
0 ignored issues
show
Unused Code introduced by
The call to Addiks\RDMBundle\DataLoa...taLoader::__construct() has too many arguments starting with 1. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        $this->dataLoader = /** @scrutinizer ignore-call */ new SimpleSelectDataLoader(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
110
        );
111
112
        $this->classMetaData = new ClassMetadata(EntityExample::class);
113
        $this->classMetaData->table = ['name' => 'some_table'];
114
        $this->classMetaData->identifier = ["id"];
115
        $this->classMetaData->fieldMappings = [
116
            "id" => [
117
                'columnName' => 'id'
118
            ]
119
        ];
120
121
        $this->mappings['foo'] = $this->createMock(MappingInterface::class);
122
        $this->mappings['bar'] = $this->createMock(MappingInterface::class);
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->entityManager->method('getMetadataFactory')->willReturn($this->createMock(ClassMetadataFactory::class));
129
        $this->connection->method('createQueryBuilder')->willReturn($this->queryBuilder);
130
        $this->queryBuilder->method('expr')->willReturn($this->expr);
131
        $this->queryBuilder->method('execute')->willReturn($this->statement);
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function loadsDataFromDatabase()
138
    {
139
        /** @var array $expectedData */
140
        $expectedData = array(
141
            'foo_column' => 'Lorem ipsum',
142
            'bar_column' => 'dolor sit amet'
143
        );
144
145
        $this->classMetaData->identifier = ["id", "secondId"];
146
        $this->classMetaData->fieldMappings = [
147
            "id"  => ['columnName' => 'id'],
148
            "secondId"  => ['columnName' => 'secondId'],
149
            "faz" => ['columnName' => 'faz']
150
        ];
151
152
        $this->queryBuilder->expects($this->once())->method('from')->with($this->equalTo('some_table'));
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\DBAL\Query\QueryBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

152
        $this->queryBuilder->/** @scrutinizer ignore-call */ 
153
                             expects($this->once())->method('from')->with($this->equalTo('some_table'));

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->exactly(2))->method('andWhere')->with($this->equalTo("*eq-return*"));
154
        $this->queryBuilder->expects($this->exactly(2))->method('addSelect');
155
        $this->queryBuilder->expects($this->once())->method('setMaxResults')->with($this->equalTo(1));
156
157
        $this->statement->method('fetch')->willReturn($expectedData);
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\DBAL\Driver\Statement. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        $this->statement->/** @scrutinizer ignore-call */ 
158
                          method('fetch')->willReturn($expectedData);

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...
158
159
        /** @var ServiceExample $fazService */
160
        $fazService = $this->createMock(ServiceExample::class);
161
162
        $entity = new EntityExample(null, null, null, $fazService, null, "second_id");
163
        $entity->id = "123";
164
165
        $this->expr->expects($this->exactly(2))->method("eq")->willReturn("*eq-return*")->withConsecutive(
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\ORM\Query\Expr. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
        $this->expr->/** @scrutinizer ignore-call */ 
166
                     expects($this->exactly(2))->method("eq")->willReturn("*eq-return*")->withConsecutive(

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...
166
            ['id', '123'],
167
            ['secondId', "'second_id'"]
168
        );
169
170
        /** @var Column $columnA */
171
        $columnA = $this->createMock(Column::class);
172
        $columnA->method('getName')->willReturn("id");
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\DBAL\Schema\Column. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
        $columnA->/** @scrutinizer ignore-call */ 
173
                  method('getName')->willReturn("id");

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...
173
174
        /** @var Column $columnA */
175
        $columnB = $this->createMock(Column::class);
176
        $columnB->method('getName')->willReturn("secondId");
177
178
        $this->entityMapping->method('collectDBALColumns')->willReturn([
0 ignored issues
show
Bug introduced by
The method method() does not exist on Addiks\RDMBundle\Mapping\EntityMappingInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
        $this->entityMapping->/** @scrutinizer ignore-call */ 
179
                              method('collectDBALColumns')->willReturn([

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...
179
            $columnA,
180
            $columnB
181
        ]);
182
183
        /** @var array $actualData */
184
        $actualData = $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager);
185
186
        $this->assertEquals($expectedData, $actualData);
187
    }
188
189
    /**
190
     * @test
191
     */
192
    public function shouldNotLoadEntityWithoutId()
193
    {
194
        /** @var array $expectedData */
195
        $expectedData = array();
196
197
        $this->classMetaData->identifier = [];
198
199
        $this->queryBuilder->expects($this->never())->method('from');
200
201
        $this->statement->method('fetch')->willReturn($expectedData);
202
203
        $entity = new EntityExample();
204
        $entity->id = "some_id";
205
206
        $this->expr->method("eq")->willReturn("*eq-return*");
0 ignored issues
show
Bug introduced by
The method method() does not exist on Doctrine\ORM\Query\Expr. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        $this->expr->/** @scrutinizer ignore-call */ 
207
                     method("eq")->willReturn("*eq-return*");

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...
207
208
        /** @var array $actualData */
209
        $actualData = $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager);
210
211
        $this->assertEquals($expectedData, $actualData);
212
    }
213
214
    /**
215
     * @test
216
     */
217
    public function storesDataInDatabase()
218
    {
219
        $this->mappings['faz'] = $this->createMock(MappingInterface::class);
220
221
        $this->setUp();
222
223
        $this->classMetaData->identifier = ["id", "faz"];
224
        $this->classMetaData->fieldMappings = [
225
            "id"  => ['columnName' => 'id'],
226
            "faz" => ['columnName' => 'faz']
227
        ];
228
229
        /** @var ServiceExample $fazService */
230
        $fazService = $this->createMock(ServiceExample::class);
231
232
        $entity = new EntityExample(null, null, null, $fazService);
233
        $entity->id = "some_id";
234
        $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 Addiks\RDMBundle\Tests\Hydration\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...
235
        $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 Addiks\RDMBundle\Tests\Hydration\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...
236
237
        $this->mappings['foo']->method("revertValue")->willReturn(["foo_column" => "ipsum"]);
238
        $this->mappings['bar']->method("revertValue")->willReturn(["bar_column" => "dolor"]);
239
        $this->mappings['faz']->method("revertValue")->willReturn(["faz_column" => "sit"]);
240
241
        $this->entityMapping->method('getEntityClassName')->willReturn(EntityExample::class);
242
        $this->entityMapping->method('getFieldMappings')->willReturn($this->mappings);
243
        $this->entityMapping->method('revertValue')->willReturn([
244
            'foo_column' => 'ipsum',
245
            'bar_column' => 'dolor',
246
            'faz_column' => 'sit'
247
        ]);
248
249
        $this->connection->expects($this->once())->method("update")->with(
250
            $this->equalTo("some_table"),
251
            $this->equalTo([
252
                'foo_column' => 'ipsum',
253
                'bar_column' => 'dolor',
254
                'faz_column' => 'sit'
255
            ]),
256
            $this->equalTo([
257
                'id'  => "some_id",
258
                'faz' => $fazService
259
            ])
260
        );
261
262
        $this->dataLoader->storeDBALDataForEntity($entity, $this->entityManager);
263
    }
264
265
    /**
266
     * @test
267
     */
268
    public function shouldNotUpdateIfDataDidNotChange()
269
    {
270
        $entity = new EntityExample();
271
        $entity->id = "some_id";
272
        $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 Addiks\RDMBundle\Tests\Hydration\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...
273
        $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 Addiks\RDMBundle\Tests\Hydration\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...
274
275
        $this->entityMapping->method('getEntityClassName')->willReturn(EntityExample::class);
276
        $this->entityMapping->method('getFieldMappings')->willReturn($this->mappings);
277
        $this->entityMapping->method('collectDBALColumns')->willReturn([
278
            new Column("foo_column", Type::getType('string')),
279
            new Column("bar_column", Type::getType('string')),
280
        ]);
281
282
        $this->mappings['foo']->method("revertValue")->willReturn(["foo_column" => "ipsum"]);
283
        $this->mappings['bar']->method("revertValue")->willReturn(["bar_column" => "dolor"]);
284
285
        /** @var array $map */
286
        $map = array(
0 ignored issues
show
Unused Code introduced by
The assignment to $map is dead and can be removed.
Loading history...
287
            "some_ipsum_service" => ["foo_column" => "ipsum"],
288
            "some_dolor_service" => ["bar_column" => "dolor"],
289
        );
290
291
#        $this->valueResolver->method("revertValue")->will($this->returnCallback(
292
#            function ($fieldMapping, $context, $value) use ($map) {
293
#                return $map[$value];
294
#            }
295
#        ));
296
297
#        $this->valueResolver->method("revertValue")->will($this->returnValueMap([
298
#            [$this->mappings['foo'], $entity, "some_ipsum_service", ["foo_column" => "ipsum"]],
299
#            [$this->mappings['bar'], $entity, "some_dolor_service", ["bar_column" => "dolor"]],
300
#        ]));
301
302
        $this->connection->expects($this->never())->method("update");
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Doctrine\DBAL\Connection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

302
        $this->connection->/** @scrutinizer ignore-call */ 
303
                           expects($this->never())->method("update");

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...
303
304
        /** @var array $expectedData */
305
        $expectedData = [
306
            'foo_column' => 'ipsum',
307
            'bar_column' => 'dolor'
308
        ];
309
310
        $this->statement->method('fetch')->willReturn($expectedData);
311
312
        $this->dataLoader->loadDBALDataForEntity($entity, $this->entityManager);
313
        $this->dataLoader->storeDBALDataForEntity($entity, $this->entityManager);
314
    }
315
316
    /**
317
     * @test
318
     */
319
    public function shouldRemoveDBALForEntity()
320
    {
321
        $this->dataLoader->removeDBALDataForEntity(new EntityExample(), $this->entityManager);
322
        $this->assertTrue(true);
323
    }
324
325
    /**
326
     * @test
327
     */
328
    public function shouldPrepareOnMetadataLoad()
329
    {
330
        $this->dataLoader->prepareOnMetadataLoad($this->entityManager, $this->classMetaData);
331
        $this->assertTrue(true);
332
    }
333
334
}
335