Skip to content

Commit 1bd716f

Browse files
TavoNiievezyesdevnullNaktibalda
authored
Support for Codeception 5 (#53)
Co-authored-by: Dan Barrett <[email protected]> Co-authored-by: Gintautas Miselis <[email protected]>
1 parent 8f6c868 commit 1bd716f

File tree

6 files changed

+121
-32
lines changed

6 files changed

+121
-32
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88

99
strategy:
1010
matrix:
11-
php: [7.4, 8.0, 8.1]
11+
php: [8.0, 8.1]
1212

1313
steps:
1414
- name: Checkout code

composer.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
"name":"Alex Kunin"
1414
}
1515
],
16-
"minimum-stability": "RC",
16+
"minimum-stability": "dev",
1717
"require": {
18-
"php": "^7.4 | ^8.0",
18+
"php": "^8.0",
1919
"ext-pdo": "*",
2020
"ext-json": "*",
21-
"codeception/codeception": "^4.0"
21+
"codeception/codeception": "^5.0.0-alpha1"
2222
},
2323
"require-dev": {
24-
"doctrine/annotations": "^1",
25-
"doctrine/data-fixtures": "^1",
24+
"codeception/stub": "^4.0",
25+
"doctrine/annotations": "^1.13",
26+
"doctrine/data-fixtures": "^1.5",
2627
"doctrine/orm": "^2.10",
27-
"phpstan/phpstan": "^0.12",
28-
"ramsey/uuid-doctrine": "^1.5",
28+
"phpstan/phpstan": "^1.0",
29+
"ramsey/uuid-doctrine": "^1.6",
2930
"symfony/cache": "^4.4 | ^5.4 | ^6.0"
3031
},
3132
"autoload": {

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 6
5+
level: 3
66
paths:
77
- src
88
- tests
99
excludePaths:
1010
analyse:
1111
- tests/data/doctrine2_fixtures/TestFixture1.php
1212
- tests/data/doctrine2_fixtures/TestFixture2.php
13+
- tests/_support/UnitTester.php
1314
checkMissingIterableValueType: false
1415
reportUnmatchedIgnoredErrors: false
1516
ignoreErrors:

src/Codeception/Module/Doctrine2.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Codeception\Lib\Interfaces\DependsOnModule;
1212
use Codeception\Lib\Interfaces\DoctrineProvider;
1313
use Codeception\Module as CodeceptionModule;
14+
use Codeception\Stub;
1415
use Codeception\TestInterface;
1516
use Codeception\Util\ReflectionPropertyAccessor;
16-
use Codeception\Util\Stub;
1717
use Doctrine\Common\Collections\Criteria;
1818
use Doctrine\Common\Collections\Expr\Expression;
1919
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
@@ -168,7 +168,7 @@ class Doctrine2 extends CodeceptionModule implements DependsOnModule, DataMapper
168168

169169
public ?EntityManagerInterface $em = null;
170170

171-
public function _depends()
171+
public function _depends(): array
172172
{
173173
if ($this->config['connection_callback']) {
174174
return [];
@@ -177,26 +177,17 @@ public function _depends()
177177
return [DoctrineProvider::class => $this->dependencyMessage];
178178
}
179179

180-
/**
181-
* @return void
182-
*/
183-
public function _inject(DoctrineProvider $dependentModule = null)
180+
public function _inject(DoctrineProvider $dependentModule = null): void
184181
{
185182
$this->dependentModule = $dependentModule;
186183
}
187184

188-
/**
189-
* @return void
190-
*/
191-
public function _beforeSuite($settings = [])
185+
public function _beforeSuite($settings = []): void
192186
{
193187
$this->retrieveEntityManager();
194188
}
195189

196-
/**
197-
* @return void
198-
*/
199-
public function _before(TestInterface $test)
190+
public function _before(TestInterface $test): void
200191
{
201192
$this->cleanupEntityManager();
202193
}
@@ -836,7 +827,7 @@ private function populateEmbeddables(object $entityObject, array $data): void
836827
* @param array $params
837828
* @return void
838829
*/
839-
public function seeInRepository($entity, $params = [])
830+
public function seeInRepository(string $entity, array $params = []): void
840831
{
841832
$res = $this->proceedSeeInRepository($entity, $params);
842833
$this->assert($res);
@@ -849,7 +840,7 @@ public function seeInRepository($entity, $params = [])
849840
* @param array $params
850841
* @return void
851842
*/
852-
public function dontSeeInRepository($entity, $params = [])
843+
public function dontSeeInRepository(string $entity, array $params = []): void
853844
{
854845
$res = $this->proceedSeeInRepository($entity, $params);
855846
$this->assertNot($res);
@@ -891,7 +882,7 @@ protected function proceedSeeInRepository(string $entity, array $params = []): a
891882
* @param array $params
892883
* @return mixed
893884
*/
894-
public function grabFromRepository($entity, $field, $params = [])
885+
public function grabFromRepository(string $entity, string $field, array $params = [])
895886
{
896887
// we need to store to database...
897888
$this->em->flush();
@@ -995,11 +986,7 @@ protected function _buildAssociationQuery(QueryBuilder $qb, string $assoc, strin
995986
}
996987
}
997988

998-
/**
999-
* @return \Doctrine\ORM\EntityManagerInterface
1000-
* @throws ModuleConfigException
1001-
*/
1002-
public function _getEntityManager()
989+
public function _getEntityManager(): EntityManagerInterface
1003990
{
1004991
if (is_null($this->em)) {
1005992
$this->retrieveEntityManager();
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Util;
6+
7+
use InvalidArgumentException;
8+
use ReflectionClass;
9+
use ReflectionException;
10+
use function array_key_exists;
11+
use function get_class;
12+
use function get_parent_class;
13+
use function gettype;
14+
use function is_object;
15+
16+
class ReflectionPropertyAccessor
17+
{
18+
/**
19+
* @return mixed
20+
* @throws ReflectionException
21+
*/
22+
public function getProperty(object $obj, string $field)
23+
{
24+
$class = get_class($obj);
25+
do {
26+
$reflectedEntity = new ReflectionClass($class);
27+
if ($reflectedEntity->hasProperty($field)) {
28+
$property = $reflectedEntity->getProperty($field);
29+
$property->setAccessible(true);
30+
return $property->getValue($obj);
31+
}
32+
$class = get_parent_class($class);
33+
} while ($class);
34+
throw new InvalidArgumentException('Property "' . $field . '" does not exists in class "' . get_class($obj) . '" and its parents');
35+
}
36+
37+
/**
38+
* @throws ReflectionException
39+
*/
40+
private function setPropertiesForClass(?object $obj, string $class, array $data): object
41+
{
42+
$reflectedEntity = new ReflectionClass($class);
43+
44+
if ($obj === null) {
45+
$constructorParameters = [];
46+
$constructor = $reflectedEntity->getConstructor();
47+
if (null !== $constructor) {
48+
foreach ($constructor->getParameters() as $parameter) {
49+
if ($parameter->isOptional()) {
50+
$constructorParameters[] = $parameter->getDefaultValue();
51+
} elseif (array_key_exists($parameter->getName(), $data)) {
52+
$constructorParameters[] = $data[$parameter->getName()];
53+
} else {
54+
throw new InvalidArgumentException(
55+
'Constructor parameter "'.$parameter->getName().'" missing'
56+
);
57+
}
58+
}
59+
}
60+
61+
$obj = $reflectedEntity->newInstance(...$constructorParameters);
62+
}
63+
64+
foreach ($reflectedEntity->getProperties() as $property) {
65+
if (isset($data[$property->name])) {
66+
$property->setAccessible(true);
67+
$property->setValue($obj, $data[$property->name]);
68+
}
69+
}
70+
return $obj;
71+
}
72+
73+
/**
74+
* @throws ReflectionException
75+
*/
76+
public function setProperties(?object $obj, array $data): void
77+
{
78+
if (!$obj || !is_object($obj)) {
79+
throw new InvalidArgumentException('Cannot set properties for "' . gettype($obj) . '", expecting object');
80+
}
81+
$class = get_class($obj);
82+
do {
83+
$obj = $this->setPropertiesForClass($obj, $class, $data);
84+
$class = get_parent_class($class);
85+
} while ($class);
86+
}
87+
88+
/**
89+
* @throws ReflectionException
90+
*/
91+
public function createWithProperties(string $class, array $data): object
92+
{
93+
$obj = null;
94+
do {
95+
$obj = $this->setPropertiesForClass($obj, $class, $data);
96+
$class = get_parent_class($class);
97+
} while ($class);
98+
return $obj;
99+
}
100+
}

tests/unit/Codeception/Module/Doctrine2Test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Codeception\Exception\ModuleException;
66
use Codeception\Lib\ModuleContainer;
77
use Codeception\Module\Doctrine2;
8+
use Codeception\Stub;
89
use Codeception\Test\Unit;
9-
use Codeception\Util\Stub;
1010
use Doctrine\Common\Collections\Criteria;
1111
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
1212
use Doctrine\Common\DataFixtures\Loader;

0 commit comments

Comments
 (0)