|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpTwinfield\UnitTests; |
| 4 | + |
| 5 | +use Money\Currency; |
| 6 | +use Money\Money; |
| 7 | +use PhpTwinfield\ApiConnectors\BankTransactionApiConnector; |
| 8 | +use PhpTwinfield\ApiConnectors\MatchesApiConnector; |
| 9 | +use PhpTwinfield\BankTransaction; |
| 10 | +use PhpTwinfield\Enums\Destiny; |
| 11 | +use PhpTwinfield\Enums\LineType; |
| 12 | +use PhpTwinfield\Enums\MatchCode; |
| 13 | +use PhpTwinfield\Exception; |
| 14 | +use PhpTwinfield\MatchLine; |
| 15 | +use PhpTwinfield\MatchSet; |
| 16 | +use PhpTwinfield\Office; |
| 17 | +use PhpTwinfield\Response\Response; |
| 18 | +use PhpTwinfield\Response\ResponseException; |
| 19 | +use PhpTwinfield\Secure\AuthenticatedConnection; |
| 20 | +use PhpTwinfield\Services\ProcessXmlService; |
| 21 | +use PhpTwinfield\Transactions\BankTransactionLine\Detail; |
| 22 | +use PhpTwinfield\Transactions\BankTransactionLine\Total; |
| 23 | +use PhpTwinfield\Transactions\BankTransactionLine\Vat; |
| 24 | +use PHPUnit\Framework\TestCase; |
| 25 | + |
| 26 | +class MatchesApiConnectorTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var MatchesApiConnector |
| 30 | + */ |
| 31 | + protected $apiConnector; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ProcessXmlService|\PHPUnit_Framework_MockObject_MockObject |
| 35 | + */ |
| 36 | + protected $processXmlService; |
| 37 | + |
| 38 | + protected function setUp() |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->processXmlService = $this->getMockBuilder(ProcessXmlService::class) |
| 43 | + ->setMethods(["sendDocument"]) |
| 44 | + ->disableOriginalConstructor() |
| 45 | + ->getMock(); |
| 46 | + |
| 47 | + /** @var AuthenticatedConnection|\PHPUnit_Framework_MockObject_MockObject $connection */ |
| 48 | + $connection = $this->createMock(AuthenticatedConnection::class); |
| 49 | + $connection |
| 50 | + ->expects($this->any()) |
| 51 | + ->method("getAuthenticatedClient") |
| 52 | + ->willReturn($this->processXmlService); |
| 53 | + |
| 54 | + $this->apiConnector = new MatchesApiConnector($connection); |
| 55 | + } |
| 56 | + |
| 57 | + private function createMatchSet(): MatchSet |
| 58 | + { |
| 59 | + $matchSet = new MatchSet(); |
| 60 | + $matchSet->setOffice(Office::fromCode("OFFICE001")); |
| 61 | + $matchSet->setMatchCode(MatchCode::CUSTOMERS()); |
| 62 | + $matchSet->setMatchDate(new \DateTimeImmutable("2018-02-19")); |
| 63 | + |
| 64 | + return $matchSet; |
| 65 | + } |
| 66 | + |
| 67 | + public function testSendAllReturnsMappedObjects() |
| 68 | + { |
| 69 | + $response = Response::fromString(file_get_contents( |
| 70 | + __DIR__."/resources/single-matchset.xml" |
| 71 | + )); |
| 72 | + |
| 73 | + $this->processXmlService->expects($this->once()) |
| 74 | + ->method("sendDocument") |
| 75 | + ->willReturn($response); |
| 76 | + |
| 77 | + $responses = $this->apiConnector->sendAll([ |
| 78 | + $this->createMatchSet(), |
| 79 | + $this->createMatchSet() |
| 80 | + ]); |
| 81 | + |
| 82 | + $this->assertCount(2, $responses); |
| 83 | + |
| 84 | + self::assertSame(1, $responses->countFailedResponses()); |
| 85 | + self::assertSame(1, $responses->countSuccessfulResponses()); |
| 86 | + |
| 87 | + [$response1, $response2] = $responses; |
| 88 | + |
| 89 | + /** @var MatchSet $matchSet */ |
| 90 | + $matchSet = $response1->unwrap(); |
| 91 | + |
| 92 | + $this->assertEquals("OFFICE001", $matchSet->getOffice()->getCode()); |
| 93 | + $this->assertEquals(MatchCode::CUSTOMERS(), $matchSet->getMatchCode()); |
| 94 | + $this->assertEquals("2018-02-19", $matchSet->getMatchDate()->format('Y-m-d')); |
| 95 | + |
| 96 | + $lines = $matchSet->getLines(); |
| 97 | + $this->assertCount(2, $lines); |
| 98 | + |
| 99 | + $line = $lines[0]; |
| 100 | + $this->assertEquals("VRK", $line->getTranscode()); |
| 101 | + $this->assertSame(201700103, $line->getTransnumber()); |
| 102 | + $this->assertSame(1, $line->getTransline()); |
| 103 | + $this->assertEquals(Money::EUR(526), $line->getMatchValue()); |
| 104 | + |
| 105 | + $line = $lines[1]; |
| 106 | + $this->assertEquals("DUM2", $line->getTranscode()); |
| 107 | + $this->assertSame(201700029, $line->getTransnumber()); |
| 108 | + $this->assertSame(79, $line->getTransline()); |
| 109 | + $this->assertEquals(Money::EUR(-526), $line->getMatchValue()); |
| 110 | + |
| 111 | + /** @var MatchSet $matchSet */ |
| 112 | + try { |
| 113 | + $matchSet = $response2->unwrap(); |
| 114 | + } catch (ResponseException $e) { |
| 115 | + $matchSet = $e->getReturnedObject(); |
| 116 | + } |
| 117 | + |
| 118 | + $this->assertEquals("OFFICE001", $matchSet->getOffice()->getCode()); |
| 119 | + $this->assertEquals(MatchCode::CUSTOMERS(), $matchSet->getMatchCode()); |
| 120 | + $this->assertEquals("2018-02-19", $matchSet->getMatchDate()->format('Y-m-d')); |
| 121 | + |
| 122 | + $lines = $matchSet->getLines(); |
| 123 | + $this->assertCount(2, $lines); |
| 124 | + |
| 125 | + $line = $lines[0]; |
| 126 | + $this->assertEquals("VRK", $line->getTranscode()); |
| 127 | + $this->assertSame(201700100, $line->getTransnumber()); |
| 128 | + $this->assertSame(1, $line->getTransline()); |
| 129 | + $this->assertEquals(Money::EUR(140), $line->getMatchValue()); |
| 130 | + |
| 131 | + $line = $lines[1]; |
| 132 | + $this->assertEquals("DUM2", $line->getTranscode()); |
| 133 | + $this->assertSame(201700029, $line->getTransnumber()); |
| 134 | + $this->assertSame(64, $line->getTransline()); |
| 135 | + $this->assertEquals(Money::EUR(-140), $line->getMatchValue()); |
| 136 | + } |
| 137 | +} |
0 commit comments