Skip to content

Commit 85f0717

Browse files
Merge #290
290: Avoid using deprecated dbal `object` type r=brunoocasali a=norkunas # Pull Request ## Related issue Fixes #287 ## What does this PR do? - Introduces a custom doctrine type instead of using a deprecated one for testing an object as a primary key of test entity. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: Tomas <[email protected]>
2 parents ecd40c1 + 023a8fc commit 85f0717

File tree

7 files changed

+81
-7
lines changed

7 files changed

+81
-7
lines changed

.editorconfig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ indent_style = space
1111
indent_size = 4
1212
max_line_length = 120
1313

14-
[*.{yml,yaml,json}]
14+
[*.{yml,yaml}]
15+
indent_size = 4
16+
17+
[*.json]
1518
indent_size = 2
1619

1720
[*.{neon,neon.dist}]

tests/Dbal/Type/DummyObjectIdType.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Bundle\Tests\Dbal\Type;
6+
7+
use Doctrine\DBAL\Platforms\AbstractPlatform;
8+
use Doctrine\DBAL\Types\ConversionException;
9+
use Doctrine\DBAL\Types\Type;
10+
use Meilisearch\Bundle\Tests\Entity\ObjectId\DummyObjectId;
11+
12+
final class DummyObjectIdType extends Type
13+
{
14+
public function getName(): string
15+
{
16+
return 'dummy_object_id';
17+
}
18+
19+
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
20+
{
21+
return $platform->getIntegerTypeDeclarationSQL($column);
22+
}
23+
24+
public function convertToPHPValue($value, AbstractPlatform $platform): ?DummyObjectId
25+
{
26+
if ($value instanceof DummyObjectId || null === $value) {
27+
return $value;
28+
}
29+
30+
if (!\is_string($value) && !is_int($value)) {
31+
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]);
32+
}
33+
34+
return new DummyObjectId((int) $value);
35+
}
36+
37+
/**
38+
* @throws ConversionException
39+
*/
40+
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
41+
{
42+
if ($value instanceof DummyObjectId) {
43+
return $value->toInt();
44+
}
45+
46+
if (null === $value || '' === $value) {
47+
return null;
48+
}
49+
50+
if (!\is_string($value) && !is_int($value)) {
51+
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]);
52+
}
53+
54+
return (int) $value;
55+
}
56+
57+
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
58+
{
59+
return true;
60+
}
61+
}

tests/Entity/ObjectId/DummyObjectId.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Meilisearch\Bundle\Tests\Entity\ObjectId;
66

7-
class DummyObjectId
7+
final class DummyObjectId
88
{
99
private int $id;
1010

@@ -13,7 +13,12 @@ public function __construct(int $id)
1313
$this->id = $id;
1414
}
1515

16-
public function __toString()
16+
public function toInt(): int
17+
{
18+
return $this->id;
19+
}
20+
21+
public function __toString(): string
1722
{
1823
return (string) $this->id;
1924
}

tests/Entity/Page.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ class Page
2222
*
2323
* @ORM\GeneratedValue(strategy="NONE")
2424
*
25-
* @ORM\Column(type="object")
25+
* @ORM\Column(type="dummy_object_id")
2626
*/
2727
#[ORM\Id]
2828
#[ORM\GeneratedValue(strategy: 'NONE')]
29-
#[ORM\Column(type: Types::OBJECT)]
29+
#[ORM\Column(type: 'dummy_object_id')]
3030
private $id;
3131

3232
/**

tests/Integration/EventListener/DoctrineEventSubscriberTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Meilisearch\Bundle\Tests\Integration\EventListener;
66

77
use Meilisearch\Bundle\Tests\BaseKernelTestCase;
8+
use Meilisearch\Bundle\Tests\Entity\ObjectId\DummyObjectId;
89
use Meilisearch\Bundle\Tests\Entity\Page;
910
use Meilisearch\Bundle\Tests\Entity\Post;
1011

@@ -31,7 +32,7 @@ public function testPostPersistWithObjectId(): void
3132
$result = $this->searchService->search($this->entityManager, Page::class, $page->getTitle());
3233

3334
$this->assertCount(1, $result);
34-
$this->assertEquals($page->getId(), $result[0]->getId());
35+
$this->assertEquals(new DummyObjectId(1), $result[0]->getId());
3536
}
3637

3738
public function testPostUpdate(): void
@@ -68,7 +69,7 @@ public function testPostUpdateWithObjectId(): void
6869
$result = $this->searchService->search($this->entityManager, Page::class, 'better');
6970

7071
$this->assertCount(1, $result);
71-
$this->assertEquals($page->getId(), $result[0]->getId());
72+
$this->assertEquals(new DummyObjectId(1), $result[0]->getId());
7273
$this->assertSame('Better page', $result[0]->getTitle());
7374
}
7475

tests/config/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ doctrine:
1010
default:
1111
driver: pdo_sqlite
1212
path: '%kernel.cache_dir%/test.sqlite'
13+
types:
14+
dummy_object_id: Meilisearch\Bundle\Tests\Dbal\Type\DummyObjectIdType
1315
orm:
1416
auto_generate_proxy_classes: true
1517
validate_xml_mapping: true

tests/config/config_php7.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ doctrine:
1515
default:
1616
driver: pdo_sqlite
1717
path: '%kernel.cache_dir%/test.sqlite'
18+
types:
19+
dummy_object_id: Meilisearch\Bundle\Tests\Dbal\Type\DummyObjectIdType
1820
orm:
1921
auto_generate_proxy_classes: true
2022
validate_xml_mapping: true

0 commit comments

Comments
 (0)