I'm building a multitenancy api with Laravel based on Account Model, i determine the tennant in use by the request reader "Account" by using a trait (AccountScoped) on all tenant models, everything is working on postman, but whyle testing the header is not acessible in trait.
AccountScopped (Trait)
trait AccountScoped
{
protected static function bootAccountScoped()
{
$account_id = request()->header('Account');
dd($account_id);
if (empty($account_id)) {
return;
}
static::creating(function (Model $model) use ($account_id) {
$model->account_id = $account_id;
});
static::addGlobalScope('byAccount', function (Builder $builder) use ($account_id) {
$builder->where('account_id', '=', $account_id);
});
}
}
The test method:
public function testActionIndexInController()
{
$categories = Category::where('account_id',$this->account->id)->get()->toJSON();
$response = $this->actingAs($this->user, 'api')
->withHeader('Account',$this->account->id)
->get(route('categories.index'));
$response->assertStatus(200)->assertSee($categories);
}
* $this->user is a User instance model with Accounts, $this->account is a Account instance related to the selected user
But when i run the test i get null value on $account_id Test return
And when i make the same request by postaman the value is there Postman Return
Aucun commentaire:
Enregistrer un commentaire