From ecd6dbd73f6c31f12dc76765e9162f9d149a9727 Mon Sep 17 00:00:00 2001 From: Boris Mikhaylov Date: Wed, 9 Jan 2013 18:46:10 +0100 Subject: [PATCH] to determine context of an example we first check if it has specification and use its class. As a fallback, class that declared example if used. --- src/PHPSpec2/Runner/Runner.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/PHPSpec2/Runner/Runner.php b/src/PHPSpec2/Runner/Runner.php index 6774f6f..4f5ac08 100644 --- a/src/PHPSpec2/Runner/Runner.php +++ b/src/PHPSpec2/Runner/Runner.php @@ -136,7 +136,7 @@ public function runSpecification(Node\Specification $specification) public function runExample(Node\Example $example, Matcher\MatchersCollection $matchers) { - $context = $example->getFunction()->getDeclaringClass()->newInstance(); + $context = $this->getExampleContext($example); $collaborators = new Prophet\CollaboratorsCollection($this->presenter); foreach ($this->getExampleInitializers() as $initializer) { @@ -231,4 +231,20 @@ final public function errorHandler($level, $message, $file, $line) // error reporting turned off or more likely suppressed with @ return false; } + + /** + * Returns an instance of the class in which context we run the example + * @param \PHPSpec2\Loader\Node\Example $example + * @return mixed + */ + private function getExampleContext(Node\Example $example) + { + if (null !== $example->getSpecification()){ + $declaringClass = $example->getSpecification()->getClass(); + } + else { + $declaringClass = $example->getFunction()->getDeclaringClass(); + } + return $declaringClass->newInstance(); + } }