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

Port cache assertions #14

Open
jwadhams opened this issue Aug 19, 2024 · 1 comment
Open

Port cache assertions #14

jwadhams opened this issue Aug 19, 2024 · 1 comment

Comments

@jwadhams
Copy link
Collaborator

Our private assertion helpers stopped working after the 1.1.1 upgrade, because they didn't track the new shape of the cached body.

If they were part of the shared library and part of its test coverage, everyone could benefit from them, and they'd upgrade when the library does, win-win.

@jwadhams
Copy link
Collaborator Author

jwadhams commented Aug 19, 2024

Working post-1.2.0 implementations

    /**
     * Create a fake cache entry for a given request.
     */
    protected static function mockRequestCachedResponse(
        AbstractRequest $request,
        string $body,
        int $status = 200,
        array $headers = [],
        array $logs = [],
    ) {
        $tags = getProperty($request, 'cacheTags');
        Cache::tags($tags)->put($request->cacheKey(), [
            'logs' => $logs,
            'response' => [$status, $headers, $body],
        ]);
    }

    /**
     * Fetch the cached response to a request and assert that it contains a substring
     */
    protected static function requestCacheBodyContains(
        string $substring,
        AbstractRequest $request,
        string $message = '',
    ): void {
        $cached = callMethod($request, 'responseFromCache');
        self::assertNotNull($cached, 'Cache should not be empty for request.');
        self::assertStringContainsString($substring, (string) $cached->getBody(), $message);
    }

Note these both rely on our mockery helper methods, but those are already present in tests/bootstrap.php so no biggie

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

No branches or pull requests

1 participant