saufen/tests/Feature/Web/BulkEditWantedStockFormTest.php
lubiana bab4d3d208
All checks were successful
/ ls (pull_request) Successful in 1m25s
emojis
2025-06-16 20:14:03 +02:00

204 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
use App\Entity\DrinkType;
use Doctrine\ORM\EntityManagerInterface;
test('Bulk Edit Wanted Stock Form displays correctly with drink types', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create test drink types
$em = $this->getContainer()->get(EntityManagerInterface::class);
$drinkType1 = new DrinkType();
$drinkType1->setName('Cola');
$drinkType1->setWantedStock(10);
$drinkType1->setCurrentStock(5);
$drinkType2 = new DrinkType();
$drinkType2->setName('Beer');
$drinkType2->setWantedStock(20);
$drinkType2->setCurrentStock(15);
$em->persist($drinkType1);
$em->persist($drinkType2);
$em->flush();
// Request the bulk edit page
$crawler = $client->request('GET', '/drink-types/bulk-edit-wanted-stock');
// Validate successful response
$this->assertResponseIsSuccessful();
// Check page title
$this->assertSelectorTextContains('h1', 'BULK EDIT DRINK TYPE WANTED STOCK');
// Check that drink types are displayed in the table (they're in the value attribute of disabled inputs)
$this->assertSelectorExists('input[value="Cola"]');
$this->assertSelectorExists('input[value="Beer"]');
// Check that the form has the correct submit button
$this->assertSelectorTextContains('button[type="submit"]', 'Update Wanted Stock Levels');
// Check that input fields exist for each drink type
$this->assertCount(2, $crawler->filter('input[name*="[wantedStock]"]'));
});
test('Bulk Edit Wanted Stock Form submission updates drink type wanted stock levels', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create test drink types
$em = $this->getContainer()->get(EntityManagerInterface::class);
$drinkType1 = new DrinkType();
$drinkType1->setName('Cola');
$drinkType1->setWantedStock(10);
$drinkType1->setCurrentStock(5);
$drinkType2 = new DrinkType();
$drinkType2->setName('Beer');
$drinkType2->setWantedStock(20);
$drinkType2->setCurrentStock(15);
$em->persist($drinkType1);
$em->persist($drinkType2);
$em->flush();
// Get the bulk edit page to get the form
$crawler = $client->request('GET', '/drink-types/bulk-edit-wanted-stock');
// Submit the form with updated values
$form = $crawler->selectButton('Update Wanted Stock Levels')->form();
// Update the wanted stock values - note: drink types are ordered by wantedStock DESC
// So Beer (wantedStock 20) comes first [0], Cola (wantedStock 10) comes second [1]
$form['bulk_edit_drink_type_wanted_stock_form[drinkTypes][0][wantedStock]'] = 25; // Beer
$form['bulk_edit_drink_type_wanted_stock_form[drinkTypes][1][wantedStock]'] = 15; // Cola
$client->submit($form);
// Check that we're redirected back to the same page
$this->assertResponseRedirects('/drink-types/bulk-edit-wanted-stock');
// Follow the redirect
$client->followRedirect();
// Check for success message
$this->assertSelectorTextContains('.alert-success', 'Wanted stock levels updated successfully!');
// Verify the database was updated
$em->clear(); // Clear entity manager to reload from database
$updatedDrinkType1 = $em->find(DrinkType::class, $drinkType1->getId());
$updatedDrinkType2 = $em->find(DrinkType::class, $drinkType2->getId());
expect($updatedDrinkType1->getWantedStock())->toBe(15); // Cola
expect($updatedDrinkType2->getWantedStock())->toBe(25); // Beer
});
test('Bulk Edit Wanted Stock Form handles empty drink types list', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Clear existing drink types
$em = $this->getContainer()->get(EntityManagerInterface::class);
$drinkTypeRepository = $em->getRepository(DrinkType::class);
$existingDrinkTypes = $drinkTypeRepository->findAll();
foreach ($existingDrinkTypes as $drinkType) {
$em->remove($drinkType);
}
$em->flush();
// Request the bulk edit page
$crawler = $client->request('GET', '/drink-types/bulk-edit-wanted-stock');
// Validate successful response
$this->assertResponseIsSuccessful();
// Check page title
$this->assertSelectorTextContains('h1', 'BULK EDIT DRINK TYPE WANTED STOCK');
// Check that the table exists but has no rows (no tbody content)
$this->assertSelectorExists('table');
$this->assertCount(0, $crawler->filter('table tbody tr'));
// Check that the form still has the submit button
$this->assertSelectorTextContains('button[type="submit"]', 'Update Wanted Stock Levels');
});
test('Bulk Edit Wanted Stock Form rejects negative values (validation)', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create test drink type
$em = $this->getContainer()->get(EntityManagerInterface::class);
$drinkType = new DrinkType();
$drinkType->setName('Test Drink');
$drinkType->setWantedStock(10);
$drinkType->setCurrentStock(5);
$em->persist($drinkType);
$em->flush();
// Get the bulk edit page
$crawler = $client->request('GET', '/drink-types/bulk-edit-wanted-stock');
// Submit the form with negative value
$form = $crawler->selectButton('Update Wanted Stock Levels')->form();
$form['bulk_edit_drink_type_wanted_stock_form[drinkTypes][0][wantedStock]'] = -5;
$client->submit($form);
// The form should NOT redirect, but show the validation error
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'BULK EDIT DRINK TYPE WANTED STOCK');
$this->assertSelectorTextContains('.form-error-message, .invalid-feedback', 'Wanted stock must not be negative');
// Verify the database was not updated
$em->clear();
$updatedDrinkType = $em->find(DrinkType::class, $drinkType->getId());
expect($updatedDrinkType->getWantedStock())->toBe(10); // Should remain unchanged
});
test('Bulk Edit Wanted Stock Form preserves drink type names as read-only', function (): void {
$this->ensureKernelShutdown();
$client = static::createClient();
// Create test drink type
$em = $this->getContainer()->get(EntityManagerInterface::class);
$drinkType = new DrinkType();
$drinkType->setName('Original Name');
$drinkType->setWantedStock(10);
$drinkType->setCurrentStock(5);
$em->persist($drinkType);
$em->flush();
// Get the bulk edit page
$crawler = $client->request('GET', '/drink-types/bulk-edit-wanted-stock');
// Check that the name field is read-only
$nameInput = $crawler->filter('input[name*="[name]"]')->first();
expect($nameInput->attr('readonly'))->toBe('readonly');
expect($nameInput->attr('disabled'))->toBe('disabled');
// Submit the form
$form = $crawler->selectButton('Update Wanted Stock Levels')->form();
$form['bulk_edit_drink_type_wanted_stock_form[drinkTypes][0][wantedStock]'] = 15;
$client->submit($form);
// Follow the redirect
$client->followRedirect();
// Verify the name was not changed
$em->clear();
$updatedDrinkType = $em->find(DrinkType::class, $drinkType->getId());
expect($updatedDrinkType->getName())->toBe('Original Name');
expect($updatedDrinkType->getWantedStock())->toBe(15);
});