Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,29 @@
],
"require": {
"php": "^8.4",
"doctrine/orm": "^3.6",
"doctrine/doctrine-laminas-hydrator": "^3.7",
"doctrine/orm": "^2.20.9 || ^3.0",
"doctrine/doctrine-laminas-hydrator": "^3.2",
"webonyx/graphql-php": "^v15.29",
"psr/container": "^2.0",
"league/event": "^3.0",
"symfony/var-exporter": "^6.4 || ^7.0 || ^8.0"
},
"require-dev": {
"doctrine/coding-standard": "^14.0",
"doctrine/dbal": "^4.0",
"doctrine/dbal": "^3.1 || ^4.0",
"phpunit/phpunit": "^13.0",
"vimeo/psalm": "^6.14",
"vimeo/psalm": "^6.16.1",
"symfony/cache": "^7.0 || ^8.0",
"php-parallel-lint/php-parallel-lint": "^1.3.2",
"phpstan/phpstan": "^2.1"
"php-parallel-lint/php-parallel-lint": "^1.4",
"phpstan/phpstan": "^2.1.51",
"amphp/dns": "^v2.4",
"amphp/socket": "^v2.4",
"amphp/cache": "^v2.0.1",
"amphp/parser": "^v1.1.1",
"amphp/process": "^v2.0.3",
"symfony/console": "^8.0",
"spatie/array-to-xml": "^3.4.4",
"daverandom/libdns": "^2.1"
},
"suggest": {
"ramsey/uuid-doctrine": "Support for an UUID Doctrine type"
Expand Down
5 changes: 4 additions & 1 deletion test/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function count;
use function date;
use function file_get_contents;
use function method_exists;

require_once __DIR__ . '/../vendor/autoload.php';

Expand All @@ -29,7 +30,9 @@ public function setUp(): void
paths: [__DIR__ . '/Entity'],
isDevMode: true,
);
$config->enableNativeLazyObjects(true);
if (method_exists($config, 'enableNativeLazyObjects')) {
$config->enableNativeLazyObjects(true);
}

// database connection
$connection = DriverManager::getConnection([
Expand Down
29 changes: 13 additions & 16 deletions test/Unit/Hydrator/DoctrineObjectWithComputedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace ApiSkeletonsTest\Doctrine\ORM\GraphQL\Unit\Hydrator;

use ApiSkeletons\Doctrine\ORM\GraphQL\Hydrator\DoctrineObjectWithComputed;
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\TestCase;
use stdClass;
use ApiSkeletonsTest\Doctrine\ORM\GraphQL\Entity\Artist;
use ApiSkeletonsTest\Doctrine\ORM\GraphQL\TestCase;

use function strlen;
use function strtoupper;
Expand All @@ -16,13 +15,11 @@ class DoctrineObjectWithComputedTest extends TestCase
{
private DoctrineObjectWithComputed $hydrator;

protected function setUp(): void
public function setUp(): void
{
// Create a stub entity manager for testing (not used by the hydrator methods we're testing)
$entityManager = $this->createStub(EntityManager::class);
parent::setUp();

// DoctrineObjectWithComputed extends DoctrineObject which takes EntityManager and byValue flag
$this->hydrator = new DoctrineObjectWithComputed($entityManager, false);
$this->hydrator = new DoctrineObjectWithComputed($this->getEntityManager(), false);
}

public function testHasComputedFieldReturnsFalseWhenNotRegistered(): void
Expand Down Expand Up @@ -58,19 +55,19 @@ public function testGetComputedFieldNamesReturnsAllRegisteredFields(): void

public function testExtractIncludesComputedFields(): void
{
$entity = new stdClass();
$entity->name = 'Test Name';
$artist = $this->getEntityManager()
->getRepository(Artist::class)
->findOneBy(['name' => 'Grateful Dead']);

// Add computed field that transforms the name
$this->hydrator->addComputedField('uppercaseName', static fn ($obj) => strtoupper($obj->name));
$this->hydrator->addComputedField('nameLength', static fn ($obj) => strlen($obj->name));
$this->hydrator->addComputedField('uppercaseName', static fn ($obj) => strtoupper($obj->getName()));
$this->hydrator->addComputedField('nameLength', static fn ($obj) => strlen($obj->getName()));

$result = $this->hydrator->extract($entity);
$result = $this->hydrator->extract($artist);

$this->assertArrayHasKey('uppercaseName', $result);
$this->assertArrayHasKey('nameLength', $result);
$this->assertEquals('TEST NAME', $result['uppercaseName']);
$this->assertEquals(9, $result['nameLength']);
$this->assertEquals('GRATEFUL DEAD', $result['uppercaseName']);
$this->assertEquals(13, $result['nameLength']);
}

public function testAddComputedFieldAllowsMultipleFields(): void
Expand Down
Loading