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

PoC Property Hooks support, what is missing still? #11852

Draft
wants to merge 1 commit into
base: 3.4.x
Choose a base branch
from
Draft
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
15 changes: 14 additions & 1 deletion tests/Tests/Models/PropertyHooks/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
// phpcs:ignoreFile
namespace Doctrine\Tests\Models\PropertyHooks;

use DateTime;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;

#[Entity]
#[Table(name: 'property_hooks_user')]
class User
{
#[Id, GeneratedValue, Column(type: Types::INTEGER)]
public ?int $id;

#[Column(type: Types::STRING)]
public string $first {
set {
if (strlen($value) === 0) {
Expand All @@ -15,6 +26,7 @@ class User
}
}

#[Column(type: Types::STRING)]
public string $last {
set {
if (strlen($value) === 0) {
Expand All @@ -34,6 +46,7 @@ class User
}
}

#[Column(type: Types::STRING)]
public string $language = 'de' {
// Override the "read" action with arbitrary logic.
get => strtoupper($this->language);
Expand Down
62 changes: 62 additions & 0 deletions tests/Tests/ORM/Functional/PropertyHooksTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

Check failure on line 1 in tests/Tests/ORM/Functional/PropertyHooksTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Missing declare(strict_types=1).

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\PropertyHooks\User;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\RequiresPhp;

#[RequiresPhp('>= 8.4.0')]
class PropertyHooksTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(
User::class,
);
}

public function testMapPropertyHooks(): void
{
$user = new User();

Check failure on line 23 in tests/Tests/ORM/Functional/PropertyHooksTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space
$user->fullName = 'John Doe';
$user->language = 'EN';

$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();

$user = $this->_em->find(User::class, $user->id);

self::assertEquals('John', $user->first);
self::assertEquals('Doe', $user->last);
self::assertEquals('John Doe', $user->fullName);
self::assertEquals('EN', $user->language, 'The property hook uppercases the language.');

$language = $this->_em->createQuery('SELECT u.language FROM ' . User::class . ' u WHERE u.id = :id')
->setParameter('id', $user->id)
->getSingleScalarResult();

$this->assertEquals('en', $language, 'Selecting a field from DQL does not go through the property hook, accessing raw data.');
}

public function testTriggerLazyLoadingWhenAccessingPropertyHooks(): void
{
$user = new User();

Check failure on line 47 in tests/Tests/ORM/Functional/PropertyHooksTest.php

View workflow job for this annotation

GitHub Actions / coding-standards / Coding Standards (PHP: 8.4)

Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space
$user->fullName = 'Ludwig von Beethoven';
$user->language = 'DE';

$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();

$user = $this->_em->getReference(User::class, $user->id);

self::assertEquals('John', $user->first);
self::assertEquals('Doe', $user->last);
self::assertEquals('John Doe', $user->fullName);
self::assertEquals('EN', $user->language, 'The property hook uppercases the language.');
}
}
Loading