From aa1b323e16193f116fb8884f7645e2608830d10f Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 4 Dec 2020 15:26:11 +0100 Subject: [PATCH] [8.x] Add implicit binding test (#35443) * Add implicit binding test * Add middleware * Allow testbench to cache routes. Signed-off-by: Mior Muhammad Zaki * Fixes CS. Signed-off-by: Mior Muhammad Zaki * Small cleanup. Signed-off-by: Mior Muhammad Zaki * Rename test * Rename class Co-authored-by: Mior Muhammad Zaki --- composer.json | 2 +- .../Routing/ImplicitRouteBindingTest.php | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Integration/Routing/ImplicitRouteBindingTest.php diff --git a/composer.json b/composer.json index a63c064c721a..5665326e7c61 100644 --- a/composer.json +++ b/composer.json @@ -84,7 +84,7 @@ "guzzlehttp/guzzle": "^6.5.5|^7.0.1", "league/flysystem-cached-adapter": "^1.0", "mockery/mockery": "^1.4.2", - "orchestra/testbench-core": "^6.5", + "orchestra/testbench-core": "^6.8", "pda/pheanstalk": "^4.0", "phpunit/phpunit": "^8.5.8|^9.3.3", "predis/predis": "^1.1.1", diff --git a/tests/Integration/Routing/ImplicitRouteBindingTest.php b/tests/Integration/Routing/ImplicitRouteBindingTest.php new file mode 100644 index 000000000000..a0130dfd0eda --- /dev/null +++ b/tests/Integration/Routing/ImplicitRouteBindingTest.php @@ -0,0 +1,95 @@ +tearDownInteractsWithPublishedFiles(); + + parent::tearDown(); + } + + protected function defineEnvironment($app) + { + $app['config']->set('app.debug', 'true'); + + $app['config']->set('database.default', 'testbench'); + + $app['config']->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + } + + protected function defineDatabaseMigrations(): void + { + Schema::create('users', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->timestamps(); + }); + + $this->beforeApplicationDestroyed(function () { + Schema::dropIfExists('users'); + }); + } + + public function testWithRouteCachingEnabled() + { + $route = <<middleware('web'); +PHP; + + file_put_contents(base_path('routes/testbench.php'), $route); + + $this->artisan('route:cache')->run(); + + $this->reloadApplication(); + + $this->assertFilenameExists('bootstrap/cache/routes-v7.php'); + + $this->requireApplicationCachedRoutes(); + + $user = ImplicitBindingModel::create(['name' => 'Dries']); + + $response = $this->postJson("/user/{$user->id}"); + + $response->assertJson([ + 'id' => $user->id, + 'name' => $user->name, + ]); + + $this->artisan('route:clear')->run(); + } +} + +class ImplicitBindingModel extends Model +{ + public $table = 'users'; + + protected $fillable = ['name']; +}