I am pulling order information from an external API and saving the entire JSON response into a documents
table.
I obviously do not want to call this API when testing so I have setup a factory, but I am struggling with how to pull in these JSON stubs. For example:
// factory
$factory->define(App\OrderDocument::class, function (Faker $faker) {
return [
'document_id' => $faker->uuid,
'status' => $faker->randomElement(['open', 'partial', 'processed', 'updating']),
'document' => $faker->text
];
});
// Currently using it like...
foreach ($this->fullDocuments() as $id => $document){
$a = factory(\App\OrderDocument::class)->create([
'document_id' => $id,
'status' => 'full'
]);
$a->setRawAttributes(['document' => $document]);
$a->save();
}
where $this->fullDocuments()
contains an array of the document_id and raw JSON response from the API.
I know a better way to do this is to factory the entire JSON document. The JSON contains about 500 lines so it would be very time consuming but also I do not own this data, so I assume I should not be trying to fake it.
Within my test, I would prefer to do something like the below, but am not sure how.
factory(OrderDocument::class, 10)->create([
'document_id' => $this->getDocumentId($i++),
'document' => $this->getDocumentStub($i++),
]);