tetssssss

This commit is contained in:
lubiana 2025-06-10 19:05:55 +02:00
parent 6f07d70436
commit 8063e7bec9
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
28 changed files with 771 additions and 273 deletions

View file

@ -1,10 +1,12 @@
<?php
declare(strict_types=1);
use App\Entity\DrinkType;
use App\Entity\PropertyChangeLog;
use Doctrine\ORM\EntityManagerInterface;
test('Update Listener', function () {
test('Update Listener', function (): void {
$drinkType = new DrinkType();
$drinkType->setName('test');
$drinkType->setWantedStock(10);
@ -16,26 +18,34 @@ test('Update Listener', function () {
$em->flush();
$propertyLogRepository = $em->getRepository(PropertyChangeLog::class);
$logs = $propertyLogRepository->findBy(['entityClass' => DrinkType::class]);
$logs = $propertyLogRepository->findBy([
'entityClass' => DrinkType::class,
]);
expect($logs)->toHaveCount(2);
$drinkType->setWantedStock(15);
$em->persist($drinkType);
$em->flush();
$logs = $propertyLogRepository->findBy(['entityClass' => DrinkType::class]);
$logs = $propertyLogRepository->findBy([
'entityClass' => DrinkType::class,
]);
expect($logs)->toHaveCount(3);
$drinkType->setCurrentStock(15);
$em->persist($drinkType);
$em->flush();
$logs = $propertyLogRepository->findBy(['entityClass' => DrinkType::class]);
$logs = $propertyLogRepository->findBy([
'entityClass' => DrinkType::class,
]);
expect($logs)->toHaveCount(4);
$drinkType->setDescription('test');
$em->persist($drinkType);
$em->flush();
$logs = $propertyLogRepository->findBy(['entityClass' => DrinkType::class]);
$logs = $propertyLogRepository->findBy([
'entityClass' => DrinkType::class,
]);
expect($logs)->toHaveCount(4);
});

View file

@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
use App\Entity\DrinkType;
use App\Repository\DrinkTypeRepository;
use Doctrine\ORM\EntityManagerInterface;
test('findAll returns all drink types ordered by wantedStock DESC', function (): void {
$em = $this->getContainer()->get(EntityManagerInterface::class);
$repository = $this->getContainer()->get(DrinkTypeRepository::class);
// Clear existing drink types
$existingDrinkTypes = $repository->findAll();
foreach ($existingDrinkTypes as $drinkType) {
$em->remove($drinkType);
}
$em->flush();
// Create test drink types with different wantedStock values
$drinkType1 = new DrinkType();
$drinkType1->setName('Drink Type 1');
$drinkType1->setWantedStock(5);
$drinkType2 = new DrinkType();
$drinkType2->setName('Drink Type 2');
$drinkType2->setWantedStock(10);
$drinkType3 = new DrinkType();
$drinkType3->setName('Drink Type 3');
$drinkType3->setWantedStock(0);
// Persist the drink types
$em->persist($drinkType1);
$em->persist($drinkType2);
$em->persist($drinkType3);
$em->flush();
// Test findAll method
$result = $repository->findAll();
// Verify the result
expect($result)->toHaveCount(3);
expect($result[0]->getName())->toBe('Drink Type 2');
expect($result[1]->getName())->toBe('Drink Type 1');
expect($result[2]->getName())->toBe('Drink Type 3');
});
test('findWanted returns only drink types with wantedStock > 0 ordered by wantedStock DESC and name ASC', function (): void {
$em = $this->getContainer()->get(EntityManagerInterface::class);
$repository = $this->getContainer()->get(DrinkTypeRepository::class);
// Clear existing drink types
$existingDrinkTypes = $repository->findAll();
foreach ($existingDrinkTypes as $drinkType) {
$em->remove($drinkType);
}
$em->flush();
// Create test drink types with different wantedStock values
$drinkType1 = new DrinkType();
$drinkType1->setName('Cola');
$drinkType1->setWantedStock(10);
$drinkType2 = new DrinkType();
$drinkType2->setName('Beer');
$drinkType2->setWantedStock(10);
$drinkType3 = new DrinkType();
$drinkType3->setName('Water');
$drinkType3->setWantedStock(5);
$drinkType4 = new DrinkType();
$drinkType4->setName('Juice');
$drinkType4->setWantedStock(0);
// Persist the drink types
$em->persist($drinkType1);
$em->persist($drinkType2);
$em->persist($drinkType3);
$em->persist($drinkType4);
$em->flush();
// Test findWanted method
$result = $repository->findWanted();
// Verify the result
expect($result)->toHaveCount(3);
// First should be Beer (wantedStock 10, name starts with B)
expect($result[0]->getName())->toBe('Beer');
expect($result[0]->getWantedStock())->toBe(10);
// Second should be Cola (wantedStock 10, name starts with C)
expect($result[1]->getName())->toBe('Cola');
expect($result[1]->getWantedStock())->toBe(10);
// Third should be Water (wantedStock 5)
expect($result[2]->getName())->toBe('Water');
expect($result[2]->getWantedStock())->toBe(5);
// Juice should not be in the result (wantedStock 0)
$names = array_map(fn($dt) => $dt->getName(), $result);
expect($names)->not->toContain('Juice');
});

View file

@ -1,18 +1,20 @@
<?php
declare(strict_types=1);
use App\Enum\SystemSettingKey;
use App\Service\Config\AppName;
use App\Service\ConfigurationService;
test('it returns the system name from configuration service', function () {
test('it returns the system name from configuration service', function (): void {
/** @var ConfigurationService $configService */
$configService = $this->getContainer()->get(ConfigurationService::class);
$appName = new AppName($configService);
expect((string)$appName)->toBe(SystemSettingKey::SYSTEM_NAME->defaultValue());
expect((string) $appName)->toBe(SystemSettingKey::SYSTEM_NAME->defaultValue());
$expected = 'Test System Name';
$configService->set(SystemSettingKey::SYSTEM_NAME, $expected);
expect((string)$appName)->toBe($expected);
expect((string) $appName)->toBe($expected);
});

View file

@ -1,12 +1,9 @@
<?php
declare(strict_types=1);
use App\Entity\SystemConfig;
use App\Enum\SystemSettingKey;
use App\Service\ConfigurationService;
test('get returns correct value', function (): void {
// Arrange
/** @var ConfigurationService $configService */

View file

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
use App\Entity\DrinkType;
use App\Entity\PropertyChangeLog;
use App\Service\DrinkType\GetStockHistory;
use Doctrine\ORM\EntityManagerInterface;
test('it returns empty array for unsaved drink type', function (): void {
$drinkType = new DrinkType();
$drinkType->setName('Test Drink');
$getStockHistory = $this->getContainer()->get(GetStockHistory::class);
$result = $getStockHistory($drinkType);
expect($result)->toBeArray();
expect($result)->toBeEmpty();
});
test('it returns stock history for a drink type', function (): void {
// Create a drink type
$drinkType = new DrinkType();
$drinkType->setName('Test Drink');
$drinkType->setCurrentStock(10);
$em = $this->getContainer()->get(EntityManagerInterface::class);
$em->persist($drinkType);
$em->flush();
// Change the current stock to create logs
$drinkType->setCurrentStock(15);
$em->persist($drinkType);
$em->flush();
$drinkType->setCurrentStock(5);
$em->persist($drinkType);
$em->flush();
// Get the stock history
$getStockHistory = $this->getContainer()->get(GetStockHistory::class);
$result = $getStockHistory($drinkType);
// Verify the result
expect($result)->toBeArray();
expect($result)->toHaveCount(3);
// Verify that all logs are for currentStock
foreach ($result as $log) {
expect($log)->toBeInstanceOf(PropertyChangeLog::class);
expect($log->getPropertyName())->toBe('currentStock');
expect($log->getEntityClass())->toBe(DrinkType::class);
expect($log->getEntityId())->toBe($drinkType->getId());
}
// Verify the values in order (oldest first)
expect($result[0]->getNewValue())->toBe('10');
expect($result[1]->getNewValue())->toBe('15');
expect($result[2]->getNewValue())->toBe('5');
});
test('it only returns logs for the specified drink type', function (): void {
$em = $this->getContainer()->get(EntityManagerInterface::class);
// Create first drink type
$drinkType1 = new DrinkType();
$drinkType1->setName('Drink 1');
$drinkType1->setCurrentStock(10);
$em->persist($drinkType1);
$em->flush();
// Create second drink type
$drinkType2 = new DrinkType();
$drinkType2->setName('Drink 2');
$drinkType2->setCurrentStock(20);
$em->persist($drinkType2);
$em->flush();
// Get history for first drink type
$getStockHistory = $this->getContainer()->get(GetStockHistory::class);
$result = $getStockHistory($drinkType1);
// Verify we only get logs for the first drink type
expect($result)->toHaveCount(1);
expect($result[0]->getEntityId())->toBe($drinkType1->getId());
expect($result[0]->getNewValue())->toBe('10');
});

View file

@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
use App\Entity\DrinkType;
use App\Entity\PropertyChangeLog;
use App\Service\DrinkType\GetWantedHistory;
use Doctrine\ORM\EntityManagerInterface;
test('it returns empty array for unsaved drink type', function (): void {
$drinkType = new DrinkType();
$drinkType->setName('Test Drink');
$getWantedHistory = $this->getContainer()->get(GetWantedHistory::class);
$result = $getWantedHistory($drinkType);
expect($result)->toBeArray();
expect($result)->toBeEmpty();
});
test('it returns wanted stock history for a drink type', function (): void {
// Create a drink type
$drinkType = new DrinkType();
$drinkType->setName('Test Drink');
$drinkType->setWantedStock(10);
$em = $this->getContainer()->get(EntityManagerInterface::class);
$em->persist($drinkType);
$em->flush();
// Change the wanted stock to create logs
$drinkType->setWantedStock(15);
$em->persist($drinkType);
$em->flush();
$drinkType->setWantedStock(5);
$em->persist($drinkType);
$em->flush();
// Get the wanted stock history
$getWantedHistory = $this->getContainer()->get(GetWantedHistory::class);
$result = $getWantedHistory($drinkType);
// Verify the result
expect($result)->toBeArray();
expect($result)->toHaveCount(3);
// Verify that all logs are for wantedStock
foreach ($result as $log) {
expect($log)->toBeInstanceOf(PropertyChangeLog::class);
expect($log->getPropertyName())->toBe('wantedStock');
expect($log->getEntityClass())->toBe(DrinkType::class);
expect($log->getEntityId())->toBe($drinkType->getId());
}
// Verify the values in order (oldest first)
expect($result[0]->getNewValue())->toBe('10');
expect($result[1]->getNewValue())->toBe('15');
expect($result[2]->getNewValue())->toBe('5');
});
test('it only returns logs for the specified drink type', function (): void {
$em = $this->getContainer()->get(EntityManagerInterface::class);
// Create first drink type
$drinkType1 = new DrinkType();
$drinkType1->setName('Drink 1');
$drinkType1->setWantedStock(10);
$em->persist($drinkType1);
$em->flush();
// Create second drink type
$drinkType2 = new DrinkType();
$drinkType2->setName('Drink 2');
$drinkType2->setWantedStock(20);
$em->persist($drinkType2);
$em->flush();
// Get history for first drink type
$getWantedHistory = $this->getContainer()->get(GetWantedHistory::class);
$result = $getWantedHistory($drinkType1);
// Verify we only get logs for the first drink type
expect($result)->toHaveCount(1);
expect($result[0]->getEntityId())->toBe($drinkType1->getId());
expect($result[0]->getNewValue())->toBe('10');
});

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
test('Hello World', function (): void {
// This calls KernelTestCase::bootKernel(), and creates a
// "client" that is acting as the browser
$this->ensureKernelShutdown();
$client = static::createClient();
// Request a specific page
$crawler = $client->request('GET', '/');
// Validate a successful response and some content
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Hello World');
});

View file

@ -4,6 +4,6 @@ declare(strict_types=1);
namespace Tests;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
abstract class TestCase extends KernelTestCase {}
abstract class TestCase extends WebTestCase {}

View file

@ -1,7 +0,0 @@
<?php
declare(strict_types=1);
test('example', function (): void {
expect(true)->toBeTrue();
});