Skip to content

Commit

Permalink
[8.x] Add implicit binding test (#35443)
Browse files Browse the repository at this point in the history
* Add implicit binding test

* Add middleware

* Allow testbench to cache routes.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Fixes CS.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Small cleanup.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Rename test

* Rename class

Co-authored-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
driesvints and crynobone authored Dec 4, 2020
1 parent a8f7118 commit aa1b323
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
95 changes: 95 additions & 0 deletions tests/Integration/Routing/ImplicitRouteBindingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Illuminate\Tests\Integration\Routing;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Concerns\InteractsWithPublishedFiles;
use Orchestra\Testbench\TestCase;

class ImplicitRouteBindingTest extends TestCase
{
use InteractsWithPublishedFiles;

protected $files = [
'routes/testbench.php',
];

/**
* Teardown the test environment.
*/
protected function tearDown(): void
{
$this->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 = <<<PHP
<?php
use Illuminate\Tests\Integration\Routing\ImplicitBindingModel;
Route::post('/user/{user}', function (ImplicitBindingModel \$user) {
return \$user;
})->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'];
}

0 comments on commit aa1b323

Please sign in to comment.