File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed
src/Transactions/TransactionLineFields
tests/UnitTests/Transactions/TransactionLineFields Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -24,9 +24,9 @@ public function getPeriod(): ?string
24
24
* @param string $period
25
25
* @return $this
26
26
*/
27
- public function setPeriod (string $ period ): self
27
+ public function setPeriod (? string $ period ): self
28
28
{
29
- if (!preg_match ("! \\d{4}/ \\d{1,2}! " , $ period )) {
29
+ if ($ period !== null && !preg_match ("! \\d{4}/ \\d{1,2}! " , $ period )) {
30
30
throw new \InvalidArgumentException ("Period must be in YYYY/PP format (got: {$ period }. " );
31
31
}
32
32
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments