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(TemporaryFileStorage::class)->disableOriginalConstructor()->getMock(); $this->organizationRepo = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock(); $this->uploadStorage = $this->getMockBuilder(UploadStorage::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(); $this->exporter = new LicenceCmfExporter($this->organizationRepo, $this->uploadStorage); $this->exporter->setAccessRepository($this->accessRepo); $this->exporter->setTwig($this->twig); $this->exporter->setEncoderIterator($this->encoderIterator); $this->exporter->setEntityManager($this->em); $this->exporter->setStorage($this->storage); } public function testSupport() { $unsupportedExportRequest = $this->getMockBuilder(ExportRequest::class)->disableOriginalConstructor()->getMock(); $this->assertTrue($this->exporter->support($this->exportRequest)); $this->assertFalse($this->exporter->support($unsupportedExportRequest)); } private function prepareModelBuilding() { $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->uploadStorage->method('getUri')->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() { $this->prepareModelBuilding(); $reflection = new ReflectionClass('App\Service\Export\LicenceCmfExporter'); $this->buildModelMethod = $reflection->getMethod('buildModel'); $this->buildModelMethod->setAccessible(true); $modelCollection = $this->buildModelMethod->invokeArgs($this->exporter, [$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()); } public function testExport() { $this->prepareModelBuilding(); $this->twig ->expects(self::once()) ->method('render') ->with('@templates/export/licence_cmf.html.twig') ->willReturn('
rendered html
'); $this->encoderIterator ->expects(self::once()) ->method('getEncoderFor') ->with('pdf') ->willReturn($this->encoder); $this->encoder ->expects(self::once()) ->method('encode') ->with('
rendered html
') ->willReturn('%%encoded%%'); $this->storage ->expects(self::once()) ->method('write') ->willReturn('/temp/abcd/licence_cmf_2020.pdf'); $file = $this->exporter->export($this->exportRequest); $this->assertMatchesRegularExpression('/licence_cmf_\d{4}.pdf/', $file->getName()); } }