Skip to content

Commit

Permalink
Merge pull request #628 from TomHAnderson/hotfix/test-illuminate-regi…
Browse files Browse the repository at this point in the history
…stry

Hotfix/test illuminate registry
  • Loading branch information
TomHAnderson authored Oct 26, 2024
2 parents 9e19f83 + 3f705f6 commit dfb0617
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 40 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
]
},
"autoload-dev": {
"psr-4": {
"LaravelDoctrineTest\\ORM\\": "tests/"
},
"files": [
"tests/helpers.php"
],
"classmap": [
"tests"
]
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration/Cache/PhpFileCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;

use function storage_path;

class PhpFileCacheProvider implements Driver
{
public function __construct(protected Repository $config)
Expand Down
1 change: 1 addition & 0 deletions src/Console/DumpDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Persistence\ManagerRegistry;
use Illuminate\Contracts\Config\Repository;

use function base_path;
use function exec;

class DumpDatabaseCommand extends Command
Expand Down
1 change: 1 addition & 0 deletions src/DoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

use function assert;
use function class_exists;
use function config_path;
use function property_exists;

class DoctrineServiceProvider extends ServiceProvider
Expand Down
56 changes: 30 additions & 26 deletions src/IlluminateRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use ReflectionClass;
use RuntimeException;
use Throwable;

use function count;
use function explode;
use function head;
use function reset;
use function sprintf;
use function strpos;

Expand Down Expand Up @@ -158,7 +158,7 @@ public function getDefaultManagerName(): string
*/
public function getManager(string|null $name = null): mixed
{
$name = $name ?: $this->getDefaultManagerName();
$name ??= $this->getDefaultManagerName();

if (! $this->managerExists($name)) {
throw new InvalidArgumentException(sprintf('Doctrine Manager named "%s" does not exist.', $name));
Expand Down Expand Up @@ -300,42 +300,42 @@ public function getRepository(string $persistentObject, string|null $persistentM
/**
* Gets the object manager associated with a given class.
*
* @param string $class A persistent object class name.
* @param class-string $className A persistent object class name.
*/
public function getManagerForClass(string $class): ObjectManager|null
public function getManagerForClass(string $className): ObjectManager|null
{
// Check for namespace alias
if (strpos($class, ':') !== false) {
[$namespaceAlias, $simpleClassName] = explode(':', $class, 2);
$class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
}

$proxyClass = new ReflectionClass($class);
if ($proxyClass->implementsInterface(Proxy::class)) {
$class = $proxyClass->getParentClass()->getName();
if (strpos($className, ':') !== false) {
[$namespaceAlias, $simpleClassName] = explode(':', $className, 2);
$className = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
}

$managerNames = $this->getManagerNames();
// Check for proxy class
try {
$proxyClass = new ReflectionClass($className);

if (count($managerNames) === 1) {
return $this->getManager(reset($managerNames));
if ($proxyClass->implementsInterface(Proxy::class)) {
$className = $proxyClass->getParentClass()->getName();
}
} catch (Throwable) {
throw new RuntimeException('Class ' . $className . ' is not a valid class name.');
}

foreach ($managerNames as $name) {
$manager = $this->getManager($name);

if ($manager->getMetadataFactory()->isTransient($class)) {
foreach ($this->getManagers() as $entityManager) {
if ($entityManager->getMetadataFactory()->isTransient($className)) {
// @codeCoverageIgnoreStart
continue;
// @codeCoverageIgnoreEnd
}

foreach ($manager->getMetadataFactory()->getAllMetadata() as $metadata) {
if ($metadata->getName() === $class) {
return $manager;
foreach ($entityManager->getMetadataFactory()->getAllMetadata() as $metadata) {
if ($metadata->getName() === $className) {
return $entityManager;
}
}
}

return null;
throw new RuntimeException('No manager found for class ' . $className);
}

/**
Expand Down Expand Up @@ -372,13 +372,17 @@ protected function getConnectionBindingName(string $connection): string
return self::CONNECTION_BINDING_PREFIX . $connection;
}

public function setDefaultManager(string $defaultManager): void
public function setDefaultManager(string $defaultManager): self
{
$this->defaultManager = $defaultManager;

return $this;
}

public function setDefaultConnection(string $defaultConnection): void
public function setDefaultConnection(string $defaultConnection): self
{
$this->defaultConnection = $defaultConnection;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Testing/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use function array_merge;
use function call_user_func;
use function database_path;
use function is_dir;

class Factory implements ArrayAccess
Expand Down
23 changes: 23 additions & 0 deletions tests/Entity/Scientist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace LaravelDoctrineTest\ORM\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: "App\Doctrine\ORM\Repository\ScientistRepository")]
class Scientist
{
#[ORM\Id]
#[ORM\Column(type: "integer")]
#[ORM\GeneratedValue(strategy: "AUTO")]
private $id;

#[ORM\Column(type: "string", nullable: true)]
private $firstName;

#[ORM\Column(type: "string", nullable: false)]
private $lastName;

#[ORM\OneToMany(targetEntity: \Theory::class, mappedBy: "scientist")]
private $theories;
}
21 changes: 21 additions & 0 deletions tests/Entity/Theory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace LaravelDoctrineTest\ORM\Entity;

use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: "App\Doctrine\ORM\Repository\TheoryRepository")]
class Theory
{
#[ORM\Id]
#[ORM\Column(type: "integer")]
#[ORM\GeneratedValue(strategy: "AUTO")]
private $id;

#[ORM\Column(type: "string", nullable: false)]
private $title;

#[ORM\ManyToOne(targetEntity: \Scientist::class, inversedBy: "theories")]
#[ORM\JoinColumn(name: "scientist_id", referencedColumnName: "id", nullable: false)]
private $scientist;
}
14 changes: 3 additions & 11 deletions tests/Feature/Configuration/Cache/FileCacheProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
class FileCacheProviderTest extends AbstractCacheProviderTest
{
public function getProvider()
{
{
$config = m::mock(Repository::class);
$config->shouldReceive('get')
->with('cache.stores.file.path', __DIR__ . DIRECTORY_SEPARATOR . '../../Stubs/storage/framework/cache')
->with('cache.stores.file.path', 'framework/cache')
->once()
->andReturn('/tmp');

$config->shouldReceive('get')
->with('doctrine.cache.namespace', 'doctrine-cache')
->once()
->andReturn('doctrine-cache');
->andReturn('doctrine-cache');

return new FileCacheProvider(
$config,
Expand All @@ -31,11 +31,3 @@ public function getExpectedInstance()
}
}

if(!function_exists('storage_path')) {
function storage_path($path = null)
{
$storage = __DIR__ . DIRECTORY_SEPARATOR . '../../Stubs/storage';

return is_null($path) ? $storage : $storage . DIRECTORY_SEPARATOR . $path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
class PhpFileCacheProviderTest extends AbstractCacheProviderTest
{
public function getProvider()
{
{
$config = m::mock(Repository::class);
$config->shouldReceive('get')
->with('cache.stores.file.path', __DIR__ . DIRECTORY_SEPARATOR . '../../Stubs/storage/framework/cache')
->with('cache.stores.file.path', 'framework/cache')
->once()
->andReturn('/tmp');

$config->shouldReceive('get')
->with('doctrine.cache.namespace', 'doctrine-cache')
->once()
->andReturn('doctrine-cache');
->andReturn('doctrine-cache');

return new PhpFileCacheProvider(
$config,
Expand Down
Loading

0 comments on commit dfb0617

Please sign in to comment.