|
| 1 | +<?php |
| 2 | +namespace Test\Mcustiel\PhpSimpleRegex; |
| 3 | + |
| 4 | +use Mcustiel\PhpSimpleRegex\RegexResponse; |
| 5 | +use Mcustiel\PhpSimpleRegex\Match; |
| 6 | +class RegexResponseTest extends \PHPUnit_Framework_TestCase |
| 7 | +{ |
| 8 | + const PATTERN = '|(\d{4})-(\d{2})-(\d{2})|'; |
| 9 | + const SUBJECT = |
| 10 | + 'Date 1: 2015-01-01, Date 2: 2015-02-02, Date 3: 2015-03-03, Date 4: 2015-04-04, Date 5: 2015-05-05'; |
| 11 | + |
| 12 | + private $result; |
| 13 | + |
| 14 | + /** |
| 15 | + * @before |
| 16 | + */ |
| 17 | + public function createRegexResult() |
| 18 | + { |
| 19 | + $result = []; |
| 20 | + preg_match_all(self::PATTERN, self::SUBJECT, $result, PREG_SET_ORDER); |
| 21 | + $this->result = new RegexResponse($result); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @test |
| 26 | + */ |
| 27 | + public function shouldGetValidMatchesCount() |
| 28 | + { |
| 29 | + $this->assertEquals(5, $this->result->getMatchesCount()); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @test |
| 34 | + */ |
| 35 | + public function shouldGetTheCorrectMatchWhenRequested() |
| 36 | + { |
| 37 | + $match = $this->result->getMatchAt(2); |
| 38 | + $this->assertInstanceOf(Match::class, $match); |
| 39 | + $this->assertEquals('2015-03-03', $match->getFullMatch()); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @test |
| 44 | + * @expectedException \OutOfBoundsException |
| 45 | + * @expectedExceptionMessage Trying to access a match at an invalid index |
| 46 | + */ |
| 47 | + public function shouldThrowAnExceptionWhenInvalidMatchIsRequested() |
| 48 | + { |
| 49 | + $this->result->getMatchAt(10); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @test |
| 54 | + */ |
| 55 | + public function shouldIterateAsAnArray() |
| 56 | + { |
| 57 | + foreach ($this->result as $index => $match) { |
| 58 | + $this->assertInstanceOf(Match::class, $match); |
| 59 | + $this->assertEquals($this->result->getMatchAt($index), $match); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments