Skip to content

Commit 322824b

Browse files
committed
Added tests and fixed errors.
1 parent 4ab5515 commit 322824b

File tree

5 files changed

+178
-17
lines changed

5 files changed

+178
-17
lines changed

src/Executor.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@ class Executor
1717
* @param number
1818
* $offset
1919
*
20+
* @throws \RuntimeException
21+
* @throws \InvalidArgumentException
22+
*
2023
* @return \Mcustiel\PhpSimpleRegex\MatchResult
2124
*/
2225
public function getAllMatches($pattern, $subject, $offset = 0)
2326
{
2427
$matches = array();
25-
preg_match_all(
28+
$result = @preg_match_all(
2629
$this->getPatternByType($pattern),
2730
$subject,
2831
$matches,
2932
PREG_SET_ORDER | PREG_OFFSET_CAPTURE,
3033
$offset
3134
);
3235

36+
$this->checkResultIsOkOrThrowException($result, $pattern);
37+
3338
return new MatchResult($matches);
3439
}
3540

@@ -41,16 +46,18 @@ public function getAllMatches($pattern, $subject, $offset = 0)
4146
* @param number
4247
* $offset
4348
*
49+
* @throws \RuntimeException
50+
* @throws \InvalidArgumentException
51+
*
4452
* @return \Mcustiel\PhpSimpleRegex\MatchResult|NULL
4553
*/
4654
public function getOneMatch($pattern, $subject, $offset = 0)
4755
{
4856
$matches = array();
4957
$pattern = $this->getPatternByType($pattern);
50-
if (preg_match($pattern, $subject, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE, $offset)) {
51-
return (new MatchResult($matches))->getMatchAt(0);
52-
}
53-
return null;
58+
$result = @preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, $offset);
59+
$this->checkResultIsOkOrThrowException($result, $pattern);
60+
return $result? new Match($matches) : null;
5461
}
5562

5663
/**
@@ -61,17 +68,23 @@ public function getOneMatch($pattern, $subject, $offset = 0)
6168
* @param number
6269
* $offset
6370
*
71+
* @throws \RuntimeException
72+
* @throws \InvalidArgumentException
73+
*
6474
* @return boolean
6575
*/
6676
public function match($pattern, $subject, $offset = 0)
6777
{
68-
return (bool) preg_match(
78+
$matches = [];
79+
$result = @preg_match(
6980
$this->getPatternByType($pattern),
7081
$subject,
71-
null,
72-
PREG_PATTERN_ORDER,
82+
$matches,
83+
0,
7384
$offset
7485
);
86+
$this->checkResultIsOkOrThrowException($result, $pattern);
87+
return (boolean) $result;
7588
}
7689

7790
/**
@@ -84,6 +97,9 @@ public function match($pattern, $subject, $offset = 0)
8497
* @param number
8598
* $limit
8699
*
100+
* @throws \RuntimeException
101+
* @throws \InvalidArgumentException
102+
*
87103
* @return \Mcustiel\PhpSimpleRegex\ReplaceResult
88104
*/
89105
public function replaceAndCount($pattern, $replacement, $subject, $limit = -1)
@@ -104,6 +120,9 @@ public function replaceAndCount($pattern, $replacement, $subject, $limit = -1)
104120
* @param number
105121
* $limit
106122
*
123+
* @throws \RuntimeException
124+
* @throws \InvalidArgumentException
125+
*
107126
* @return string
108127
*/
109128
public function replace($pattern, $replacement, $subject, $limit = -1)
@@ -121,6 +140,9 @@ public function replace($pattern, $replacement, $subject, $limit = -1)
121140
* @param number
122141
* $limit
123142
*
143+
* @throws \RuntimeException
144+
* @throws \InvalidArgumentException
145+
*
124146
* @return string
125147
*/
126148
public function replaceCallback($pattern, callable $callback, $subject, $limit = -1)
@@ -137,6 +159,8 @@ public function replaceCallback($pattern, callable $callback, $subject, $limit =
137159
* $subject
138160
* @param number
139161
* $limit
162+
* @throws \RuntimeException
163+
* @throws \InvalidArgumentException
140164
*
141165
* @return \Mcustiel\PhpSimpleRegex\ReplaceResult
142166
*/
@@ -151,7 +175,7 @@ public function replaceCallbackAndCount($pattern, callable $callback, $subject,
151175
/**
152176
* @param mixed $pattern
153177
* @throws \InvalidArgumentException
154-
* @return unknown
178+
* @return string
155179
*/
156180
private function getPatternByType($pattern)
157181
{
@@ -171,4 +195,16 @@ private function getPatternByType($pattern)
171195
. ' or an instance of SelvinOrtiz\Utils\Flux\Flux'
172196
);
173197
}
198+
199+
/**
200+
* @param string $pattern
201+
* @param boolean $result
202+
* @throws \RuntimeException
203+
*/
204+
private function checkResultIsOkOrThrowException($result, $pattern)
205+
{
206+
if ($result === false) {
207+
throw new \RuntimeException('An error occurred executing the pattern ' . $pattern);
208+
}
209+
}
174210
}

src/Match.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,26 @@ public function __construct(array $responseElement)
2525

2626
/**
2727
*
28-
* @return mixed
28+
* @return null|string
2929
*/
3030
public function getFullMatch()
3131
{
3232
return empty($this->element) ? null : $this->element[0][0];
3333
}
3434

35+
/**
36+
* @return null|int
37+
*/
38+
public function getOffset()
39+
{
40+
return empty($this->element) ? null : $this->element[0][1];
41+
}
42+
3543
/**
3644
*
37-
* @param unknown $index
45+
* @param int $index
3846
* @throws \OutOfBoundsException
39-
* @return mixed
47+
* @return string
4048
*/
4149
public function getSubMatchAt($index)
4250
{
@@ -45,12 +53,21 @@ public function getSubMatchAt($index)
4553
return $this->element[$index][0];
4654
}
4755

56+
/**
57+
* @param int $index
58+
* @throws \OutOfBoundsException
59+
* @return int
60+
*/
4861
public function getSubmatchOffsetAt($index)
4962
{
5063
$this->validateIndex($index);
5164
return $this->element[$index][1];
5265
}
5366

67+
/**
68+
* @param int $index
69+
* @throws \OutOfBoundsException
70+
*/
5471
private function validateIndex($index)
5572
{
5673
if (! isset($this->element[$index]) || $index == 0) {

src/MatchResult.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
*/
99
class MatchResult implements \Iterator
1010
{
11+
/**
12+
* @var array
13+
*/
1114
protected $response;
15+
/**
16+
* @var int
17+
*/
1218
private $current;
19+
/**
20+
* @var int
21+
*/
1322
private $count;
1423

1524
/**
@@ -25,7 +34,7 @@ public function __construct(array $regexResponse)
2534

2635
/**
2736
*
28-
* @return number
37+
* @return int
2938
*/
3039
public function getMatchesCount()
3140
{
@@ -34,19 +43,22 @@ public function getMatchesCount()
3443

3544
/**
3645
*
37-
* @param unknown $index
46+
* @param int $index
3847
* @throws \OutOfBoundsException
3948
* @return \Mcustiel\PhpSimpleRegex\Match
4049
*/
4150
public function getMatchAt($index)
4251
{
43-
if (! isset($this->response[$index])) {
52+
if (!isset($this->response[$index])) {
4453
throw new \OutOfBoundsException('Trying to access a match at an invalid index');
4554
}
4655

4756
return new Match($this->response[$index]);
4857
}
4958

59+
/**
60+
* @return \Mcustiel\PhpSimpleRegex\Match
61+
*/
5062
public function current()
5163
{
5264
return new Match($this->response[$this->current]);

tests/unit/Test/ExecutorTest.php

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,96 @@
11
<?php
22
namespace Test\Mcustiel\PhpSimpleRegex;
33

4-
class ExecutorTest
4+
use Mcustiel\PhpSimpleRegex\Executor;
5+
use Mcustiel\PhpSimpleRegex\MatchResult;
6+
use Mcustiel\PhpSimpleRegex\Match;
7+
8+
class ExecutorTest extends \PHPUnit_Framework_TestCase
59
{
10+
const PATTERN = '/(a|b)\d+/';
11+
const MATCHING_SUBJECT = 'a12z56b34hzyp5a5opqb9';
12+
const NOT_MATCHING_SUBJECT = 'abcdefghijklmnopqrstuvwxyz';
13+
14+
/**
15+
* @var \Mcustiel\PhpSimpleRegex\Executor
16+
*/
17+
private $executor;
18+
19+
/**
20+
* @before
21+
*/
22+
public function initExecutor()
23+
{
24+
$this->executor = new Executor();
25+
}
26+
27+
/**
28+
* @test
29+
*/
30+
public function shouldReturnAListOfValidMatchesWhenGetAllMatchesCalledCorrectly()
31+
{
32+
$result = $this->executor->getAllMatches(self::PATTERN, self::MATCHING_SUBJECT);
33+
$this->assertInstanceOf(MatchResult::class, $result);
34+
$this->assertEquals(4, $result->getMatchesCount());
35+
$this->assertEquals('a5', $result->getMatchAt(2)->getFullMatch());
36+
$this->assertEquals('a', $result->getMatchAt(2)->getSubMatchAt(1));
37+
$this->assertEquals(14, $result->getMatchAt(2)->getSubmatchOffsetAt(1));
38+
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function shouldReturnAnEmptyListOfMatchesWhenGetAllMatchesDoesNotMatchAnything()
44+
{
45+
$result = $this->executor->getAllMatches(self::PATTERN, self::NOT_MATCHING_SUBJECT);
46+
$this->assertInstanceOf(MatchResult::class, $result);
47+
$this->assertEquals(0, $result->getMatchesCount());
48+
}
49+
50+
/**
51+
* @test
52+
*/
53+
public function shouldReturnAMatchesWhenGetOneMatchCalledCorrectly()
54+
{
55+
$result = $this->executor->getOneMatch(self::PATTERN, self::MATCHING_SUBJECT);
56+
$this->assertInstanceOf(Match::class, $result);
57+
$this->assertEquals('a12', $result->getFullMatch());
58+
$this->assertEquals('a', $result->getSubMatchAt(1));
59+
$this->assertEquals(0, $result->getSubmatchOffsetAt(1));
60+
}
61+
62+
/**
63+
* @test
64+
*/
65+
public function shouldReturnNullWhenGetOneMatchDoesNotMatchAnything()
66+
{
67+
$result = $this->executor->getOneMatch(self::PATTERN, self::NOT_MATCHING_SUBJECT);
68+
$this->assertNull($result);
69+
}
70+
71+
/**
72+
* @test
73+
*/
74+
public function shouldReturnTrueIfMatchCalledAndPatternMatch()
75+
{
76+
$this->assertTrue($this->executor->match(self::PATTERN, self::MATCHING_SUBJECT));
77+
}
78+
79+
/**
80+
* @test
81+
*/
82+
public function shouldReturnFalseIfMatchCalledAndPatternDoesNotMatch()
83+
{
84+
$this->assertFalse($this->executor->match(self::PATTERN, self::NOT_MATCHING_SUBJECT));
85+
}
686

87+
/**
88+
* @test
89+
* @expectedException \RuntimeException
90+
* @expectedExceptionMessage An error occurred executing the pattern a^b**z
91+
*/
92+
public function shouldThrowAnExceptionIfMatchCalledAndPatternIsNotRight()
93+
{
94+
$this->assertFalse($this->executor->match('a^b**z', self::NOT_MATCHING_SUBJECT));
95+
}
796
}

tests/unit/Test/MatchTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function shouldReturnTheFullMatch()
3131
$this->assertEquals(self::SUBJECT, $this->result->getFullMatch());
3232
}
3333

34+
/**
35+
* @test
36+
*/
37+
public function shouldReturnTheOffset()
38+
{
39+
$this->assertEquals(0, $this->result->getOffset());
40+
}
41+
3442
/**
3543
* @test
3644
*/
@@ -40,7 +48,6 @@ public function shouldReturnTheOffsetOfTheInnerMatch()
4048
$this->assertEquals(5, $this->result->getSubmatchOffsetAt(2));
4149
}
4250

43-
4451
/**
4552
* @test
4653
*/

0 commit comments

Comments
 (0)