Skip to content

Commit d45db2c

Browse files
committed
Merge pull request #17 from chrismichaels84/exception-message
Exception message
2 parents bf7b985 + 021f7eb commit d45db2c

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ $this->specify('this assertion is failing', function() {
179179
?>
180180
```
181181

182+
In both cases, you can optionally test the exception message
183+
184+
``` php
185+
<?php
186+
187+
$this->specify('some exception with a message', function() {
188+
throw new NotFoundException("my error message');
189+
}, ['throws' => ['NotFoundException', 'my error message']]);
190+
?>
191+
```
192+
182193
## Examples
183194

184195
DataProviders alternative. Quite useful for basic data providers.

src/Codeception/Specify.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,56 @@ private function getSpecifyExamples($params)
8585

8686
private function getSpecifyExpectedException($params)
8787
{
88-
$throws = false;
8988
if (isset($params['throws'])) {
90-
$throws = $params['throws'];
89+
$throws = (is_array($params['throws'])) ? $params['throws'][0] : $params['throws'];
90+
9191
if (is_object($throws)) {
9292
$throws = get_class($throws);
9393
}
9494
if ($throws === 'fail') {
9595
$throws = 'PHPUnit_Framework_AssertionFailedError';
9696
}
97+
98+
$message = (is_array($params['throws']) && isset($params['throws'][1])) ? $params['throws'][1] : false;
99+
100+
return [$throws, $message];
97101
}
98-
return $throws;
102+
103+
return false;
99104
}
100105

101106
private function specifyExecute($test, $throws = false, $examples = array())
102107
{
108+
$message = false;
109+
110+
if (is_array($throws)) {
111+
$message = ($throws[1]) ? strtolower($throws[1]) : false;
112+
$throws = $throws[0];
113+
}
114+
103115
$result = $this->getTestResultObject();
104116
try {
105117
call_user_func_array($test, $examples);
106118
} catch (\PHPUnit_Framework_AssertionFailedError $e) {
107-
if ($throws !== get_class($e)) $result->addFailure(clone($this), $e, $result->time());
119+
if ($throws !== get_class($e)){
120+
$result->addFailure(clone($this), $e, $result->time());
121+
}
122+
123+
if ($message !==false && $message !== strtolower($e->getMessage())) {
124+
$f = new \PHPUnit_Framework_AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
125+
$result->addFailure(clone($this), $f, $result->time());
126+
}
108127
} catch (\Exception $e) {
109128
if ($throws) {
110129
if ($throws !== get_class($e)) {
111130
$f = new \PHPUnit_Framework_AssertionFailedError("exception '$throws' was expected, but " . get_class($e) . ' was thrown');
112131
$result->addFailure(clone($this), $f, $result->time());
113132
}
133+
134+
if ($message !==false && $message !== strtolower($e->getMessage())) {
135+
$f = new \PHPUnit_Framework_AssertionFailedError("exception message '$message' was expected, but '" . $e->getMessage() . "' was received");
136+
$result->addFailure(clone($this), $f, $result->time());
137+
}
114138
} else {
115139
throw $e;
116140
}

tests/SpecifyTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function testSpecification()
1616
$this->user->name = 'jon';
1717
$this->assertEquals('jon', $this->user->name);
1818
});
19-
19+
2020
$this->assertEquals('davert', $this->user->name);
2121

2222
$this->specify('i can fail here but test goes on', function() {
@@ -57,7 +57,7 @@ function testAfterCallback()
5757
$this->user = "jon";
5858
});
5959
$this->assertEquals('davert', $this->user);
60-
}
60+
}
6161

6262
function testMultiAfterCallback()
6363
{
@@ -104,6 +104,33 @@ public function testExceptions()
104104
}, ['throws' => 'fail']);
105105
}
106106

107+
public function testExceptionsWithMessages()
108+
{
109+
$this->specify('user is invalid', function() {
110+
throw new Exception("test message");
111+
}, ['throws' => ['Exception', 'test message']]);
112+
113+
$this->specify('user is invalid', function() {
114+
throw new RuntimeException("test message");
115+
}, ['throws' => ['RuntimeException', 'test message']]);
116+
117+
$this->specify('user is invalid', function() {
118+
throw new RuntimeException("test message");
119+
}, ['throws' => [new RuntimeException(), "test message"]]);
120+
121+
$this->specify('i can handle fails', function() {
122+
$this->fail("test message");
123+
}, ['throws' => ['fail', 'test message']]);
124+
125+
$this->specify('ignores an empty message', function() {
126+
$this->fail("test message");
127+
}, ['throws' => ['fail']]);
128+
129+
$this->specify('mixed case exception messages', function() {
130+
throw new RuntimeException("teSt mESSage");
131+
}, ['throws' => ['RuntimeException', 'Test MessaGE']]);
132+
}
133+
107134
/**
108135
* @expectedException RuntimeException
109136
*/
@@ -140,7 +167,7 @@ public function testDeepCopy()
140167
$this->assertEquals(2, $this->a->prop->prop);
141168
});
142169
$this->assertEquals(1, $this->a->prop->prop);
143-
170+
144171
}
145172

146173
public function testConfiguration()

0 commit comments

Comments
 (0)