improve coverage, remove infection
All checks were successful
/ ls (pull_request) Successful in 1m30s

This commit is contained in:
lubiana 2025-01-26 11:48:16 +01:00
parent eaa723a58b
commit 0aa25d107b
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
10 changed files with 160 additions and 1317 deletions

View file

@ -23,19 +23,7 @@ jobs:
- name: lint
run: composer lint
- name: test
run: composer mutation
- name: Add comment to pull request
run: |
echo '```' >> /tmp/pull-request-comment
cat var/log/infection.txt >> /tmp/pull-request-comment
cat var/log/summary.log >> /tmp/pull-request-comment
echo '```' >> /tmp/pull-request-comment
jq -n --arg msg "$(cat /tmp/pull-request-comment)" '{"body": $msg}' > /tmp/git-msg
curl -X POST \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
-d @/tmp/git-msg \
"${{ env.GITHUB_SERVER_URL }}/api/v1/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
run: composer test
- name: GIT commit and push all changed files
env:
CI_COMMIT_MESSAGE: Continuous Integration Fixes

View file

@ -27,7 +27,7 @@ jobs:
- name: lint
run: composer lint
- name: test
run: composer mutation
run: composer test
- name: GIT commit and push all changed files
env:
CI_COMMIT_MESSAGE: Continuous Integration Fixes

View file

@ -26,7 +26,6 @@
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^4.0",
"infection/infection": "^0.29.6",
"lubiana/code-quality": "^1.7.2",
"phpunit/phpunit": "^9.6.20",
"symfony/browser-kit": "7.2.*",
@ -42,8 +41,7 @@
"php-http/discovery": true,
"symfony/flex": true,
"symfony/runtime": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"infection/extension-installer": true
"dealerdirect/phpcodesniffer-composer-installer": true
},
"sort-packages": true,
"platform": {
@ -68,7 +66,8 @@
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*",
"symfony/polyfill-php81": "*",
"symfony/polyfill-php82": "*"
"symfony/polyfill-php82": "*",
"symfony/polyfill-php83": "*"
},
"scripts": {
"auto-scripts": {
@ -86,8 +85,7 @@
"rector",
"ecs --fix || ecs --fix"
],
"test": "bin/phpunit",
"mutation": "infection --threads=8 --show-mutations"
"test": "bin/phpunit"
},
"conflict": {
"symfony/symfony": "*"

1211
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,7 @@ namespace App\Form;
use App\Entity\MenuItem;
use App\Repository\MenuItemRepository;
use Doctrine\DBAL\Types\TextType;
use Doctrine\ORM\QueryBuilder;
use Override;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
@ -13,6 +14,7 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Uid\Ulid;
use Symfony\Component\Validator\Constraints\NotBlank;
use function array_map;
use function assert;
@ -22,41 +24,21 @@ final class MenuItemType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$item = $options['data'];
assert($item instanceof MenuItem); // Ensure it's of the correct type
$vendorId = $item->getFoodVendor()?->getId(); // Use safe navigation operator in case FoodVendor is null
assert($item instanceof MenuItem);
$builder->add('name'); // Basic field
$builder->add('name', \Symfony\Component\Form\Extension\Core\Type\TextType::class, [
'constraints' => [
new NotBlank(),
new \Symfony\Component\Validator\Constraints\Length(['min' => 3]),
]
]);
$builder->add('aliases', EntityType::class, [
'class' => MenuItem::class,
'choice_label' => 'name',
'multiple' => true,
'expanded' => true,
'query_builder' => static function (MenuItemRepository $repository) use ($item, $vendorId): QueryBuilder {
$ids = $repository->createQueryBuilder('m')
->select('DISTINCT IDENTITY(m.aliasOf)')
->where('m.deletedAt IS NULL')
->andWhere('m.aliasOf IS NOT NULL')
->getquery();
$ids = $ids->getScalarResult();
$ids = array_map(static fn(array $id): Ulid => Ulid::fromBinary($id[1]), $ids);
// Build the main query with a NOT EXISTS constraint
$qb = $repository->createQueryBuilder('m');
$qb
->where('m.foodVendor = :vendorId')
->andWhere('m.deletedAt IS NULL')
->andWhere('m.id != :id');
foreach ($ids as $key => $id) {
$qb->andWhere("m.id != :idBy{$key}");
$qb->setParameter("idBy{$key}", $id, UlidType::NAME);
}
$qb
->orderBy('m.name', 'ASC')
->setParameter('vendorId', $vendorId, UlidType::NAME) // ULID or appropriate type
->setParameter('id', $item->getId()); // ULID or appropriate type
return $qb;
},
'query_builder' => static fn (MenuItemRepository $repository): QueryBuilder
=> $repository->getSuitableAliasQueryBuilder($item),
]);
}

View file

@ -5,6 +5,8 @@ namespace App\Repository;
use App\Entity\MenuItem;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bridge\Doctrine\Types\UlidType;
use Symfony\Component\Uid\Ulid;
/**
* @extends ServiceEntityRepository<MenuItem>
@ -16,28 +18,29 @@ final class MenuItemRepository extends ServiceEntityRepository
parent::__construct($registry, MenuItem::class);
}
// /**
// * @return MenuItem[] Returns an array of MenuItem objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('m')
// ->andWhere('m.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('m.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
public function getSuitableAliasQueryBuilder(MenuItem $menuItem): \Doctrine\ORM\QueryBuilder
{
$ids = $this->createQueryBuilder('m')
->select('DISTINCT IDENTITY(m.aliasOf)')
->where('m.deletedAt IS NULL')
->andWhere('m.aliasOf IS NOT NULL')
->getquery();
$ids = $ids->getScalarResult();
$ids = array_map(static fn(array $id): Ulid => Ulid::fromBinary($id[1]), $ids);
// public function findOneBySomeField($value): ?MenuItem
// {
// return $this->createQueryBuilder('m')
// ->andWhere('m.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
$qb = $this->createQueryBuilder('m');
$qb
->where('m.foodVendor = :vendorId')
->andWhere('m.deletedAt IS NULL')
->andWhere('m.id != :id');
foreach ($ids as $key => $id) {
$qb->andWhere("m.id != :idBy{$key}")
->setParameter("idBy{$key}", $id, UlidType::NAME);
}
$qb
->orderBy('m.name', 'ASC')
->setParameter('vendorId', $menuItem->getFoodVendor()->getId(), UlidType::NAME)
->setParameter('id', $menuItem->getId(), UlidType::NAME);
return $qb;
}
}

View file

@ -21,6 +21,18 @@
<td>{{ menu_item.aliasOf.name }}</td>
</tr>
{% endif %}
{% if(menu_item.aliases|length > 0) %}
<tr>
<th>Aliases</th>
<td>
<ul>
{% for alias in menu_item.aliases %}
<li>{{ alias.name }}</li>
{% endfor %}
</ul>
</td>
</tr>
{% endif %}
</tbody>
</table>

View file

@ -9,6 +9,7 @@ use Override;
use function sprintf;
final class FoodVendorControllerTest extends DbWebTest
{
private string $path = '/food/vendor/';
@ -23,15 +24,20 @@ final class FoodVendorControllerTest extends DbWebTest
public function testNew(): void
{
self::assertSame(0, $this->repository->count([]));
$this->client->request('GET', sprintf('%snew', $this->path));
self::assertResponseStatusCodeSame(200);
$this->client->submitForm('Save', [
'food_vendor[name]' => 'Testing',
'food_vendor[name]' => 'TestingNew',
]);
$newVendor = $this->repository->findOneBy(['name' => 'TestingNew']);
$this->assertInstanceof(FoodVendor::class, $newVendor);
self::assertSame(1, $this->repository->count([]));
}
public function testShow(): void

View file

@ -14,6 +14,9 @@ final class MenuItemControllerTest extends DbWebTest
{
private string $path = '/menu/item/';
private FoodVendor $vendor;
private MenuItem $menuItem;
private MenuItem $aliasOne;
private MenuItem $aliasTwo;
#[Override]
public function setUp(): void
@ -23,7 +26,30 @@ final class MenuItemControllerTest extends DbWebTest
$this->vendor->setName('Food Vendor');
$this->manager->persist($this->vendor);
$this->menuItem = new MenuItem;
$this->menuItem->setName('Testing 1 2');
$this->vendor->addMenuItem($this->menuItem);
$this->manager->persist($this->vendor);
$this->manager->persist($this->menuItem);
$this->aliasOne = new MenuItem;
$this->aliasOne->setName('AliasOne');
$this->aliasOne->setFoodVendor($this->vendor);
$this->menuItem->addAlias($this->aliasOne);
$this->aliasTwo = new MenuItem;
$this->aliasTwo->setName('AliasTwo');
$this->aliasTwo->setFoodVendor($this->vendor);
$this->aliasTwo->setAliasOf($this->menuItem);
$this->menuItem->addAlias($this->aliasTwo);
$this->manager->persist($this->aliasOne);
$this->manager->persist($this->aliasTwo);
$this->manager->persist($this->menuItem);
$this->manager->flush();
}
#[Override]
@ -34,70 +60,82 @@ final class MenuItemControllerTest extends DbWebTest
public function testShow(): void
{
$menuItem = new MenuItem;
$menuItem->setName('Testing 1 2');
$this->vendor->addMenuItem($menuItem);
$this->manager->persist($this->vendor);
$this->manager->persist($menuItem);
$this->manager->flush();
$crawler = $this->client->request('GET', "{$this->path}{$menuItem->getId()}");
$crawler = $this->client->request('GET', "{$this->path}{$this->menuItem->getId()}");
$idValue = $crawler->filter(
'.table > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
)->text();
$nameValue = $crawler->filter(
'.table > tbody:nth-child(1) > tr:nth-child(2) > td:nth-child(2)'
)->text();
$aliasTwoNameValue = $crawler->filter(
'.table > tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(2) > ul:nth-child(1) > li:nth-child(1)'
)->text();
$aliasOneNameValue = $crawler->filter(
'.table > tbody:nth-child(1) > tr:nth-child(3) > td:nth-child(2) > ul:nth-child(1) > li:nth-child(2)'
)->text();
self::assertResponseStatusCodeSame(200);
$this->assertEquals($idValue, $menuItem->getId());
$this->assertEquals($nameValue, $menuItem->getName());
$this->assertEquals($idValue, $this->menuItem->getId());
$this->assertEquals($nameValue, $this->menuItem->getName());
$this->assertEquals($aliasTwoNameValue, $this->aliasOne->getName());
$this->assertEquals($aliasOneNameValue, $this->aliasTwo->getName());
}
public function testEdit(): void
{
$menuItem = new MenuItem;
$menuItem->setName('Testing 1 2');
$this->vendor->addMenuItem($menuItem);
$this->manager->persist($this->vendor);
$this->manager->persist($menuItem);
$this->manager->flush();
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $menuItem->getId()));
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId()));
$nameElem = $crawler->filter('#menu_item_name');
$this->assertEquals(
'Testing 1 2',
$nameElem->attr('value')
);
$this->client->submitForm('Update', [
'menu_item[name]' => 'Testing-1',
]);
$form = $crawler->selectButton('Update')->form();
$form['menu_item[name]'] = 'Testing-1';
$form['menu_item[aliases]'][0]->untick();
self::assertResponseRedirects(sprintf('/menu/item/%s', $menuItem->getId()));
$this->client->submit($form);
self::assertResponseRedirects(sprintf('/menu/item/%s', $this->menuItem->getId()));
$menuItem = $this->repository->find($this->menuItem->getId());
$this->assertEquals('Testing-1', $menuItem->getName());
$this->assertEquals(1, $menuItem->getAliases()->count());
$aliasOne = $this->repository->find($this->aliasOne->getId());
$this->assertNull($aliasOne->getAliasOf());
}
public function testEditInvalid(): void
{
$crawler = $this->client->request('GET', sprintf('%s%s/edit', $this->path, $this->menuItem->getId()));
$nameElem = $crawler->filter('#menu_item_name');
$this->assertEquals(
'Testing 1 2',
$nameElem->attr('value')
);
$form = $crawler->selectButton('Update')->form();
$form['menu_item[name]'] = 'a';
$this->client->submit($form);
self::assertResponseStatusCodeSame(422);
}
public function testDelete(): void
{
$menuItem = new MenuItem;
$menuItem->setName('Testing 1 2');
$this->vendor->addMenuItem($menuItem);
$this->manager->persist($this->vendor);
$this->manager->persist($menuItem);
$order = new FoodOrder;
$order->setFoodVendor($this->vendor);
$this->manager->persist($order);
$this->manager->flush();
$this->assertFalse($menuItem->isDeleted());
$this->assertFalse($this->menuItem->isDeleted());
$this->client->request('GET', "{$this->path}{$menuItem->getId()}");
$this->client->request('GET', "{$this->path}{$this->menuItem->getId()}");
$this->client->submitForm('Delete', []);
$menuItem = $this->repository->find($menuItem->getId());
$menuItem = $this->repository->find($this->menuItem->getId());
$this->assertTrue($menuItem->isDeleted());
@ -105,7 +143,7 @@ final class MenuItemControllerTest extends DbWebTest
$count = $crawler->filter('body > main:nth-child(2) > div:nth-child(5)')
->children()
->count();
$this->assertSame(0, $count);
$this->assertSame(2, $count);
$this->assertResponseIsSuccessful();

View file

@ -27,4 +27,29 @@ final class MenuItemTest extends TestCase
$this->assertTrue($item->isDeleted());
$this->assertInstanceOf(DateTimeImmutable::class, $item->getDeletedAt());
}
public function testMenuItemAlias(): void
{
$item = new MenuItem;
$item->setName('Test');
$this->assertEquals('Test', $item->getName());
$vendor = new FoodVendor;
$vendor->setName('Test');
$item->setFoodVendor($vendor);
$item2 = new MenuItem;
$item2->setName('Test2');
$item2->setFoodVendor($vendor);
$item->addAlias($item2);
$this->assertCount(1, $item->getAliases());
$this->assertSame($item, $item2->getAliasOf());
$item->removeAlias($item2);
$this->assertCount(0, $item->getAliases());
$this->assertNull($item2->getAliasOf());
}
}