What is the correct syntax to post 'AccountApiKey[merchant_id]' in the below example for testing insertion in Laravel 5.4.
In production, the code works fine without any issues, in tests I get the error "table not found" where it is not inserting correctly. I've asked in IRC but it stumped them so I'm hoping the wonderful community of stackoverflow can assist.
Thanks!
View:
{{-- Merchant ID form input --}}
<div class="form-group">
{!! Form::label('AccountApiKey[merchant_id]', 'Seller ID:') !!}
{!! Form::text('AccountApiKey[merchant_id]', null, ['class' => 'form-control']) !!}
</div>
Test (See below update for complete test):
$accountApiKey = factory(AccountApiKey::class)->make([
'merchant_id' => 'test'
]);
$this->post($this->urlToUse(), [
'name' => $account->name,
'AccountApiKey[merchant_id]' => $accountApiKey->merchant_id,
]);
$this->assertDatabaseHas('account_api_keys', [
'merchant_id' => $accountApiKey->merchant_id,
]);
Controller:
$account->accountApiKey()->save(new AccountApiKey($request->get('AccountApiKey')));
Update as per Sandeesh comment:
Model:
class AccountApiKey extends Model implements Transformable
{
use TransformableTrait;
protected $fillable = ['last_modified_user_id', 'merchant_id'];
protected $events = ['saving' => SettingsUpdated::class];
public function account()
{
return $this->belongsTo('App\Models\Account\Settings\Accounts');
}
}
Complete Test:
class StoreTest extends TestCase implements TestInterface
{
use DatabaseMigrations;
/**
* Tests all forms are inserting correct into database
*/
public function test_inserting_into_database()
{
$user1 = $this->userAndCompany();
$this->actingAs($user1);
$account = factory(Account::class)->create([
'company_id' => $user1->company()->first()->id,
]);
$accountApiKey = factory(Account\AccountApiKey::class)->make([
'last_modified_user' => $user1->id,
'account_id' => $account->id,
'merchant_id' => 'test'
]);
$this->post($this->urlToUse(), [
'name' => $account->name,
'AccountApiKey[merchant_id]' => $accountApiKey->merchant_id,
]);
$this->assertDatabaseHas('accounts', [
'name' => $account->name, //asserts true
]);
$this->assertDatabaseHas('account_api_keys', [
'merchant_id' => $accountApiKey->merchant_id, // asserts false
]);
}
/**
* The url which is under test
* @return mixed
*/
function urlToUse()
{
return 'account/settings/account';
}
}
php artisan migrate --database=testingto be sure