Skip to content

Commit

Permalink
Merge pull request #161 from nthmedia/master
Browse files Browse the repository at this point in the history
Transaction Period should be able to be empty
  • Loading branch information
Willem Stuursma-Ruwen authored Feb 14, 2020
2 parents eb0cb0f + 282cd46 commit 9605ed8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Transactions/TransactionLineFields/PeriodField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use PhpTwinfield\Transactions\TransactionLineFields\PeriodField;
use PHPUnit\Framework\TestCase;

/**
* @covers \PhpTwinfield\Transactions\TransactionLineFields\PeriodField
*/
class PeriodFieldUnitTest extends TestCase
{
/** @test */
public function testPeriodInCorrectFormat()
{
$periodFieldTrait = $this->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');
}
}

0 comments on commit 9605ed8

Please sign in to comment.