Skip to content

Commit d65b1f5

Browse files
committed
tests: Add tests for the Doctrine integration
1 parent d0b4ee4 commit d65b1f5

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

tests/DoctrineEventsTest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Edudobay\DoctrineSerializable\Tests;
6+
7+
use Doctrine\ORM\EntityManager;
8+
use Doctrine\ORM\EntityManagerInterface;
9+
use Doctrine\ORM\ORMSetup;
10+
use Doctrine\ORM\Tools\SchemaTool;
11+
use Edudobay\DoctrineSerializable\Examples\SerializerFactory;
12+
use Edudobay\DoctrineSerializable\PersistenceEventSubscriber;
13+
use Edudobay\DoctrineSerializable\ReflectionClassMetadataFactory;
14+
use Edudobay\DoctrineSerializable\SerializationHandler;
15+
use Edudobay\DoctrineSerializable\Tests\ORMEntities\Category;
16+
use Edudobay\DoctrineSerializable\Tests\ORMEntities\Product;
17+
use PHPUnit\Framework\TestCase;
18+
19+
use function dirname;
20+
21+
class DoctrineEventsTest extends TestCase
22+
{
23+
private EntityManagerInterface $entityManager;
24+
25+
protected function setUp(): void
26+
{
27+
$config = ORMSetup::createAttributeMetadataConfiguration(
28+
paths: [dirname(__DIR__, 2) . '/tests/ORMEntities'],
29+
isDevMode: true,
30+
);
31+
32+
$connectionParams = [
33+
'driver' => 'pdo_sqlite',
34+
'path' => ":memory:",
35+
];
36+
37+
$this->entityManager = EntityManager::create($connectionParams, $config);
38+
// Create tables in the test database
39+
(new SchemaTool($this->entityManager))->createSchema([
40+
$this->entityManager->getClassMetadata(Product::class),
41+
]);
42+
43+
$serializer = SerializerFactory::serializer();
44+
$handler = new SerializationHandler($serializer, new ReflectionClassMetadataFactory());
45+
46+
$this->entityManager->getEventManager()->addEventSubscriber(
47+
new PersistenceEventSubscriber($handler)
48+
);
49+
}
50+
51+
public function test_persist_entity()
52+
{
53+
$this->entityManager->wrapInTransaction(function () {
54+
$this->entityManager->persist(new Product(
55+
id: 'pr-1479',
56+
name: 'Water Bottle 500 ml',
57+
categories: [new Category('cat-012', 'Drinks')],
58+
));
59+
});
60+
61+
$db = $this->entityManager->getConnection();
62+
$data = $db->fetchAllAssociative('SELECT * FROM Product');
63+
64+
self::assertEquals(
65+
[
66+
[
67+
'id' => 'pr-1479',
68+
'name' => 'Water Bottle 500 ml',
69+
'categories' => '[{"id":"cat-012","name":"Drinks"}]',
70+
],
71+
],
72+
$data
73+
);
74+
}
75+
76+
public function test_retrieve_entity()
77+
{
78+
$db = $this->entityManager->getConnection();
79+
$qb = $db->createQueryBuilder();
80+
$qb
81+
->insert('Product')
82+
->values([
83+
'id' => $qb->createPositionalParameter('pr-19945'),
84+
'name' => $qb->createPositionalParameter('Watermelon'),
85+
'categories' => $qb->createPositionalParameter('[{"id":"cat-008","name":"Fruits & Vegetables"}]'),
86+
])
87+
->executeStatement();
88+
89+
$expectedProduct = new Product(
90+
id: 'pr-19945',
91+
name: 'Watermelon',
92+
categories: [new Category('cat-008', 'Fruits & Vegetables')],
93+
);
94+
95+
/** @var Product $product */
96+
$product = $this->entityManager->find(Product::class, 'pr-19945');
97+
unset($product->_categories); // Attribute used only for backing storage
98+
99+
self::assertEquals($expectedProduct, $product);
100+
101+
// Now modify the Product and verify that it was correctly updated in the database.
102+
103+
$this->entityManager->wrapInTransaction(function () use ($product) {
104+
$product->name = 'Melon';
105+
106+
$this->entityManager->flush();
107+
});
108+
109+
$result = $db->fetchAssociative('SELECT name FROM Product WHERE id = ?', ['pr-19945']);
110+
self::assertEquals(['name' => 'Melon'], $result);
111+
}
112+
}

tests/ORMEntities/Category.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Edudobay\DoctrineSerializable\Tests\ORMEntities;
6+
7+
class Category
8+
{
9+
public function __construct(
10+
public string $id,
11+
public string $name,
12+
) {
13+
}
14+
}

tests/ORMEntities/Product.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Edudobay\DoctrineSerializable\Tests\ORMEntities;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use Edudobay\DoctrineSerializable\Attributes\Serializable;
9+
10+
#[ORM\Entity, ORM\Table]
11+
class Product
12+
{
13+
#[ORM\Column('categories', type: 'json')]
14+
public array $_categories;
15+
16+
public function __construct(
17+
#[ORM\Id, ORM\Column]
18+
public string $id,
19+
20+
#[ORM\Column]
21+
public string $name,
22+
23+
#[Serializable]
24+
/** @var Category[] */
25+
public array $categories,
26+
) {
27+
}
28+
}

0 commit comments

Comments
 (0)