Skip to content

Commit

Permalink
Remove skeleton-mapper usage from FunctionalTest
Browse files Browse the repository at this point in the history
  • Loading branch information
SenseException committed Jul 25, 2024
1 parent 7aa13d1 commit e6b3395
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 98 deletions.
19 changes: 0 additions & 19 deletions tests/DataSources/Users.php

This file was deleted.

59 changes: 3 additions & 56 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@

namespace Doctrine\StaticWebsiteGenerator\Tests;

use Doctrine\Common\EventManager;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\RST\Parser as RSTParser;
use Doctrine\SkeletonMapper\DataSource\DataSourceObjectDataRepository;
use Doctrine\SkeletonMapper\Hydrator\BasicObjectHydrator;
use Doctrine\SkeletonMapper\Mapping\ClassMetadataFactory;
use Doctrine\SkeletonMapper\Mapping\ClassMetadataInstantiator;
use Doctrine\SkeletonMapper\ObjectFactory;
use Doctrine\SkeletonMapper\ObjectIdentityMap;
use Doctrine\SkeletonMapper\ObjectManager;
use Doctrine\SkeletonMapper\ObjectRepository\ObjectRepositoryFactory;
use Doctrine\SkeletonMapper\ObjectRepository\ObjectRepositoryInterface;
use Doctrine\SkeletonMapper\Persister\ObjectPersisterFactory;
use Doctrine\StaticWebsiteGenerator\Controller\ControllerExecutor;
use Doctrine\StaticWebsiteGenerator\Controller\ControllerProvider;
use Doctrine\StaticWebsiteGenerator\Controller\ResponseFactory;
Expand All @@ -35,7 +23,6 @@
use Doctrine\StaticWebsiteGenerator\SourceFile\SourceFilesBuilder;
use Doctrine\StaticWebsiteGenerator\Tests\Controllers\HomepageController;
use Doctrine\StaticWebsiteGenerator\Tests\Controllers\UserController;
use Doctrine\StaticWebsiteGenerator\Tests\DataSources\Users;
use Doctrine\StaticWebsiteGenerator\Tests\Models\User;
use Doctrine\StaticWebsiteGenerator\Tests\Repositories\UserRepository;
use Doctrine\StaticWebsiteGenerator\Tests\Requests\UserRequests;
Expand All @@ -62,10 +49,10 @@ public function testBuild(): void

$responseFactory = new ResponseFactory();

$objectManager = $this->createObjectManager();
$user1 = new User('jwage');
$user2 = new User('ocramius');

$userRepository = $objectManager->getRepository(User::class);
assert($userRepository instanceof UserRepository);
$userRepository = new UserRepository([$user1, $user2]);

$controllerProvider = new ControllerProvider([
HomepageController::class => new HomepageController($userRepository, $responseFactory),
Expand Down Expand Up @@ -186,44 +173,4 @@ private function getFileContents(string $buildDir, string $file): string

return $contents;
}

private function createObjectManager(): ObjectManager
{
$objectRepositoryFactory = new ObjectRepositoryFactory();

$objectPersisterFactory = new ObjectPersisterFactory();

/** @var \Doctrine\Persistence\Mapping\ClassMetadataFactory<ClassMetadata<object>> $classMetadataFactory */
$classMetadataFactory = new ClassMetadataFactory(
new ClassMetadataInstantiator(),
);

$objectIdentityMap = new ObjectIdentityMap($objectRepositoryFactory);

$eventManager = new EventManager();

$objectManager = new ObjectManager(
$objectRepositoryFactory,
$objectPersisterFactory,
$objectIdentityMap,
$classMetadataFactory,
$eventManager,
);

$objectFactory = new ObjectFactory();
$objectHydrator = new BasicObjectHydrator($objectManager);
/** @var ObjectRepositoryInterface<object> $userRepository */
$userRepository = new UserRepository(
$objectManager,
new DataSourceObjectDataRepository($objectManager, new Users(), User::class),
$objectFactory,
$objectHydrator,
$eventManager,
User::class,
);

$objectRepositoryFactory->addObjectRepository(User::class, $userRepository);

return $objectManager;
}
}
18 changes: 2 additions & 16 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,10 @@

namespace Doctrine\StaticWebsiteGenerator\Tests\Models;

use Doctrine\SkeletonMapper\Hydrator\HydratableInterface;
use Doctrine\SkeletonMapper\Mapping\ClassMetadataInterface;
use Doctrine\SkeletonMapper\Mapping\LoadMetadataInterface;
use Doctrine\SkeletonMapper\ObjectManagerInterface;

class User implements HydratableInterface, LoadMetadataInterface
class User
{
private string $username;

public static function loadMetadata(ClassMetadataInterface $metadata): void
{
$metadata->setIdentifier(['username']);
}

/** @param mixed[] $project */
public function hydrate(array $project, ObjectManagerInterface $objectManager): void
public function __construct(private string $username)
{
$this->username = (string) ($project['username'] ?? '');
}

public function getUsername(): string
Expand Down
27 changes: 20 additions & 7 deletions tests/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@

namespace Doctrine\StaticWebsiteGenerator\Tests\Repositories;

use Doctrine\SkeletonMapper\ObjectRepository\BasicObjectRepository;
use Doctrine\StaticWebsiteGenerator\Tests\Models\User;

use function array_filter;
use function array_pop;
use function assert;

/**
* @template T of User
* @template-extends BasicObjectRepository<T>
*/
class UserRepository extends BasicObjectRepository
/** @template T of User */
class UserRepository
{
/** @param list<User> $users */
public function __construct(private array $users)
{
}

public function findOneByUsername(string $username): User
{
$user = $this->findOneBy(['username' => $username]);
$users = array_filter($this->findAll(), static function (User $user) use ($username) {
return $user->getUsername() === $username;
});
$user = array_pop($users);

assert($user instanceof User);

return $user;
}

/** @return User[] */
public function findAll(): array
{
return $this->users;
}
}

0 comments on commit e6b3395

Please sign in to comment.