Skip to content

Commit

Permalink
Merge pull request #276 from l3l0/fix/issue269
Browse files Browse the repository at this point in the history
Fix issue #269 with exception masking and generation class/method bug
  • Loading branch information
ciaranmcnulty committed Feb 3, 2014
2 parents 12e8105 + da6652b commit 785ceef
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 4 deletions.
5 changes: 4 additions & 1 deletion features/bootstrap/PhpSpecContext.php
Expand Up @@ -69,7 +69,10 @@ public function iStartDescribing($class)
*/
public function theFileContains($file, PyStringNode $string)
{
mkdir(dirname($file), 0777, true);
$dirname = dirname($file);
if (!file_exists($dirname)) {
mkdir($dirname, 0777, true);
}

file_put_contents($file, $string->getRaw());

Expand Down
59 changes: 59 additions & 0 deletions features/code_generation/developer_generates_class.feature
Expand Up @@ -17,3 +17,62 @@ Feature: Developer generates a class
}
"""

@issue269
Scenario: Generating a class when expectations on collaborator are defined
Given the spec file "spec/CodeGeneration/MethodExample2/ForgotPasswordSpec.php" contains:
"""
<?php
namespace spec\CodeGeneration\MethodExample2;
use CodeGeneration\MethodExample2\UserRepository;
use CodeGeneration\MethodExample2\User;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class ForgotPasswordSpec extends ObjectBehavior
{
function it_changes_password_for_user(UserRepository $repository, User $user)
{
$repository->findOneByEmail('leszek.prabucki@gmail.com')->willReturn($user);
$user->changePassword('123')->shouldBeCalled();
$this->changePassword('leszek.prabucki@gmail.com', '123');
}
}
"""
And the class file "src/CodeGeneration/MethodExample2/User.php" contains:
"""
<?php
namespace CodeGeneration\MethodExample2;
interface User
{
public function changePassword($newPassword);
}
"""
And the class file "src/CodeGeneration/MethodExample2/UserRepository.php" contains:
"""
<?php
namespace CodeGeneration\MethodExample2;
interface UserRepository
{
public function findOneByEmail($email);
}
"""
When I run phpspec and answer "y" when asked if I want to generate the code
Then the class in "src/CodeGeneration/MethodExample2/ForgotPassword.php" should contain:
"""
<?php
namespace CodeGeneration\MethodExample2;
class ForgotPassword
{
}
"""
86 changes: 86 additions & 0 deletions features/runner/developer_runs_specs.feature
Expand Up @@ -44,3 +44,89 @@ Feature: Developer runs the specs
"""
When I run phpspec
Then the suite should pass

@issue214
Scenario: Letgo is executed after successful spec
Given the spec file "spec/Runner/SpecExample3/MarkdownSpec.php" contains:
"""
<?php
namespace spec\Runner\SpecExample3;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class MarkdownSpec extends ObjectBehavior
{
function letgo()
{
throw new \Exception('Letgo is called');
}
function it_converts_plain_text_to_html_paragraphs()
{
$this->toHtml('Hi, there')->shouldReturn('<p>Hi, there</p>');
}
}
"""
And the class file "src/Runner/SpecExample3/Markdown.php" contains:
"""
<?php
namespace Runner\SpecExample3;
class Markdown
{
public function toHtml($text)
{
return sprintf('<p>%s</p>', $text);
}
}
"""
When I run phpspec
Then I should see "Letgo is called"

@issue214
Scenario: Letgo is executed after exception is thrown
Given the spec file "spec/Runner/SpecExample4/MarkdownSpec.php" contains:
"""
<?php
namespace spec\Runner\SpecExample4;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class MarkdownSpec extends ObjectBehavior
{
function letgo()
{
throw new \Exception('Letgo is called');
}
function it_converts_plain_text_to_html_paragraphs()
{
$this->toHtml('Hi, there')->shouldReturn('<p>Hi, there</p>');
}
}
"""
And the class file "src/Runner/SpecExample4/Markdown.php" contains:
"""
<?php
namespace Runner\SpecExample4;
class Markdown
{
public function toHtml($text)
{
throw new \Exception('Some exception');
}
}
"""
When I run phpspec
Then I should see "Letgo is called"
5 changes: 3 additions & 2 deletions spec/PhpSpec/Runner/ExampleRunnerSpec.php
Expand Up @@ -3,6 +3,7 @@
namespace spec\PhpSpec\Runner;

use PhpSpec\ObjectBehavior;
use PhpSpec\Runner\Maintainer\LetAndLetgoMaintainer;
use Prophecy\Argument;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -112,9 +113,9 @@ function it_runs_all_supported_maintainers_before_and_after_each_example(
$this->run($example);
}

function it_runs_all_supported_maintainers_before_and_after_each_example_if_the_example_throws_an_exception(
function it_runs_let_and_letgo_maintainer_before_and_after_each_example_if_the_example_throws_an_exception(
ExampleNode $example, SpecificationNode $specification, ReflectionClass $specReflection,
$context, ReflectionMethod $exampReflection, MaintainerInterface $maintainer,
$context, ReflectionMethod $exampReflection, LetAndLetgoMaintainer $maintainer,
SpecificationInterface $context
)
{
Expand Down
23 changes: 22 additions & 1 deletion src/PhpSpec/Runner/ExampleRunner.php
Expand Up @@ -15,6 +15,7 @@

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use PhpSpec\Runner\Maintainer\LetAndLetgoMaintainer;
use PhpSpec\Formatter\Presenter\PresenterInterface;
use PhpSpec\SpecificationInterface;
use PhpSpec\Event\ExampleEvent;
Expand Down Expand Up @@ -141,7 +142,13 @@ protected function executeExample(SpecificationInterface $context, ExampleNode $
try {
$reflection->invokeArgs($context, $collaborators->getArgumentsFor($reflection));
} catch (\Exception $e) {
$this->runMaintainersTeardown($maintainers, $example, $context, $matchers, $collaborators);
$this->runMaintainersTeardown(
$this->searchExceptionMaintainers($maintainers),
$example,
$context,
$matchers,
$collaborators
);
throw $e;
}

Expand All @@ -161,4 +168,18 @@ private function runMaintainersTeardown($maintainers, $example, $context, $match
$maintainer->teardown($example, $context, $matchers, $collaborators);
}
}

/**
* @param Maintainer\MaintainerInterface[] $maintainers
* @return Maintainer\MaintainerInterface[]
*/
private function searchExceptionMaintainers($maintainers)
{
return array_filter(
$maintainers,
function ($maintainer) {
return $maintainer instanceof LetAndLetgoMaintainer;
}
);
}
}

0 comments on commit 785ceef

Please sign in to comment.