From 91b4b4111ee0ade89175e9569b4d4fc0546ab6e9 Mon Sep 17 00:00:00 2001 From: crynobone Date: Fri, 4 Mar 2016 09:28:03 +0800 Subject: [PATCH 1/3] Backport fluent named route bugfix. Signed-off-by: crynobone --- src/Traits/ApplicationTrait.php | 2 + tests/RouteTest.php | 76 +++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/Traits/ApplicationTrait.php b/src/Traits/ApplicationTrait.php index b04d9df..b724b12 100644 --- a/src/Traits/ApplicationTrait.php +++ b/src/Traits/ApplicationTrait.php @@ -102,6 +102,8 @@ public function createApplication() $app->make('Illuminate\Foundation\Bootstrap\BootProviders')->bootstrap($app); + $app['router']->getRoutes()->refreshNameLookups(); + return $app; } diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 1b8dba3..518f3ec 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -1,5 +1,7 @@ get('hello', function () { + $app['router']->get('hello', ['as' => 'hi', 'uses' => function () { return 'hello world'; + }]); + + $app['router']->get('goodbye', function () { + return 'goodbye world'; + })->name('bye'); + + $app['router']->group(['prefix' => 'boss'], function (Router $router) { + $router->get('hello', ['as' => 'boss.hi', 'uses' => function () { + return 'hello boss'; + }]); + + $router->get('goodbye', function () { + return 'goodbye boss'; + })->name('boss.bye'); }); $app['router']->resource('foo', 'Orchestra\Testbench\TestCase\FooController'); } /** - * Test GET hello route. + * Test GET routes. * * @test */ - public function testGetHelloRoute() + public function testGetRoutes() { $crawler = $this->call('GET', 'hello'); - //$this->assertResponseOk(); $this->assertEquals('hello world', $crawler->getContent()); + + $crawler = $this->call('GET', 'goodbye'); + + $this->assertEquals('goodbye world', $crawler->getContent()); + + } + + /** + * Test GET routes via named route. + * + * @test + */ + public function testGetRoutesViaNamedRoute() + { + $crawler = $this->route('GET', 'hi'); + + $this->assertEquals('hello world', $crawler->getContent()); + + $crawler = $this->route('GET', 'bye'); + + $this->assertEquals('goodbye world', $crawler->getContent()); + } + + /** + * Test GET routes with prefix. + * + * @test + */ + public function testGetPrefixedRoutes() + { + $crawler = $this->call('GET', 'boss/hello'); + + $this->assertEquals('hello boss', $crawler->getContent()); + + $crawler = $this->route('GET', 'boss.bye'); + + $this->assertEquals('goodbye boss', $crawler->getContent()); + } + + /** + * Test GET routes with prefix via named route. + * + * @test + */ + public function testGetPrefixedRoutesViaNameRoute() + { + $crawler = $this->route('GET', 'boss.hi'); + + $this->assertEquals('hello boss', $crawler->getContent()); + + $crawler = $this->call('GET', 'boss/goodbye'); + + $this->assertEquals('goodbye boss', $crawler->getContent()); } /** From c7a8a4aeac2752449dbc756b4779097ef801cfc0 Mon Sep 17 00:00:00 2001 From: Loren Klingman Date: Tue, 16 Aug 2016 12:05:10 -0400 Subject: [PATCH 2/3] Create a load migrations from function to do database migrations --- src/TestCase.php | 17 ++++++ tests/DatabaseLoadMigrationTest.php | 94 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 tests/DatabaseLoadMigrationTest.php diff --git a/src/TestCase.php b/src/TestCase.php index 0518e09..6fad2bc 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -73,6 +73,23 @@ public function tearDown() } } + /** + * Define hooks to migrate the database before and after each test. + * + * @param string $realpah + * @return void + */ + public function loadMigrationsFrom($realpath) + { + $this->artisan('migrate', [ + '--realpath' => $realpath, + ]); + + $this->beforeApplicationDestroyed(function () { + $this->artisan('migrate:rollback'); + }); + } + /** * Register a callback to be run before the application is destroyed. * diff --git a/tests/DatabaseLoadMigrationTest.php b/tests/DatabaseLoadMigrationTest.php new file mode 100644 index 0000000..fb54163 --- /dev/null +++ b/tests/DatabaseLoadMigrationTest.php @@ -0,0 +1,94 @@ +app['router']->enableFilters(); + + // call migrations for packages upon which our package depends, e.g. Cartalyst/Sentry + // not necessary if your package doesn't depend on another package that requires + // running migrations for proper installation + /* uncomment as necessary + $this->artisan('migrate', [ + '--database' => 'testbench', + '--path' => '../vendor/cartalyst/sentry/src/migrations', + ]); + */ + $this->loadMigrationsFrom(realpath(__DIR__.'/migrations')); + } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * + * @return void + */ + protected function getEnvironmentSetUp($app) + { + $app['config']->set('database.default', 'testing'); + } + + /** + * Get package providers. At a minimum this is the package being tested, but also + * would include packages upon which our package depends, e.g. Cartalyst/Sentry + * In a normal app environment these would be added to the 'providers' array in + * the config/app.php file. + * + * @param \Illuminate\Foundation\Application $app + * + * @return array + */ + protected function getPackageProviders($app) + { + return [ + //'Cartalyst\Sentry\SentryServiceProvider', + //'YourProject\YourPackage\YourPackageServiceProvider', + ]; + } + + /** + * Get package aliases. In a normal app environment these would be added to + * the 'aliases' array in the config/app.php file. If your package exposes an + * aliased facade, you should add the alias here, along with aliases for + * facades upon which your package depends, e.g. Cartalyst/Sentry. + * + * @param \Illuminate\Foundation\Application $app + * + * @return array + */ + protected function getPackageAliases($app) + { + return [ + //'Sentry' => 'Cartalyst\Sentry\Facades\Laravel\Sentry', + //'YourPackage' => 'YourProject\YourPackage\Facades\YourPackage', + ]; + } + + /** + * Test running migration. + * + * @test + */ + public function testRunningMigration() + { + $users = \DB::table('users')->where('id', '=', 1)->first(); + + $this->assertEquals('hello@orchestraplatform.com', $users->email); + $this->assertTrue(\Hash::check('123', $users->password)); + + $this->beforeApplicationDestroyed(function () { + $this->assertSame(\Schema::hasTable('users'), false); + }); + } +} From ca41540d3f20836f13ca496be56b19546c844924 Mon Sep 17 00:00:00 2001 From: crynobone Date: Wed, 17 Aug 2016 06:34:21 +0800 Subject: [PATCH 3/3] Change method visibility and update CS. Signed-off-by: crynobone --- src/TestCase.php | 3 ++- tests/BehatFeatureContextTest.php | 2 +- tests/DatabaseLoadMigrationTest.php | 3 +-- tests/Integrations/AggregateServiceProviderTest.php | 3 +-- tests/RouteTest.php | 1 - tests/TestCaseTest.php | 2 +- 6 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/TestCase.php b/src/TestCase.php index 6fad2bc..0b69da1 100644 --- a/src/TestCase.php +++ b/src/TestCase.php @@ -77,9 +77,10 @@ public function tearDown() * Define hooks to migrate the database before and after each test. * * @param string $realpah + * * @return void */ - public function loadMigrationsFrom($realpath) + protected function loadMigrationsFrom($realpath) { $this->artisan('migrate', [ '--realpath' => $realpath, diff --git a/tests/BehatFeatureContextTest.php b/tests/BehatFeatureContextTest.php index 2f05f83..edade55 100644 --- a/tests/BehatFeatureContextTest.php +++ b/tests/BehatFeatureContextTest.php @@ -12,7 +12,7 @@ class BehatFeatureContextTest extends \PHPUnit_Framework_TestCase public function testCreateApplicationMethod() { $stub = new StubFeatureContext([]); - $app = $stub->createApplication(); + $app = $stub->createApplication(); $this->assertInstanceOf('\Orchestra\Testbench\TestCaseInterface', $stub); $this->assertInstanceOf('\Illuminate\Foundation\Application', $app); diff --git a/tests/DatabaseLoadMigrationTest.php b/tests/DatabaseLoadMigrationTest.php index fb54163..506e2f7 100644 --- a/tests/DatabaseLoadMigrationTest.php +++ b/tests/DatabaseLoadMigrationTest.php @@ -4,7 +4,6 @@ class DatabaseLoadMigrationsTest extends \Orchestra\Testbench\TestCase { - /** * Setup the test environment. */ @@ -86,7 +85,7 @@ public function testRunningMigration() $this->assertEquals('hello@orchestraplatform.com', $users->email); $this->assertTrue(\Hash::check('123', $users->password)); - + $this->beforeApplicationDestroyed(function () { $this->assertSame(\Schema::hasTable('users'), false); }); diff --git a/tests/Integrations/AggregateServiceProviderTest.php b/tests/Integrations/AggregateServiceProviderTest.php index b828ef1..bd122dc 100644 --- a/tests/Integrations/AggregateServiceProviderTest.php +++ b/tests/Integrations/AggregateServiceProviderTest.php @@ -37,7 +37,6 @@ public function testServiceIsAvailable() } } - class ParentService extends AggregateServiceProvider { protected $providers = [ @@ -69,4 +68,4 @@ public function register() { $this->app['child.deferred.loaded'] = true; } -} \ No newline at end of file +} diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 518f3ec..4646fa2 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -48,7 +48,6 @@ public function testGetRoutes() $crawler = $this->call('GET', 'goodbye'); $this->assertEquals('goodbye world', $crawler->getContent()); - } /** diff --git a/tests/TestCaseTest.php b/tests/TestCaseTest.php index 2882759..6ea9d42 100644 --- a/tests/TestCaseTest.php +++ b/tests/TestCaseTest.php @@ -10,7 +10,7 @@ class TestCaseTest extends \PHPUnit_Framework_TestCase public function testCreateApplicationMethod() { $stub = new StubTestCase(); - $app = $stub->createApplication(); + $app = $stub->createApplication(); $this->assertInstanceOf('\Orchestra\Testbench\TestCaseInterface', $stub); $this->assertInstanceOf('\Illuminate\Foundation\Application', $app);