This free course uses older versions of Laravel. It is provided as-is, without ongoing updates or individual support.
In this lesson we diagnose why are test is passing after commenting out our implementation, and come up with a new strategy to run our tests with isolated data.
Two small corrections were made to the code in the video before committing:
array_firstIn the lastCharge method, we were trying to access the element at offset 0 to get the most recent charge:
private function lastCharge()
{
return \Stripe\Charge::all(
['limit' => 1],
['api_key' => config('services.stripe.secret')]
)[data][0];
}
This will throw an error if you don't happen to have any charges created yet.
Instead, use array_first to get the first charge or null if no charges are present:
private function lastCharge()
{
return array_first(\Stripe\Charge::all(
['limit' => 1],
['api_key' => config('services.stripe.secret')]
)['data']);
}
The newCharges method was also updated to check for null before trying to get the id of the last charge:
private function newCharges()
{
return \Stripe\Charge::all(
[
'limit' => 1,
'ending_before' => $this->lastCharge ? $this->lastCharge->id : null,
],
['api_key' => config('services.stripe.secret')]
)['data'];
}
When copying over the newCharges implementation, we mistakenly left in the limit parameter:
private function newCharges()
{
return \Stripe\Charge::all(
[
'limit' => 1,
'ending_before' => $this->lastCharge ? $this->lastCharge->id : null,
],
['api_key' => config('services.stripe.secret')]
)['data'];
}
This was removed before committing to make sure we always get all the charges back that were created in the test, and not just the most recent one:
private function newCharges()
{
return \Stripe\Charge::all(
[
'ending_before' => $this->lastCharge ? $this->lastCharge->id : null,
],
['api_key' => config('services.stripe.secret')]
)['data'];
}