| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- use App\ApiResources\Export\ExportRequest;
- use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
- use App\Entity\Access\Access;
- use App\Entity\Core\File;
- use App\Entity\Network\NetworkOrganization;
- use App\Entity\Organization\Organization;
- use App\Entity\Organization\Parameters;
- use App\Entity\Person\Person;
- use App\Repository\Access\AccessRepository;
- use App\Repository\Organization\OrganizationRepository;
- use App\Service\Export\Encoder\PdfEncoder;
- use App\Service\Export\LicenceCmfExporter;
- use App\Service\Export\Model\LicenceCmf;
- use App\Service\Export\Model\LicenceCmfCollection;
- use App\Service\ServiceIterator\EncoderIterator;
- use App\Service\Storage\LocalStorage;
- use App\Tests\TestToolsTrait;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\ORM\EntityManagerInterface;
- use PHPUnit\Framework\MockObject\MockObject;
- use PHPUnit\Framework\TestCase;
- use Twig\Environment;
- class LicenceCmfExporterTest extends TestCase
- {
- use TestToolsTrait;
- private MockObject | LicenceCmfOrganizationER $exportRequest;
- private MockObject | AccessRepository $accessRepo;
- private MockObject | Environment $twig;
- private MockObject | EncoderIterator $encoderIterator;
- private MockObject | EntityManagerInterface $em;
- private MockObject | LocalStorage $storage;
- private MockObject | OrganizationRepository $organizationRepo;
- private MockObject | Access $access;
- private MockObject | Organization $organization;
- private MockObject | Organization $cmf;
- private MockObject | NetworkOrganization $networkOrgs;
- private MockObject | Collection $collection;
- private MockObject | Organization $parent;
- private MockObject | File $logo;
- private MockObject | Access $presidentAccess;
- private MockObject | Person $president;
- private MockObject | Parameters $cmfParameters;
- private MockObject | File $qrCode;
- private MockObject | PdfEncoder $encoder;
- public function setUp(): void {
- $this->exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
- $this->accessRepo = $this->getMockBuilder(AccessRepository::class)->disableOriginalConstructor()->getMock();
- $this->twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock();
- $this->encoderIterator = $this->getMockBuilder(EncoderIterator::class)->disableOriginalConstructor()->getMock();
- $this->encoder = $this->getMockBuilder(PdfEncoder::class)->disableOriginalConstructor()->getMock();
- $this->em = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
- $this->storage = $this->getMockBuilder(LocalStorage::class)->disableOriginalConstructor()->getMock();
- $this->organizationRepo = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
- $this->access = $this->getMockBuilder(Access::class)->getMock();
- $this->organization = $this->getMockBuilder(Organization::class)->getMock();
- $this->cmf = $this->getMockBuilder(Organization::class)->getMock();
- $this->networkOrgs = $this->getMockBuilder(NetworkOrganization::class)->getMock();
- $this->collection = $this->getMockBuilder(Collection::class)->getMock();
- $this->parent = $this->getMockBuilder(Organization::class)->getMock();
- $this->logo = $this->getMockBuilder(File::class)->getMock();
- $this->presidentAccess = $this->getMockBuilder(Access::class)->getMock();
- $this->president = $this->getMockBuilder(Person::class)->getMock();
- $this->cmfParameters = $this->getMockBuilder(Parameters::class)->getMock();
- $this->qrCode = $this->getMockBuilder(File::class)->getMock();
- }
- private function makeExporterMock(string $methodUnderTest): LicenceCmfExporter | MockObject
- {
- $exporter = $this->getMockBuilder(LicenceCmfExporter::class)
- ->setConstructorArgs([$this->organizationRepo])
- ->setMethodsExcept(['setAccessRepository', 'setTwig', 'setEncoderIterator',
- 'setEntityManager', 'setEntityManager', 'setStorage', $methodUnderTest])
- ->getMock();
- $exporter->setAccessRepository($this->accessRepo);
- $exporter->setTwig($this->twig);
- $exporter->setEncoderIterator($this->encoderIterator);
- $exporter->setEntityManager($this->em);
- $exporter->setStorage($this->storage);
- return $exporter;
- }
- public function testSupport(): void
- {
- $exporter = $this->makeExporterMock('support');
- $unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock();
- $this->assertTrue($exporter->support($this->exportRequest));
- $this->assertFalse($exporter->support($unsupportedExportRequest));
- }
- private function prepareModelBuilding(): void
- {
- $this->exportRequest->method('getRequesterId')->willReturn(1);
- $this->exportRequest->method('getYear')->willReturn(2020);
- $this->exportRequest->method('getFormat')->willReturn('pdf');
- $this->accessRepo->method('find')->with(1)->willReturn($this->access);
- $this->access->method('getOrganization')->willReturn($this->organization);
- $this->organization->method('getId')->willReturn(1);
- $this->organization->method('getName')->willReturn('my_organization');
- $this->organization->method('getIdentifier')->willReturn('org1');
- $this->organization->method('getNetworkOrganizations')->willReturn($this->collection);
- $this->collection->method('get')->willReturn($this->networkOrgs);
- $this->networkOrgs->expects(self::once())->method('getParent')->willReturn($this->parent);
- $this->parent->expects(self::once())->method('getName')->willReturn('my_network');
- $this->organization->method('getLogo')->willReturn($this->logo);
- $this->logo->method('getId')->willReturn(1);
- $this->storage->method('getDownloadIri')->willReturn('http:://foo.bar/1');
- $this->president->method('getId')->willReturn(1);
- $this->president->method('getGender')->willReturn('M');
- $this->president->method('getGivenName')->willReturn('Joe');
- $this->president->method('getName')->willReturn('Dalton');
- $this->accessRepo
- ->expects(self::once())
- ->method('findByOrganizationAndMission')
- ->with($this->organization, 'PRESIDENT')
- ->willReturn([$this->presidentAccess]);
- $this->presidentAccess->method('getPerson')->willReturn($this->president);
- $this->cmf->method('getParameters')->willReturn($this->cmfParameters);
- $this->cmfParameters->expects(self::once())->method('getQrCode')->willReturn($this->qrCode);
- $this->qrCode->method('getId')->willReturn(1);
- $this->organizationRepo->expects(self::once())->method('find')->with(12097)->willReturn($this->cmf);
- }
- public function testBuildModel(): void
- {
- $exporter = $this->makeExporterMock('buildModel');
- $this->prepareModelBuilding();
- $modelCollection = $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
- $licence = $modelCollection->getLicences()[0];
- $this->assertEquals('my_network', $licence->getFederationName());
- $this->assertEquals(true, $licence->isOrganizationLicence());
- $this->assertEquals(2020, $licence->getYear());
- $this->assertEquals('931572', $licence->getColor());
- $this->assertEquals('http:://foo.bar/1', $licence->getQrCodeUri());
- $this->assertEquals('http:://foo.bar/1', $licence->getLogoUri());
- }
- /**
- * If the organization cannot be determined from the exportRequest author, an error should be thrown
- */
- public function testBuildModelWithoutOrganization(): void
- {
- $exporter = $this->makeExporterMock('buildModel');
- $this->exportRequest->method('getRequesterId')->willReturn(1);
- $this->accessRepo->method('find')->with(1)->willReturn(null);
- $this->expectException(RuntimeException::class);
- $this->invokeMethod($exporter, 'buildModel', [$this->exportRequest]);
- }
- public function testGetFileBasename(): void
- {
- $exporter = $this->makeExporterMock('getFileBasename');
- $licence = $this->getMockBuilder(LicenceCmf::class)->getMock();
- $licence->method('getYear')->willReturn(2020);
- $exportRequest = $this->getMockBuilder(LicenceCmfOrganizationER::class)->getMock();
- $exportRequest->method('getYear')->willReturn(2020);
- $result = $this->invokeMethod($exporter, 'getFileBasename', [$exportRequest]);
- $this->assertEquals(
- 'licence_cmf_2020.pdf',
- $result
- );
- }
- public function testGetFileType(): void
- {
- $exporter = $this->makeExporterMock('getFileType');
- $this->assertEquals(
- 'LICENCE_CMF',
- $this->invokeMethod($exporter, 'getFileType')
- );
- }
- /**
- * Given year is before the start year, first color is used
- */
- public function testGetLicenceColorPastYear(): void
- {
- $exporter = $this->makeExporterMock('getLicenceColor');
- $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR - 10;
- $this->assertEquals(
- LicenceCmfExporter::LICENCE_CMF_COLOR[0],
- $this->invokeMethod($exporter, 'getLicenceColor', [$year])
- );
- }
- /**
- * Given year is within the 5 first years
- */
- public function testGetLicenceColorFirstYears(): void
- {
- $exporter = $this->makeExporterMock('getLicenceColor');
- $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 3;
- $this->assertEquals(
- LicenceCmfExporter::LICENCE_CMF_COLOR[3],
- $this->invokeMethod($exporter, 'getLicenceColor', [$year])
- );
- }
- /**
- * Given year is beyond the 5 first years
- */
- public function testGetLicenceColorNExtYears(): void
- {
- $exporter = $this->makeExporterMock('getLicenceColor');
- $year = LicenceCmfExporter::LICENCE_CMF_COLOR_START_YEAR + 8;
- $this->assertEquals(
- LicenceCmfExporter::LICENCE_CMF_COLOR[3],
- $this->invokeMethod($exporter, 'getLicenceColor', [$year])
- );
- }
- }
|