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

Add developer documentation regarding unit tests with KFC #72

Closed
chrispenny opened this issue May 22, 2023 · 0 comments · Fixed by #76
Closed

Add developer documentation regarding unit tests with KFC #72

chrispenny opened this issue May 22, 2023 · 0 comments · Fixed by #76

Comments

@chrispenny
Copy link
Member

chrispenny commented May 22, 2023

  • KFC includes a feature to make sure that it only updates the cache for any unique record once per request.
  • An entire unit test case is considered "one request".
  • A developer might want to create tests that check their cache key updates when they trigger a change on that record.

Below is an example of a reasonable test that a developer might want to undertake. You would expect that the LastEdited and getCacheKey() values update with each var_dump().

class SiteConfigTest extends SapphireTest
{

    protected $usesDatabase = true;

    public function testSiteConfigCacheKey(): void
    {
        $siteConfig = SiteConfig::current_site_config();
        var_dump($siteConfig->LastEdited);
        var_dump($siteConfig->getCacheKey());

        // Sleep for a second to make sure our date changes
        sleep(1);

        // Make a change to SiteConfig
        $siteConfig->Title = 'Updated Site Title 1';
        $siteConfig->write();

        var_dump($siteConfig->LastEdited);
        var_dump($siteConfig->getCacheKey());

        // Sleep for a second to make sure our date changes
        sleep(1);

        // Make a change to SiteConfig
        $siteConfig->Title = 'Updated Site Title 2';
        $siteConfig->write();

        var_dump($siteConfig->LastEdited);
        var_dump($siteConfig->getCacheKey());
    }

}

Actual: LastEdited does update, but getCacheKey() does not.

In order to get the expected outcome, developers need to add ProcessedUpdatesService::singleton()->flush(); before they write() their record. This clears the cache for KFC, and will mean that any records saved will now have their cache key updated (even if they have done so previously in this request).

EG:

class SiteConfigTest extends SapphireTest
{

    protected $usesDatabase = true;

    public function testSiteConfigCacheKey(): void
    {
        $siteConfig = SiteConfig::current_site_config();
        var_dump($siteConfig->LastEdited);
        var_dump($siteConfig->getCacheKey());

        // Sleep for a second to make sure our date changes
        sleep(1);

        // Flush updates from KFC so that new writes invalidate cache keys
        ProcessedUpdatesService::singleton()->flush();

        // Make a change to SiteConfig
        $siteConfig->Title = 'Updated Site Title 1';
        $siteConfig->write();

        var_dump($siteConfig->LastEdited);
        var_dump($siteConfig->getCacheKey());

        // Sleep for a second to make sure our date changes
        sleep(1);

        // Flush updates from KFC so that new writes invalidate cache keys
        ProcessedUpdatesService::singleton()->flush();

        // Make a change to SiteConfig
        $siteConfig->Title = 'Updated Site Title 2';
        $siteConfig->write();

        var_dump($siteConfig->LastEdited);
        var_dump($siteConfig->getCacheKey());
    }

}

Reconsider how we cache updated records?

It's possible as well that we should reconsider how we cache what records have been updated, so that devs don't even ever have this issue.

Off the top of my head, perhaps we could use onAfter hooks in our Extension to perform a flush() on ProcessedUpdatesService

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 a pull request may close this issue.

1 participant