|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Tests\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use App\Repository\ProductRepository; |
|
8
|
|
|
use App\Entity\Product; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Test cases for class ProductRepository. |
|
12
|
|
|
*/ |
|
13
|
|
|
class ProductRepositoryTest extends KernelTestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private EntityManagerInterface $entityManager; |
|
16
|
|
|
private ProductRepository $repository; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* setUp |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function setUp(): void |
|
24
|
|
|
{ |
|
25
|
|
|
self::bootKernel(); |
|
26
|
|
|
|
|
27
|
|
|
$kernel = self::$kernel; |
|
28
|
|
|
|
|
29
|
|
|
// Check if kernel is not null |
|
30
|
|
|
if ($kernel === null) { |
|
31
|
|
|
throw new \RuntimeException('Kernel is not initialized.'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// Access the container |
|
35
|
|
|
$container = $kernel->getContainer(); |
|
36
|
|
|
|
|
37
|
|
|
// Get the Doctrine registry service |
|
38
|
|
|
$doctrine = $container->get('doctrine'); |
|
39
|
|
|
|
|
40
|
|
|
// Get the EntityManager from the container |
|
41
|
|
|
// @phpstan-ignore-next-line |
|
42
|
|
|
$this->entityManager = $doctrine->getManager(); |
|
43
|
|
|
|
|
44
|
|
|
// Clear existing data |
|
45
|
|
|
$this->entityManager->getConnection()->executeStatement('DELETE FROM product'); |
|
46
|
|
|
|
|
47
|
|
|
// Initialize the repository |
|
48
|
|
|
// @phpstan-ignore-next-line |
|
49
|
|
|
$this->repository = $this->entityManager->getRepository(Product::class); |
|
50
|
|
|
|
|
51
|
|
|
// Insert test data |
|
52
|
|
|
$products = [ |
|
53
|
|
|
new Product(), |
|
54
|
|
|
new Product(), |
|
55
|
|
|
new Product(), |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
$products[0]->setName('Product 1'); |
|
59
|
|
|
$products[0]->setValue(10); |
|
60
|
|
|
$products[1]->setName('Product 2'); |
|
61
|
|
|
$products[1]->setValue(20); |
|
62
|
|
|
$products[2]->setName('Product 3'); |
|
63
|
|
|
$products[2]->setValue(30); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($products as $product) { |
|
66
|
|
|
$this->entityManager->persist($product); |
|
67
|
|
|
} |
|
68
|
|
|
$this->entityManager->flush(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* testFindByMinimumValue |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testFindByMinimumValue(): void |
|
77
|
|
|
{ |
|
78
|
|
|
// Find products with value >= 15 |
|
79
|
|
|
$results = $this->repository->findByMinimumValue(15); |
|
80
|
|
|
|
|
81
|
|
|
// Assert that only products with value >= 15 are returned |
|
82
|
|
|
$this->assertCount(2, $results); |
|
83
|
|
|
$this->assertEquals(20, $results[0]->getValue()); |
|
84
|
|
|
$this->assertEquals(30, $results[1]->getValue()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* testFindByMinimumValue2 |
|
89
|
|
|
* |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testFindByMinimumValue2(): void |
|
93
|
|
|
{ |
|
94
|
|
|
// Find products with value >= 15 using raw SQL |
|
95
|
|
|
$results = $this->repository->findByMinimumValue2(15); |
|
96
|
|
|
|
|
97
|
|
|
// Assert array results |
|
98
|
|
|
//$this->assertIsArray($results); |
|
99
|
|
|
$this->assertCount(2, $results); |
|
100
|
|
|
$this->assertEquals(20, $results[0]['value']); |
|
101
|
|
|
$this->assertEquals(30, $results[1]['value']); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|