Skip to content

Commit 8e9cd1f

Browse files
committed
Added ability to define multiple before/afterSpecify callbacks
1 parent c615e07 commit 8e9cd1f

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/Codeception/Specify.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
trait Specify {
88

9-
private $beforeSpecify;
10-
private $afterSpecify;
9+
private $beforeSpecify = array();
10+
private $afterSpecify = array();
1111

1212
/**
1313
* @var Specify\Config
@@ -46,15 +46,23 @@ function specify($specification, \Closure $callable = null, $params = [])
4646
// copy current object properties
4747
$this->specifyCloneProperties($properties);
4848

49-
if ($this->beforeSpecify instanceof \Closure) $this->beforeSpecify->__invoke();
49+
if (!empty($this->beforeSpecify) && is_array($this->beforeSpecify)) {
50+
foreach ($this->beforeSpecify as $closure) {
51+
if ($closure instanceof \Closure) $closure->__invoke();
52+
}
53+
}
5054
$this->specifyExecute($test, $throws, $example);
5155

5256
// restore object properties
5357
foreach ($properties as $property => $val) {
5458
if ($this->specifyConfig->propertyIgnored($property)) continue;
5559
$this->$property = $val;
5660
}
57-
if ($this->afterSpecify instanceof \Closure) $this->afterSpecify->__invoke();
61+
if (!empty($this->afterSpecify) && is_array($this->afterSpecify)) {
62+
foreach ($this->afterSpecify as $closure) {
63+
if ($closure instanceof \Closure) $closure->__invoke();
64+
}
65+
}
5866
}
5967

6068
// restore test name
@@ -126,17 +134,17 @@ public function specifyConfig()
126134

127135
function beforeSpecify(\Closure $callable = null)
128136
{
129-
$this->beforeSpecify = $callable->bindTo($this);
137+
$this->beforeSpecify[] = $callable->bindTo($this);
130138
}
131139

132140
function afterSpecify(\Closure $callable = null)
133141
{
134-
$this->afterSpecify = $callable->bindTo($this);
142+
$this->afterSpecify[] = $callable->bindTo($this);
135143
}
136144

137145
function cleanSpecify()
138146
{
139-
$this->beforeSpecify = $this->afterSpecify = null;
147+
$this->beforeSpecify = $this->afterSpecify = array();
140148
}
141149

142150
/**

tests/SpecifyTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ function testBeforeCallback()
3535
});
3636
}
3737

38+
function testMultiBeforeCallback()
39+
{
40+
$this->beforeSpecify(function() {
41+
$this->user = "davert";
42+
});
43+
$this->beforeSpecify(function() {
44+
$this->user .= "jon";
45+
});
46+
$this->specify("user should be davertjon", function() {
47+
$this->assertEquals('davertjon', $this->user);
48+
});
49+
}
50+
3851
function testAfterCallback()
3952
{
4053
$this->afterSpecify(function() {
@@ -46,6 +59,20 @@ function testAfterCallback()
4659
$this->assertEquals('davert', $this->user);
4760
}
4861

62+
function testMultiAfterCallback()
63+
{
64+
$this->afterSpecify(function() {
65+
$this->user = "davert";
66+
});
67+
$this->afterSpecify(function() {
68+
$this->user .= "jon";
69+
});
70+
$this->specify("user should be davertjon", function() {
71+
$this->user = "jon";
72+
});
73+
$this->assertEquals('davertjon', $this->user);
74+
}
75+
4976
function testCleanSpecifyCallbacks()
5077
{
5178
$this->afterSpecify(function() {

0 commit comments

Comments
 (0)