|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpCfdi\XmlCancelacion\Tests\Unit; |
| 6 | + |
| 7 | +use DateTimeImmutable; |
| 8 | +use LogicException; |
| 9 | +use PhpCfdi\XmlCancelacion\Capsule; |
| 10 | +use PhpCfdi\XmlCancelacion\Credentials; |
| 11 | +use PhpCfdi\XmlCancelacion\Tests\TestCase; |
| 12 | +use PhpCfdi\XmlCancelacion\XmlCancelacionHelper; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | + |
| 15 | +/** @covers \PhpCfdi\XmlCancelacion\XmlCancelacionHelper */ |
| 16 | +class XmlCancelacionHelperTest extends TestCase |
| 17 | +{ |
| 18 | + /** @return Credentials&MockObject */ |
| 19 | + private function createFakeCredentials(): Credentials |
| 20 | + { |
| 21 | + /** @var Credentials&MockObject $credentials */ |
| 22 | + $credentials = $this->createMock(Credentials::class); |
| 23 | + return $credentials; |
| 24 | + } |
| 25 | + |
| 26 | + private function createRealCredentials(): Credentials |
| 27 | + { |
| 28 | + $cerFile = $this->filePath('LAN7008173R5.cer.pem'); |
| 29 | + $keyFile = $this->filePath('LAN7008173R5.key.pem'); |
| 30 | + $passPhrase = trim($this->fileContents('LAN7008173R5.password')); |
| 31 | + return new Credentials($cerFile, $keyFile, $passPhrase); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCredentialChanges(): void |
| 35 | + { |
| 36 | + $fakeCredentials = $this->createFakeCredentials(); |
| 37 | + |
| 38 | + $helper = new XmlCancelacionHelper(); |
| 39 | + $this->assertFalse($helper->hasCredentials()); |
| 40 | + |
| 41 | + $this->assertSame($helper, $helper->setCredentials($fakeCredentials)); |
| 42 | + $this->assertTrue($helper->hasCredentials()); |
| 43 | + |
| 44 | + $cerFile = $this->filePath('LAN7008173R5.cer.pem'); |
| 45 | + $keyFile = $this->filePath('LAN7008173R5.key.pem'); |
| 46 | + $passPhrase = trim($this->fileContents('LAN7008173R5.password')); |
| 47 | + $this->assertSame($helper, $helper->setNewCredentials($cerFile, $keyFile, $passPhrase)); |
| 48 | + $this->assertTrue($helper->hasCredentials()); |
| 49 | + $this->assertSame('LAN7008173R5', $helper->getCredentials()->rfc()); |
| 50 | + } |
| 51 | + |
| 52 | + public function testMakeCallsMakeUuids(): void |
| 53 | + { |
| 54 | + $uuid = '11111111-2222-3333-4444-000000000001'; |
| 55 | + $predefinedReturn = 'signed-xml'; |
| 56 | + |
| 57 | + /** @var XmlCancelacionHelper&MockObject $helper */ |
| 58 | + $helper = $this->getMockBuilder(XmlCancelacionHelper::class) |
| 59 | + ->onlyMethods(['makeUuids']) |
| 60 | + ->getMock(); |
| 61 | + $helper->expects($this->once()) |
| 62 | + ->method('makeUuids') |
| 63 | + ->with($this->equalTo([$uuid]), $this->isNull()) |
| 64 | + ->willReturn($predefinedReturn); |
| 65 | + |
| 66 | + $this->assertSame($predefinedReturn, $helper->make($uuid)); |
| 67 | + } |
| 68 | + |
| 69 | + public function testMakeUuids(): void |
| 70 | + { |
| 71 | + $credentials = $this->createRealCredentials(); |
| 72 | + $rfc = $credentials->rfc(); |
| 73 | + $uuids = ['11111111-2222-3333-4444-000000000001', '11111111-2222-3333-4444-000000000002']; |
| 74 | + $helper = new class() extends XmlCancelacionHelper { |
| 75 | + /** @var Capsule */ |
| 76 | + private $capsule; |
| 77 | + |
| 78 | + protected function createCapsule(string $rfc, array $uuids, DateTimeImmutable $dateTime): Capsule |
| 79 | + { |
| 80 | + $this->capsule = parent::createCapsule($rfc, $uuids, $dateTime); |
| 81 | + return $this->capsule; |
| 82 | + } |
| 83 | + |
| 84 | + public function getCreatedCapsule(): Capsule |
| 85 | + { |
| 86 | + return $this->capsule; |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + $now = new DateTimeImmutable(); |
| 91 | + $result = $helper->setCredentials($credentials)->makeUuids($uuids); |
| 92 | + $this->assertStringContainsString('<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">', $result); |
| 93 | + |
| 94 | + /** @var Capsule $spyCapsule */ |
| 95 | + $spyCapsule = $helper->getCreatedCapsule(); |
| 96 | + $spyDate = $spyCapsule->date(); |
| 97 | + $this->assertSame($rfc, $spyCapsule->rfc()); |
| 98 | + $this->assertSame($uuids, $spyCapsule->uuids()); |
| 99 | + $this->assertTrue($spyDate > $now->modify('-1 second') && $spyDate < $now->modify('+1 second')); |
| 100 | + } |
| 101 | + |
| 102 | + public function testGetCredentialsWithoutSettingBefore(): void |
| 103 | + { |
| 104 | + $helper = new XmlCancelacionHelper(); |
| 105 | + $this->expectException(LogicException::class); |
| 106 | + $this->expectExceptionMessage('The object has no credentials'); |
| 107 | + $helper->getCredentials(); |
| 108 | + } |
| 109 | + |
| 110 | + public function testCanConstructWithCredentials(): void |
| 111 | + { |
| 112 | + $credentials = $this->createFakeCredentials(); |
| 113 | + $helper = new XmlCancelacionHelper($credentials); |
| 114 | + $this->assertSame($credentials, $helper->getCredentials()); |
| 115 | + } |
| 116 | +} |
0 commit comments