diff --git a/src/Transactions/TransactionLineFields/PeriodField.php b/src/Transactions/TransactionLineFields/PeriodField.php index 13fe4357..347c7a65 100644 --- a/src/Transactions/TransactionLineFields/PeriodField.php +++ b/src/Transactions/TransactionLineFields/PeriodField.php @@ -24,9 +24,9 @@ public function getPeriod(): ?string * @param string $period * @return $this */ - public function setPeriod(string $period): self + public function setPeriod(?string $period): self { - if (!preg_match("!\\d{4}/\\d{1,2}!", $period)) { + if ($period !== null && !preg_match("!\\d{4}/\\d{1,2}!", $period)) { throw new \InvalidArgumentException("Period must be in YYYY/PP format (got: {$period}."); } diff --git a/tests/UnitTests/Transactions/TransactionLineFields/PeriodFieldUnitTest.php b/tests/UnitTests/Transactions/TransactionLineFields/PeriodFieldUnitTest.php new file mode 100644 index 00000000..165b3691 --- /dev/null +++ b/tests/UnitTests/Transactions/TransactionLineFields/PeriodFieldUnitTest.php @@ -0,0 +1,40 @@ +getMockForTrait(PeriodField::class); + + $periodFieldTrait->setPeriod('2020/01'); + + $this->assertEquals('2020/01', $periodFieldTrait->getPeriod()); + } + + /** @test */ + public function testPeriodCanBeNull() + { + $periodFieldTrait = $this->getMockForTrait(PeriodField::class); + + $periodFieldTrait->setPeriod(null); + + $this->assertNull($periodFieldTrait->getPeriod()); + } + + /** @test */ + public function testPeriodInIncorrectFormatThrowsException() + { + $this->expectException(\InvalidArgumentException::class); + + $periodFieldTrait = $this->getMockForTrait(PeriodField::class); + + $periodFieldTrait->setPeriod('asdf'); + } +}