Skip to content

Commit 5e5b94c

Browse files
committed
Added test and improved exception message
1 parent ed9d81d commit 5e5b94c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Match.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function getSubMatchAt($index)
8787
*/
8888
public function getSubMatchOrDefaultAt($index, $default = null)
8989
{
90-
return isset($this->element[$index][0]) ? $this->element[$index][0] : $default;
90+
return isset($this->element[$index]) ? $this->element[$index][0] : $default;
9191
}
9292

9393
/**
@@ -113,7 +113,9 @@ public function getSubmatchOffsetAt($index)
113113
private function validateIndex($index)
114114
{
115115
if (! isset($this->element[$index]) || $index == 0) {
116-
throw new \OutOfBoundsException('Trying to access invalid submatch index');
116+
throw new \OutOfBoundsException(
117+
'Trying to access invalid submatch index ' . $index . 'in match: ' . $this->getFullMatch()
118+
);
117119
}
118120
}
119121
}

tests/unit/Test/MatchTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ public function shouldReturnTheInnerMatch()
7373
$this->assertEquals('456', $this->result->getSubMatchAt(2));
7474
}
7575

76+
/**
77+
* @test
78+
*/
79+
public function shouldReturnTheDefaultIfInnerMatchIsNotSet()
80+
{
81+
$this->assertEquals('potato', $this->result->getSubMatchOrDefaultAt(3, 'potato'));
82+
$this->assertNull($this->result->getSubMatchOrDefaultAt(4));
83+
}
84+
7685
/**
7786
* @test
7887
* @expectedException \OutOfBoundsException

0 commit comments

Comments
 (0)