Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the HTTP facade instead of manually creating a new request #76

Merged

Conversation

macbookandrew
Copy link
Contributor

This allows consuming applications to use Http::fake() to mock the FileMaker Data API.

Prior to 2.2.1, I was able to do something like this in applications to mock the DAPI for my app tests:

/** @param  array<string, string|\Illuminate\Http\Client\ResponseSequence|\GuzzleHttp\Promise\PromiseInterface>  $requests */
protected function mockFileMakerDataApi(array $requests = []): void
{
    $database = config('database.connections.filemaker.database');

    $sessionsUrl = sprintf(
        'https://%s/fmi/data/%s/databases/%s/sessions',
        config('database.connections.filemaker.host'),
        config('database.connections.filemaker.version'),
        $database,
    );

    Http::fake(array_merge([
        $sessionsUrl => Http::response(['response' => ['token' => 'fake-token']], 200),
    ], $requests));
}

$this->mockFileMakerDataApi([
    'https://server/fmi/data/databases/database/layouts/test/*' => Http::response([/** fake data */]),
]);

Since 7b94af3 specifically, I now get an error:

  Layout: test - Authorization header missing 'Basic' or 'Bearer'.

  at vendor/gearbox-solutions/eloquent-filemaker/src/Services/FileMakerConnection.php:176
    172▕                         $customMessage = 'Layout: '.$this->getLayout().' - '.$message['message'];
    173▕                     } else {
    174▕                         $customMessage = $message['message'];
    175▕                     }
  ➜ 176▕                     throw new FileMakerDataApiException($customMessage, $code);
    177▕                 }
    178▕             }
    179▕         } else {
    180▕             $response->throw();

      +11 vendor frames 

Because the new PendingRequest() does not know about the already-faked responses it should return, it tries to run real requests.


Solution: upgrade Laravel to ^11.3.0 to take advantage of the new Http::createPendingRequest() 😆

Alternate solution for older apps: use Http::acceptJson() to create the pending request while retaining knowledge of faked requests.

This allows consuming applications to use Http::fake() to mock the FileMaker Data API.
@macbookandrew
Copy link
Contributor Author

@Smef do you want me to fix the code style issues or leave them so this PR is specific to the issue?

@Smef Smef self-requested a review July 29, 2024 17:44
@Smef Smef merged commit 472e491 into gearbox-solutions:2.x Jul 29, 2024
1 check failed
@Smef
Copy link
Member

Smef commented Jul 29, 2024

I'll fix that formatting. Thanks!

@macbookandrew macbookandrew deleted the bugfix/allow-http-fake-in-apps branch July 29, 2024 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants