Skip to content

Commit 3a50490

Browse files
Only load doctrine/persistence class alias when using Faker\ORM\Doctrine (#446)
* Only load doctrine/persistence class alias if classes exist * Only load class alias when using Faker\ORM\Doctrine classes. This will also add autoload for phpstan to register the alias. * Add tests to check if backward compatibility is working Each test will run in a separate process to make sure the autoload is done in every single one of them.
1 parent a5c05ef commit 3a50490

File tree

8 files changed

+83
-4
lines changed

8 files changed

+83
-4
lines changed

Diff for: composer.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
"autoload": {
2828
"psr-4": {
2929
"Faker\\": "src/Faker/"
30-
},
31-
"files": [
32-
"src/Faker/ORM/Doctrine/backward-compatibility.php"
33-
]
30+
}
3431
},
3532
"autoload-dev": {
3633
"psr-4": {

Diff for: phpstan.neon.dist

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ includes:
33

44
parameters:
55
level: 5
6+
bootstrapFiles:
7+
- src/Faker/ORM/Doctrine/backward-compatibility.php
68
paths:
79
- src
810
tmpDir: .build/phpstan/

Diff for: src/Faker/ORM/Doctrine/ColumnTypeGuesser.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
66
use Faker\Generator;
77

8+
require_once 'backward-compatibility.php';
9+
810
class ColumnTypeGuesser
911
{
1012
protected $generator;

Diff for: src/Faker/ORM/Doctrine/EntityPopulator.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
66
use Doctrine\Common\Persistence\ObjectManager;
77

8+
require_once 'backward-compatibility.php';
9+
810
/**
911
* Service class for populating a table through a Doctrine Entity class.
1012
*/

Diff for: src/Faker/ORM/Doctrine/Populator.php

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Doctrine\Common\Persistence\ObjectManager;
66
use Faker\Generator;
77

8+
require_once 'backward-compatibility.php';
9+
810
/**
911
* Service class for populating a database using the Doctrine ORM or ODM.
1012
* A Populator can populate several tables using ActiveRecord classes.

Diff for: test/Faker/ORM/Doctrine/ColumnTypeGuesserTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Faker\Test\ORM\Doctrine;
6+
7+
use Faker\ORM\Doctrine\ColumnTypeGuesser;
8+
use Faker\Test\TestCase;
9+
10+
final class ColumnTypeGuesserTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
*/
15+
public function testClassGenerationWithBackwardCompatibility(): void
16+
{
17+
$columnTypeGuesser = new ColumnTypeGuesser($this->faker);
18+
// Mock ClassMetadata after autoload to test class alias
19+
$classMetaData = $this->createMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
20+
$classMetaData->method('getTypeOfField')->with(self::anything())->willReturn('integer');
21+
22+
$fakerClosure = $columnTypeGuesser->guessFormat('test', $classMetaData);
23+
self::assertIsNumeric($fakerClosure());
24+
}
25+
}

Diff for: test/Faker/ORM/Doctrine/EntityPopulatorTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Faker\Test\ORM\Doctrine;
6+
7+
use Faker\ORM\Doctrine\EntityPopulator;
8+
use Faker\Test\TestCase;
9+
10+
final class EntityPopulatorTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
*/
15+
public function testClassGenerationWithBackwardCompatibility(): void
16+
{
17+
// trigger autoload before using to load backward compatibility fix
18+
class_exists(EntityPopulator::class);
19+
20+
$classMetaData = $this->createMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
21+
$classMetaData->method('getName')->willReturn('test');
22+
$entityPopulator = new EntityPopulator($classMetaData);
23+
24+
self::assertSame('test', $entityPopulator->getClass());
25+
}
26+
}

Diff for: test/Faker/ORM/Doctrine/PopulatorTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Faker\Test\ORM\Doctrine;
6+
7+
use Faker\ORM\Doctrine\Populator;
8+
use Faker\Test\TestCase;
9+
10+
final class PopulatorTest extends TestCase
11+
{
12+
/**
13+
* @runInSeparateProcess
14+
*/
15+
public function testClassGenerationWithBackwardCompatibility(): void
16+
{
17+
$populator = new Populator($this->faker);
18+
// Mock ObjectManager after autoload to test class alias
19+
$objectManager = $this->createMock('Doctrine\Common\Persistence\ObjectManager');
20+
21+
self::assertEmpty($populator->execute($objectManager));
22+
}
23+
}

0 commit comments

Comments
 (0)