Skip to content

Commit 9605ed8

Browse files
author
Willem Stuursma-Ruwen
authored
Merge pull request #161 from nthmedia/master
Transaction Period should be able to be empty
2 parents eb0cb0f + 282cd46 commit 9605ed8

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/Transactions/TransactionLineFields/PeriodField.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function getPeriod(): ?string
2424
* @param string $period
2525
* @return $this
2626
*/
27-
public function setPeriod(string $period): self
27+
public function setPeriod(?string $period): self
2828
{
29-
if (!preg_match("!\\d{4}/\\d{1,2}!", $period)) {
29+
if ($period !== null && !preg_match("!\\d{4}/\\d{1,2}!", $period)) {
3030
throw new \InvalidArgumentException("Period must be in YYYY/PP format (got: {$period}.");
3131
}
3232

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use PhpTwinfield\Transactions\TransactionLineFields\PeriodField;
4+
use PHPUnit\Framework\TestCase;
5+
6+
/**
7+
* @covers \PhpTwinfield\Transactions\TransactionLineFields\PeriodField
8+
*/
9+
class PeriodFieldUnitTest extends TestCase
10+
{
11+
/** @test */
12+
public function testPeriodInCorrectFormat()
13+
{
14+
$periodFieldTrait = $this->getMockForTrait(PeriodField::class);
15+
16+
$periodFieldTrait->setPeriod('2020/01');
17+
18+
$this->assertEquals('2020/01', $periodFieldTrait->getPeriod());
19+
}
20+
21+
/** @test */
22+
public function testPeriodCanBeNull()
23+
{
24+
$periodFieldTrait = $this->getMockForTrait(PeriodField::class);
25+
26+
$periodFieldTrait->setPeriod(null);
27+
28+
$this->assertNull($periodFieldTrait->getPeriod());
29+
}
30+
31+
/** @test */
32+
public function testPeriodInIncorrectFormatThrowsException()
33+
{
34+
$this->expectException(\InvalidArgumentException::class);
35+
36+
$periodFieldTrait = $this->getMockForTrait(PeriodField::class);
37+
38+
$periodFieldTrait->setPeriod('asdf');
39+
}
40+
}

0 commit comments

Comments
 (0)