-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy path12-testing.php
More file actions
121 lines (95 loc) · 4.36 KB
/
12-testing.php
File metadata and controls
121 lines (95 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Example 12: Testing with TenantTestTrait
*
* The bundle provides TenantTestTrait for PHPUnit tests.
* It simplifies switching tenants and cleaning up state between tests.
*/
namespace App\Tests\Functional;
use App\Entity\Tenant\Product;
use Hakam\MultiTenancyBundle\Doctrine\ORM\TenantEntityManager;
use Hakam\MultiTenancyBundle\Test\TenantTestTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class ProductServiceTest extends KernelTestCase
{
use TenantTestTrait;
protected function setUp(): void
{
self::bootKernel();
}
// ──────────────────────────────────────────────
// runInTenant() — execute code in a tenant context
// with automatic cleanup afterward
// ──────────────────────────────────────────────
public function testCreateProductInTenant(): void
{
$result = $this->runInTenant('42', function () {
$em = $this->getTenantEntityManager();
$product = new Product();
$product->setName('Test Product');
$product->setPrice('19.99');
$em->persist($product);
$em->flush();
return $product->getId();
});
$this->assertNotNull($result);
// After runInTenant(), tenant state is automatically reset.
// The next call can safely use a different tenant.
}
// ──────────────────────────────────────────────
// Test data isolation between tenants
// ──────────────────────────────────────────────
public function testTenantDataIsolation(): void
{
// Insert data into tenant A
$this->runInTenant('tenant_a', function () {
$em = $this->getTenantEntityManager();
$product = new Product();
$product->setName('Tenant A Product');
$product->setPrice('10.00');
$em->persist($product);
$em->flush();
});
// Verify tenant B doesn't see tenant A's data
$this->runInTenant('tenant_b', function () {
$em = $this->getTenantEntityManager();
$products = $em->getRepository(Product::class)->findAll();
$this->assertCount(0, $products, 'Tenant B should not see Tenant A data');
});
}
// ──────────────────────────────────────────────
// Manual switching (when you need more control)
// ──────────────────────────────────────────────
public function testManualSwitching(): void
{
// Switch to tenant — no automatic cleanup
$this->switchToTenant('42');
$em = $this->getTenantEntityManager();
$products = $em->getRepository(Product::class)->findAll();
$this->assertIsArray($products);
// Manually reset state when done
$this->resetTenantState();
}
}
// ──────────────────────────────────────────────
// Testing with the SwitchDbEvent directly
// (without TenantTestTrait)
// ──────────────────────────────────────────────
namespace App\Tests\Integration;
use Hakam\MultiTenancyBundle\Event\SwitchDbEvent;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class ManualTenantTest extends KernelTestCase
{
public function testDirectSwitch(): void
{
self::bootKernel();
$container = self::getContainer();
$dispatcher = $container->get('event_dispatcher');
// Dispatch the switch event directly
$dispatcher->dispatch(new SwitchDbEvent('42'));
// Now the tenant entity manager targets tenant 42's database
$tenantEm = $container->get('tenant_entity_manager');
$result = $tenantEm->getConnection()->executeQuery('SELECT 1')->fetchOne();
$this->assertEquals(1, $result);
}
}