Skip to content

Commit 4c65f98

Browse files
committed
Drop support for older versions, upgrade phpunit and tests
1 parent ebadf03 commit 4c65f98

10 files changed

Lines changed: 160 additions & 592 deletions

.github/workflows/php.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,27 @@ jobs:
99
strategy:
1010
matrix:
1111
include:
12-
- { php-version: '7.3', orm-version: '2.7', fixtures-version: '1.4' }
13-
- { php-version: '7.4', orm-version: '2.7', fixtures-version: '1.4' }
14-
- { php-version: '8.0', orm-version: '2.7', fixtures-version: '1.4' }
1512
- { php-version: '8.1', orm-version: '2.7', fixtures-version: '1.4' }
1613
- { php-version: '8.2', orm-version: '2.7', fixtures-version: '1.4' }
1714
- { php-version: '8.3', orm-version: '2.7', fixtures-version: '1.4' }
1815
- { php-version: '8.4', orm-version: '2.7', fixtures-version: '1.4' }
16+
- { php-version: '8.5', orm-version: '2.7', fixtures-version: '1.4' }
1917

2018
- { php-version: '8.1', orm-version: '3.0', fixtures-version: '1.4' }
2119
- { php-version: '8.2', orm-version: '3.0', fixtures-version: '1.4' }
2220
- { php-version: '8.4', orm-version: '3.0', fixtures-version: '1.4' }
21+
- { php-version: '8.5', orm-version: '3.0', fixtures-version: '1.4' }
2322

2423
- { php-version: '8.1', orm-version: '2.7', fixtures-version: '2.0' }
2524
- { php-version: '8.2', orm-version: '2.7', fixtures-version: '2.0' }
2625
- { php-version: '8.3', orm-version: '2.7', fixtures-version: '2.0' }
2726
- { php-version: '8.4', orm-version: '2.7', fixtures-version: '2.0' }
27+
- { php-version: '8.5', orm-version: '2.7', fixtures-version: '2.0' }
2828

2929
- { php-version: '8.1', orm-version: '3.0', fixtures-version: '2.0' }
3030
- { php-version: '8.2', orm-version: '3.0', fixtures-version: '2.0' }
3131
- { php-version: '8.4', orm-version: '3.0', fixtures-version: '2.0' }
32+
- { php-version: '8.5', orm-version: '3.0', fixtures-version: '2.0' }
3233

3334
name: PHP ${{ matrix.php-version }} / ORM ${{ matrix.orm-version }} / Fixtures ${{ matrix.fixtures-version }}
3435
steps:

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# v1.4.0
2+
3+
* Drop support for PHP <8.1
4+
* Ensure PHP 8.5 compatibility
5+
* Upgrade test suite to PHPUnit 10 and update tests to be compatible
6+
17
# v1.3.6
28

39
* Ensure PHP 8.4 compatibility

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
}
1414
],
1515
"require": {
16-
"php": "^7.3|^8.0",
16+
"php": "^8.1",
1717
"doctrine/data-fixtures": "^1.4|^2.0",
1818
"doctrine/instantiator": "^1.3|^2.0",
1919
"doctrine/persistence": "^1.3|^2.0|^3.0|^4.0"
2020
},
2121
"require-dev": {
2222
"doctrine/orm": "^2.7|^3.0",
2323
"doctrine/mongodb-odm": "^2.2",
24-
"phpunit/phpunit": "^9.5",
24+
"phpunit/phpunit": "^10.5",
2525
"phpstan/phpstan": "^1.11",
2626
"phpstan/phpstan-deprecation-rules": "^1.2",
2727
"phpstan/phpstan-doctrine": "^1.4",

tests/ArrayFixtureTest.php

Lines changed: 148 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as ODMClassMetadata;
1919
use Doctrine\ORM\EntityManagerInterface;
2020
use Doctrine\ORM\Mapping\ClassMetadata as ORMClassMetadata;
21-
use Doctrine\Persistence\Mapping\ClassMetadata as ClassMetadataInterface;
21+
use Doctrine\Persistence\ObjectManager;
2222
use InvalidArgumentException;
23+
use PHPUnit\Framework\Constraint\Callback;
24+
use PHPUnit\Framework\Constraint\IsInstanceOf;
25+
use PHPUnit\Framework\MockObject\MockObject;
2326
use PHPUnit\Framework\TestCase;
2427
use RuntimeException;
2528
use Tests\Orbitale\Component\ArrayFixture\Fixtures\CustomNumberOfFlushesFixtureStub;
@@ -29,9 +32,6 @@
2932
use Tests\Orbitale\Component\ArrayFixture\Fixtures\PostSelfReferenceFixtureStub;
3033
use Tests\Orbitale\Component\ArrayFixture\Fixtures\PostTitleFixtureStub;
3134
use Tests\Orbitale\Component\ArrayFixture\Fixtures\ToStringPrefixFixtureStub;
32-
use Tests\Orbitale\Component\ArrayFixture\Stubs\DocumentManagerStub;
33-
use Tests\Orbitale\Component\ArrayFixture\Stubs\EntityManagerStub;
34-
use Tests\Orbitale\Component\ArrayFixture\Stubs\ObjectManagerStub;
3535
use Tests\Orbitale\Component\ArrayFixture\Stubs\PostStub;
3636
use Tests\Orbitale\Component\ArrayFixture\Stubs\ReferenceRepositoryStub;
3737

@@ -41,34 +41,51 @@ public function test post title fixture(): void
4141
{
4242
$manager = $this->getObjectManager();
4343

44-
(new PostTitleFixtureStub())->load($manager);
44+
$manager->expects($this->once())->method('flush');
45+
$manager->expects($this->once())->method('persist')->with(
46+
new Callback(function ($stub) {
47+
return is_object($stub) && $stub instanceof PostStub && $stub->getTitle() === 'Default title';
48+
}),
49+
);
4550

46-
self::assertSame(1, $manager->getFlushed());
51+
//
4752

48-
$entities = $manager->getPersisted();
49-
self::assertCount(1, $entities);
50-
self::assertInstanceOf(PostStub::class, $entities[0]);
51-
self::assertSame('Default title', $entities[0]->getTitle());
53+
(new PostTitleFixtureStub())->load($manager);
5254
}
5355

5456
public function test post self reference fixture(): void
5557
{
5658
$manager = $this->getObjectManager();
57-
$refs = new ReferenceRepositoryStub($manager);
59+
60+
$titles = [
61+
'Default title',
62+
'Second title',
63+
];
64+
/** @var null|PostStub $lastStub */
65+
$lastStub = null;
66+
67+
$manager->expects($this->once())->method('flush');
68+
$manager->expects($this->exactly(2))->method('persist')->with(
69+
new Callback(function ($stub) use (&$titles, &$lastStub) {
70+
if (!count($titles)) {
71+
self::fail('Persist was called too many times');
72+
}
73+
$title = array_shift($titles);
74+
$result = is_object($stub)
75+
&& $stub instanceof PostStub
76+
&& $stub->getTitle() === $title
77+
&& ($lastStub ? $stub->getParent() === $lastStub : true)
78+
;
79+
$lastStub = $stub;
80+
return $result;
81+
}),
82+
);
83+
84+
//
5885

5986
$fixture = new PostSelfReferenceFixtureStub();
60-
$fixture->setReferenceRepository($refs);
87+
$fixture->setReferenceRepository(new ReferenceRepositoryStub($manager));
6188
$fixture->load($manager);
62-
63-
self::assertSame(1, $manager->getFlushed());
64-
65-
$entities = $manager->getPersisted();
66-
self::assertCount(2, $entities);
67-
self::assertInstanceOf(PostStub::class, $entities[0]);
68-
self::assertInstanceOf(PostStub::class, $entities[1]);
69-
self::assertSame('Default title', $entities[0]->getTitle());
70-
self::assertSame('Second title', $entities[1]->getTitle());
71-
self::assertSame($entities[0], $entities[1]->getParent());
7289
}
7390

7491
public function test inexistent property(): void
@@ -78,6 +95,8 @@ public function test inexistent property(): void
7895
$this->expectException(InvalidArgumentException::class);
7996
$this->expectExceptionMessage('Cannot set property "does_not_exist" to "Tests\Orbitale\Component\ArrayFixture\Stubs\PostStub" object since this property does not exist.');
8097

98+
//
99+
81100
(new InexistentPropertyFixtureStub())->load($manager);
82101
}
83102

@@ -86,19 +105,31 @@ public function test toString for prefix(): void
86105
$manager = $this->getObjectManager();
87106
$refsRepo = new ReferenceRepositoryStub($manager);
88107

108+
/** @var PostStub $persistedStub */
109+
$persistedStub = null;
110+
111+
$manager->expects($this->once())->method('flush');
112+
$manager->expects($this->once())->method('persist')->with(
113+
new Callback(function ($stub) use (&$persistedStub) {
114+
$result = is_object($stub) && $stub instanceof PostStub && $stub->getTitle() === 'Default title';
115+
if ($result) {
116+
$persistedStub = $stub;
117+
}
118+
return $result;
119+
}),
120+
);
121+
122+
//
123+
89124
$fixture = new ToStringPrefixFixtureStub();
90125
$fixture->setReferenceRepository($refsRepo);
91126
$fixture->load($manager);
92127

93-
self::assertSame(1, $manager->getFlushed());
128+
//
94129

95-
$entities = $manager->getPersisted();
96-
self::assertCount(1, $entities);
97-
self::assertInstanceOf(PostStub::class, $entities[0]);
98-
self::assertSame('Default title', $entities[0]->getTitle());
99130
$refs = $refsRepo->getReferences();
100131
self::assertArrayHasKey('post-Default title', $refs);
101-
self::assertSame($refs['post-Default title'], $entities[0]);
132+
self::assertSame($refs['post-Default title'], $persistedStub);
102133
}
103134

104135
public function test inexistent prefix method(): void
@@ -108,68 +139,135 @@ public function test inexistent prefix method(): void
108139
$this->expectException(RuntimeException::class);
109140
$this->expectExceptionMessage('If you want to specify a reference with prefix "post-", method "getInexistentField" or "__toString()" must exist in the class, or you can override the "getMethodNameForReference" method and add your own.');
110141

142+
//
143+
111144
(new InexistentMethodPrefixFixtureStub())->load($manager);
112145
}
113146

114147
public function test custom number of flushes(): void
115148
{
116149
$manager = $this->getObjectManager();
117150

118-
$fixture = new CustomNumberOfFlushesFixtureStub();
119-
$fixture->load($manager);
120-
121-
self::assertSame(5, $manager->getFlushed());
122-
self::assertCount(20, $manager->getPersisted());
151+
$manager->expects($this->exactly(5))->method('flush');
152+
$manager->expects($this->exactly(20))->method('persist');
153+
154+
//
155+
156+
$manager->expects($this->exactly(5))->method('flush');
157+
$manager->expects($this->exactly(20))->method('persist')->with(
158+
new Callback(function ($stub) use (&$persistedStubs) {
159+
$result = is_object($stub) && $stub instanceof PostStub;
160+
if ($result) {
161+
$persistedStubs[] = $stub;
162+
}
163+
return $result;
164+
}),
165+
);
166+
167+
//
168+
169+
(new CustomNumberOfFlushesFixtureStub())->load($manager);
170+
self::assertCount(20, $persistedStubs);
171+
for ($i = 0; $i < 20; $i++) {
172+
self::assertSame('Title'.$i, $persistedStubs[$i]->getTitle());
173+
self::assertNull($persistedStubs[$i]->getParent());
174+
}
123175
}
124176

125177
public function test entities with ids(): void
126178
{
127179
$entityManager = $this->getEntityManager();
128180

129-
$fixture = new ObjectsWithIdsStub();
130-
$fixture->load($entityManager);
131-
132-
self::assertSame(1, $entityManager->getFlushed());
133-
self::assertCount(2, $entityManager->getPersisted());
181+
/** @var PostStub[] $persistedStubs */
182+
$persistedStubs = [];
183+
184+
$entityManager->expects($this->once())->method('flush')->withAnyParameters();
185+
$entityManager->expects($this->exactly(2))->method('persist')->with(
186+
new Callback(function ($stub) use (&$persistedStubs) {
187+
$result = is_object($stub) && $stub instanceof PostStub;
188+
if ($result) {
189+
$persistedStubs[] = $stub;
190+
}
191+
return $result;
192+
}),
193+
);
194+
195+
//
196+
197+
(new ObjectsWithIdsStub())->load($entityManager);
198+
self::assertSame('Default title', $persistedStubs[0]->getTitle());
199+
self::assertNull($persistedStubs[0]->getParent());
200+
self::assertSame('Another title', $persistedStubs[1]->getTitle());
201+
self::assertNull($persistedStubs[1]->getParent());
134202
}
135203

136204
public function test documents with ids(): void
137205
{
138206
$documentManager = $this->getDocumentManager();
139207

140-
$fixture = new ObjectsWithIdsStub();
141-
$fixture->load($documentManager);
208+
/** @var PostStub[] $persistedStubs */
209+
$persistedStubs = [];
210+
211+
$documentManager->expects($this->once())->method('flush')->withAnyParameters();
212+
$documentManager->expects($this->exactly(2))->method('persist')->with(
213+
new Callback(function ($stub) use (&$persistedStubs) {
214+
$result = is_object($stub) && $stub instanceof PostStub;
215+
if ($result) {
216+
$persistedStubs[] = $stub;
217+
}
218+
return $result;
219+
}),
220+
);
221+
142222

143-
self::assertSame(1, $documentManager->getFlushed());
144-
self::assertCount(2, $documentManager->getPersisted());
223+
//
224+
225+
(new ObjectsWithIdsStub())->load($documentManager);
226+
self::assertSame('Default title', $persistedStubs[0]->getTitle());
227+
self::assertNull($persistedStubs[0]->getParent());
228+
self::assertSame('Another title', $persistedStubs[1]->getTitle());
229+
self::assertNull($persistedStubs[1]->getParent());
145230
}
146231

147-
private function getObjectManager(): ObjectManagerStub
232+
/**
233+
* @return ObjectManager&MockObject
234+
*/
235+
private function getObjectManager(): ObjectManager
148236
{
149237
$metadata = $this->createMock(ORMClassMetadata::class);
150238
$metadata->method('getIdentifierFieldNames')->willReturn(['id']); // Default to "id" since composite are not supported yet.
151239

152-
return new ObjectManagerStub($metadata);
240+
$om = $this->createMock(ObjectManager::class);
241+
$om->method('getClassMetadata')->willReturn($metadata);
242+
243+
return $om;
153244
}
154245

246+
/**
247+
* @return EntityManagerInterface&MockObject
248+
*/
155249
private function getEntityManager(): EntityManagerInterface
156250
{
157251
$metadata = $this->createMock(ORMClassMetadata::class);
158252
$metadata->method('getIdentifierFieldNames')->willReturn(['id']); // Default to "id" since composite are not supported yet.
159253

160-
$driver = $this->createMock(Driver::class);
254+
$em = $this->createMock(EntityManagerInterface::class);
255+
$em->method('getClassMetadata')->willReturn($metadata);
161256

162-
$stub = new EntityManagerStub($metadata);
163-
$stub->setDriver($driver);
164-
165-
return $stub;
257+
return $em;
166258
}
167259

260+
/**
261+
* @return DocumentManager&MockObject
262+
*/
168263
private function getDocumentManager(): DocumentManager
169264
{
170265
$metadata = $this->createMock(ODMClassMetadata::class);
171266
$metadata->method('getIdentifierFieldNames')->willReturn(['id']); // Default to "id" since composite are not supported yet.
172267

173-
return new DocumentManagerStub($metadata);
268+
$dm = $this->createStub(DocumentManager::class);
269+
$dm->method('getClassMetadata')->willReturn($metadata);
270+
271+
return $dm;
174272
}
175273
}

tests/Stubs/EntityManagerStub.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)