-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Add implicit binding test (#35443)
* 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
1 parent
a8f7118
commit aa1b323
Showing
2 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} |