Merge remote-tracking branch 'origin/master' into refactor-using-rector-202303

This commit is contained in:
2023-04-15 00:07:09 +02:00
143 changed files with 3395 additions and 1300 deletions

View File

@@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Controller;
use Chill\MainBundle\Doctrine\Model\Point;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\AddressReference;
use Chill\MainBundle\Repository\AddressRepository;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
class AddressToReferenceMatcherControllerTest extends WebTestCase
{
use PrepareClientTrait;
private AddressRepository $addressRepository;
protected function setUp(): void
{
self::bootKernel();
$this->addressRepository = self::$container->get(AddressRepository::class);
}
/**
* @dataProvider addressToReviewProvider
*/
public function testMarkAddressAsReviewed(int $addressId): void
{
$client = $this->getClientAuthenticated();
$client->request('POST', "/api/1.0/main/address/reference-match/${addressId}/set/reviewed");
$this->assertResponseIsSuccessful();
$address = $this->addressRepository->find($addressId);
$this->assertEquals(Address::ADDR_REFERENCE_STATUS_REVIEWED, $address->getRefStatus());
}
/**
* @dataProvider addressUnsyncedProvider
*/
public function testSyncAddressWithReference(int $addressId): void
{
$client = $this->getClientAuthenticated();
$client->request('POST', "/api/1.0/main/address/reference-match/${addressId}/sync-with-reference");
$this->assertResponseIsSuccessful();
$address = $this->addressRepository->find($addressId);
$this->assertEquals(Address::ADDR_REFERENCE_STATUS_MATCH, $address->getRefStatus());
$this->assertEquals($address->getAddressReference()->getStreet(), $address->getStreet());
$this->assertEquals($address->getAddressReference()->getStreetNumber(), $address->getStreetNumber());
$this->assertEquals($address->getAddressReference()->getPoint()->toWKT(), $address->getPoint()->toWKT());
}
public static function addressToReviewProvider(): iterable
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$nb = $em->createQuery('SELECT count(a) FROM '.Address::class.' a')
->getSingleScalarResult();
if (0 === $nb) {
throw new \RuntimeException("There aren't any address with a ref status 'matched'");
}
/** @var Address $address */
$address = $em->createQuery('SELECT a FROM '.Address::class.' a')
->setFirstResult(rand(0, $nb))
->setMaxResults(1)
->getSingleResult();
$address->setRefStatus(Address::ADDR_REFERENCE_STATUS_TO_REVIEW);
$em->flush();
yield [$address->getId()];
}
public static function addressUnsyncedProvider(): iterable
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$nb = $em->createQuery('SELECT count(a) FROM '.AddressReference::class.' a')
->getSingleScalarResult();
if (0 === $nb) {
throw new \RuntimeException("There isn't any address reference");
}
$ref = $em->createQuery('SELECT a FROM '.AddressReference::class.' a')
->setMaxResults(1)
->setFirstResult(rand(0, $nb))
->getSingleResult();
$address = Address::createFromAddressReference($ref);
// make the address dirty
$address->setStreet('tagada')
->setStreetNumber('-250')
->setPoint(Point::fromLonLat(0, 0))
->setRefStatus(Address::ADDR_REFERENCE_STATUS_TO_REVIEW);
$em->persist($address);
$em->flush();
yield [$address->getId()];
}
}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Controller;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Test\PrepareClientTrait;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
class GeographicalUnitByAddressApiControllerTest extends WebTestCase
{
use PrepareClientTrait;
/**
* @dataProvider generateRandomAddress
*/
public function testGetGeographicalUnitCoveringAddress(int $addressId): void
{
$client = $this->getClientAuthenticated();
$client->request('GET', '/api/1.0/main/geographical-unit/by-address/'.$addressId.'.json');
$this->assertResponseIsSuccessful();
}
public static function generateRandomAddress(): iterable
{
self::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$nb = $em->createQuery('SELECT COUNT(a) FROM '.Address::class.' a')->getSingleScalarResult();
/** @var \Chill\MainBundle\Entity\Address $random */
$random = $em->createQuery('SELECT a FROM '.Address::class.' a')
->setFirstResult(rand(0, $nb))
->setMaxResults(1)
->getSingleResult();
yield [$random->getId()];
}
}

View File

@@ -99,7 +99,7 @@ final class AddressReferenceBaseImporterTest extends KernelTestCase
'abcc guessed fixed'
);
$this->assertCount('1', $addresses);
$this->assertCount(1, $addresses);
$this->assertEquals('Rue test abccc guessed fixed', $addresses[0]->getStreet());
$this->assertEquals($previousAddressId, $addresses[0]->getId());
}

View File

@@ -0,0 +1,136 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Services\Import;
use Chill\MainBundle\Doctrine\Model\Point;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Entity\AddressReference;
use Chill\MainBundle\Entity\PostalCode;
use Chill\MainBundle\Repository\AddressReferenceRepository;
use Chill\MainBundle\Repository\CountryRepository;
use Chill\MainBundle\Service\Import\AddressReferenceBaseImporter;
use Chill\MainBundle\Service\Import\AddressToReferenceMatcher;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
/**
* @internal
* @coversNothing
*/
class AddressToReferenceMatcherTest extends KernelTestCase
{
private AddressToReferenceMatcher $addressToReferenceMatcher;
private AddressReferenceRepository $addressReferenceRepository;
private CountryRepository $countryRepository;
private EntityManagerInterface $entityManager;
private AddressReferenceBaseImporter $addressReferenceBaseImporter;
protected function setUp(): void
{
parent::setUp();
self::bootKernel();
$this->addressToReferenceMatcher = self::$container->get(AddressToReferenceMatcher::class);
$this->addressReferenceRepository = self::$container->get(AddressReferenceRepository::class);
$this->countryRepository = self::$container->get(CountryRepository::class);
$this->addressReferenceBaseImporter = self::$container->get(AddressReferenceBaseImporter::class);
$this->entityManager = self::$container->get(EntityManagerInterface::class);
}
public function testCheckAddressesMatchingReferences(): void
{
if (null === $belgium = $this->countryRepository->findOneBy(['countryCode' => 'BE'])) {
throw new \RuntimeException('Belgium not found');
}
$postalCode = (new PostalCode())
->setName('test for matcher')
->setPostalCodeSource('test for matcher')
->setCode('78910')
->setRefPostalCodeId($refPostalCodeId = '78910'.uniqid())
->setCountry($belgium)
;
$this->entityManager->persist($postalCode);
$this->entityManager->flush();
$this->addressReferenceBaseImporter->importAddress(
$refAddress = '010203_test'.uniqid(),
$refPostalCodeId,
'78910',
$street = 'Rue Poulet',
$streetNumber = '14',
'test_matcher',
$lat = 50.0123456789,
$lon = 5.0123456789,
4326
);
$this->addressReferenceBaseImporter->finalize();
if (null === $addressReference = $this->addressReferenceRepository->findOneBy(['refId' => $refAddress])) {
throw new \RuntimeException('address reference not created');
}
$address = Address::createFromAddressReference($addressReference);
$this->assertEquals('Rue Poulet', $address->getStreet());
$this->entityManager->persist($address);
$this->entityManager->flush();
// we update the address
$this->addressReferenceBaseImporter->importAddress(
$refAddress,
$refPostalCodeId,
'78910',
'Rue Poulet aux amandes',
'14',
'test_matcher',
50.01234456789,
5.0123456789,
4326
);
$this->addressReferenceBaseImporter->finalize();
$this->entityManager->flush();
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
$this->entityManager->refresh($address);
$this->assertEquals('to_review', $address->getRefStatus());
// we update the address
$this->addressReferenceBaseImporter->importAddress(
$refAddress,
$refPostalCodeId,
'78910',
$street,
$streetNumber,
'test_matcher',
$lat,
$lon,
4326
);
$this->addressReferenceBaseImporter->finalize();
$this->addressToReferenceMatcher->checkAddressesMatchingReferences();
$this->entityManager->refresh($address);
$this->assertEquals('Rue Poulet', $address->getStreet());
$this->assertEquals('match', $address->getRefStatus());
}
}

View File

@@ -18,6 +18,7 @@ use Chill\MainBundle\Templating\Entity\AddressRender;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Iterator;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Templating\EngineInterface;
/**