Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only load doctrine/persistence class alias when using Faker\ORM\Doctrine #446

Merged
merged 3 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions composer.json
Expand Up @@ -27,10 +27,7 @@
"autoload": {
"psr-4": {
"Faker\\": "src/Faker/"
},
"files": [
"src/Faker/ORM/Doctrine/backward-compatibility.php"
]
}
},
"autoload-dev": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Expand Up @@ -3,6 +3,8 @@ includes:

parameters:
level: 5
bootstrapFiles:
- src/Faker/ORM/Doctrine/backward-compatibility.php
paths:
- src
tmpDir: .build/phpstan/
2 changes: 2 additions & 0 deletions src/Faker/ORM/Doctrine/ColumnTypeGuesser.php
Expand Up @@ -5,6 +5,8 @@
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Faker\Generator;

require_once 'backward-compatibility.php';

class ColumnTypeGuesser
{
protected $generator;
Expand Down
2 changes: 2 additions & 0 deletions src/Faker/ORM/Doctrine/EntityPopulator.php
Expand Up @@ -5,6 +5,8 @@
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\Common\Persistence\ObjectManager;

require_once 'backward-compatibility.php';

/**
* Service class for populating a table through a Doctrine Entity class.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Faker/ORM/Doctrine/Populator.php
Expand Up @@ -5,6 +5,8 @@
use Doctrine\Common\Persistence\ObjectManager;
use Faker\Generator;

require_once 'backward-compatibility.php';

/**
* Service class for populating a database using the Doctrine ORM or ODM.
* A Populator can populate several tables using ActiveRecord classes.
Expand Down
25 changes: 25 additions & 0 deletions test/Faker/ORM/Doctrine/ColumnTypeGuesserTest.php
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Faker\Test\ORM\Doctrine;

use Faker\ORM\Doctrine\ColumnTypeGuesser;
use Faker\Test\TestCase;

final class ColumnTypeGuesserTest extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testClassGenerationWithBackwardCompatibility(): void
{
$columnTypeGuesser = new ColumnTypeGuesser($this->faker);
// Mock ClassMetadata after autoload to test class alias
$classMetaData = $this->createMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
$classMetaData->method('getTypeOfField')->with(self::anything())->willReturn('integer');

$fakerClosure = $columnTypeGuesser->guessFormat('test', $classMetaData);
self::assertIsNumeric($fakerClosure());
}
}
26 changes: 26 additions & 0 deletions test/Faker/ORM/Doctrine/EntityPopulatorTest.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Faker\Test\ORM\Doctrine;

use Faker\ORM\Doctrine\EntityPopulator;
use Faker\Test\TestCase;

final class EntityPopulatorTest extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testClassGenerationWithBackwardCompatibility(): void
{
// trigger autoload before using to load backward compatibility fix
class_exists(EntityPopulator::class);

$classMetaData = $this->createMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
$classMetaData->method('getName')->willReturn('test');
$entityPopulator = new EntityPopulator($classMetaData);

self::assertSame('test', $entityPopulator->getClass());
}
}
23 changes: 23 additions & 0 deletions test/Faker/ORM/Doctrine/PopulatorTest.php
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Faker\Test\ORM\Doctrine;

use Faker\ORM\Doctrine\Populator;
use Faker\Test\TestCase;

final class PopulatorTest extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testClassGenerationWithBackwardCompatibility(): void
{
$populator = new Populator($this->faker);
// Mock ObjectManager after autoload to test class alias
$objectManager = $this->createMock('Doctrine\Common\Persistence\ObjectManager');

self::assertEmpty($populator->execute($objectManager));
}
}