Skip to content

Commit 2a16a09

Browse files
committed
Merge pull request #7 from BenConstable/fix-laravel-bootstrapping
Only bootstrap Laravel env for Laravel specs
2 parents c0f3ea1 + 69386c0 commit 2a16a09

File tree

4 files changed

+53
-21
lines changed

4 files changed

+53
-21
lines changed

spec/PhpSpec/Laravel/Listener/LaravelListenerSpec.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use Prophecy\Argument;
77
use PhpSpec\Laravel\Util\Laravel;
88
use PhpSpec\Event\SpecificationEvent;
9+
use PhpSpec\Loader\Node\SpecificationNode;
10+
use ReflectionClass;
911

1012
class LaravelListenerSpec extends ObjectBehavior
1113
{
@@ -19,8 +21,26 @@ function it_is_a_listener()
1921
$this->shouldHaveType('Symfony\Component\EventDispatcher\EventSubscriberInterface');
2022
}
2123

22-
function it_refreshes_the_laravel_framework_before_spec_is_run(Laravel $laravel, SpecificationEvent $event)
24+
function it_refreshes_the_laravel_framework_before_spec_is_run(Laravel $laravel,
25+
SpecificationEvent $event,
26+
SpecificationNode $spec,
27+
ReflectionClass $refl)
2328
{
29+
$event
30+
->getSpecification()
31+
->shouldBeCalled()
32+
->willReturn($spec);
33+
34+
$spec
35+
->getClassReflection()
36+
->shouldBeCalled()
37+
->willReturn($refl);
38+
39+
$refl
40+
->hasMethod('setLaravel')
41+
->shouldBeCalled()
42+
->willReturn(true);
43+
2444
$laravel->refreshApplication()->shouldBeCalled();
2545

2646
$this->beforeSpecification($event);

spec/PhpSpec/Laravel/Util/LaravelSpec.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,53 @@
99

1010
class LaravelSpec extends ObjectBehavior
1111
{
12-
function let(Application $app)
12+
private $appInst;
13+
14+
function let(Application $appInst)
1315
{
14-
$app->setRequestForConsoleEnvironment()->shouldBeCalled();
15-
$app->boot()->shouldBeCalled();
16+
$this->appInst = $appInst;
1617
}
1718

18-
function it_boots_in_the_testing_env_by_default(Application $app)
19+
function it_boots_in_the_testing_env_by_default()
1920
{
20-
$this->beConstructedWith(null, '.', false, $app);
21+
$this->beConstructedWith(null, '.', false);
2122
$this->getEnv()->shouldBe('testing');
2223
}
2324

24-
function it_allows_the_env_to_be_set_to_anything(Application $app)
25+
function it_allows_the_env_to_be_set_to_anything()
2526
{
26-
$this->beConstructedWith('whatever', '.', false, $app);
27+
$this->beConstructedWith('whatever', '.', false);
2728
$this->getEnv()->shouldBe('whatever');
2829
}
2930

30-
function it_will_run_migrations_if_told_to(Application $app, Console $console)
31+
function it_will_run_migrations_if_told_to(Console $console)
3132
{
3233
$console->call('migrate:install')->shouldBeCalled();
3334
$console->call('migrate:refresh')->shouldBeCalled();
34-
$app->make('artisan')->shouldBeCalled();
35-
$app->make('artisan')->willReturn($console);
3635

37-
$this->beConstructedWith(null, '.', true, $app);
36+
$this->appInst->setRequestForConsoleEnvironment()->shouldBeCalled();
37+
$this->appInst->boot()->shouldBeCalled();
38+
$this->appInst->make('artisan')->shouldBeCalled();
39+
$this->appInst->make('artisan')->willReturn($console);
40+
41+
$this->beConstructedWith(null, '.', true);
3842
$this->getMigrateDatabase()->shouldBe(true);
43+
$this->refreshApplication($this->appInst);
3944
}
4045

41-
function it_allows_access_to_the_app(Application $app)
46+
function it_allows_access_to_the_app()
4247
{
43-
$this->beConstructedWith(null, '.', false, $app);
48+
$this->appInst->setRequestForConsoleEnvironment()->shouldBeCalled();
49+
$this->appInst->boot()->shouldBeCalled();
50+
51+
$this->beConstructedWith(null, '.', false);
52+
$this->refreshApplication($this->appInst);
4453
$this->app->shouldHaveType('Illuminate\Foundation\Application');
4554
}
4655

47-
function it_throws_an_exception_when_trying_to_get_inaccessible_vars(Application $app)
56+
function it_throws_an_exception_when_trying_to_get_inaccessible_vars()
4857
{
49-
$this->beConstructedWith(null, '.', false, $app);
58+
$this->beConstructedWith(null, '.', false);
5059
$this->shouldThrow('ErrorException')->during('__get', array('inaccessible'));
5160
}
5261
}

src/PhpSpec/Laravel/Listener/LaravelListener.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/**
99
* This listener is used to setup the Laravel application for each spec.
10+
*
11+
* This only applies to specs that implement the LaravelBehaviorInterface.
1012
*/
1113
class LaravelListener implements EventSubscriberInterface {
1214

@@ -48,6 +50,10 @@ public static function getSubscribedEvents()
4850
*/
4951
public function beforeSpecification(SpecificationEvent $event)
5052
{
51-
$this->laravel->refreshApplication();
53+
$spec = $event->getSpecification();
54+
55+
if ($spec->getClassReflection()->hasMethod('setLaravel')) {
56+
$this->laravel->refreshApplication();
57+
}
5258
}
5359
}

src/PhpSpec/Laravel/Util/Laravel.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,13 @@ class Laravel {
4646
* @param string $bootstrapPath Path to the Laravel bootstrap dir
4747
* @param boolean $migrateDatabase Whether or not to run db migrations after
4848
* bootstrapping. False by default
49-
* @param mixed @see refreshApplication()
5049
* @return void
5150
*/
52-
public function __construct($env, $bootstrapPath, $migrateDatabase = false, $app = null)
51+
public function __construct($env, $bootstrapPath, $migrateDatabase = false)
5352
{
5453
$this->env = $env ?: 'testing';
5554
$this->bootstrapPath = $bootstrapPath;
5655
$this->migrateDatabase = $migrateDatabase;
57-
58-
$this->refreshApplication($app);
5956
}
6057

6158
/**

0 commit comments

Comments
 (0)