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]) ); } }