Skip to content

Commit

Permalink
Increase test coverage on cached graph Update Github Actions (#34)
Browse files Browse the repository at this point in the history
* Increase test coverage on cached graph

* Linting and update our silverstripe actions version
  • Loading branch information
chrispenny authored Nov 4, 2021
1 parent 759a936 commit e23e781
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ on:

jobs:
ci:
uses: silverstripe/github-actions-ci-cd/.github/workflows/[email protected].2
uses: silverstripe/github-actions-ci-cd/.github/workflows/[email protected].9
100 changes: 94 additions & 6 deletions tests/RelationshipGraph/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Terraformers\KeysForCache\Tests\RelationshipGraph;

use ReflectionClass;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\SiteConfig\SiteConfig;
Expand Down Expand Up @@ -237,33 +238,120 @@ public function testCacheSetOnFlush(): void
$reflectionClass = new ReflectionClass(Graph::class);
$edges = $reflectionClass->getProperty('edges');
$edges->setAccessible(true);
$cares = $reflectionClass->getProperty('global_cares');
$cares->setAccessible(true);
$globalCares = $reflectionClass->getProperty('global_cares');
$globalCares->setAccessible(true);

// Reset property values
$edges->setValue($graph, []);
$cares->setValue($graph, []);
$globalCares->setValue($graph, []);
// Clear any cache that would have been created during initial instantiation
$cache = Injector::inst()->get(Graph::CACHE_KEY);
$cache->clear();

// Check that we're set up with no cache and no property values
$this->assertEmpty($edges->getValue($graph));
$this->assertEmpty($cares->getValue($graph));
$this->assertEmpty($globalCares->getValue($graph));
$this->assertFalse($cache->has(Graph::CACHE_KEY_EDGES));
$this->assertFalse($cache->has(Graph::CACHE_KEY_GLOBAL_CARES));

// Trigger a flush, which should rebuild our cache and set the property values again
Graph::singleton()::flush();
$graph::flush();

// Check that our edges and global_cares properties have been filled during the flush
$this->assertNotEmpty($edges->getValue($graph));
$this->assertNotEmpty($cares->getValue($graph));
$this->assertNotEmpty($globalCares->getValue($graph));
// There should now also be cache values
$this->assertTrue($cache->has(Graph::CACHE_KEY_EDGES));
$this->assertTrue($cache->has(Graph::CACHE_KEY_GLOBAL_CARES));
}

public function testBuildFromCache(): void
{
// Instantiating the singleton will build
$graph = Graph::singleton();
// Using ReflectionClass so that we can reset and test that these properties are populated as we expect
$reflectionClass = new ReflectionClass(Graph::class);
$edges = $reflectionClass->getProperty('edges');
$edges->setAccessible(true);
$globalCares = $reflectionClass->getProperty('global_cares');
$globalCares->setAccessible(true);
$buildEdges = $reflectionClass->getMethod('buildEdges');
$buildEdges->setAccessible(true);
$buildGlobalCares = $reflectionClass->getMethod('buildGlobalCares');
$buildGlobalCares->setAccessible(true);

$edgesCount = count($edges->getValue($graph));
$globalCaresCount = count($globalCares->getValue($graph));

// Reset property values
$edges->setValue($graph, []);
$globalCares->setValue($graph, []);
// Fetch our cache
$cache = Injector::inst()->get(Graph::CACHE_KEY);

// Check that we're set up with a cache but with no property values
$this->assertEmpty($edges->getValue($graph));
$this->assertEmpty($globalCares->getValue($graph));
$this->assertTrue($cache->has(Graph::CACHE_KEY_EDGES));
$this->assertTrue($cache->has(Graph::CACHE_KEY_GLOBAL_CARES));

// Let's now change some of our configuration from what is in the cache. The next time we trigger a build
// (without a flush) we should still have our expected Graph (because our cache won't have updated)
// Below we have updated cares/touches to only have one relationship, and we have added a global_cares
CaresPage::config()
->set(
'cares',
[
'CaredHasOneModel',
]
)
->set(
'global_cares',
[
SiteTree::class,
]
);

$buildEdges->invoke($graph);
$buildGlobalCares->invoke($graph);

// Check that our global cares are still the same as our cache
$this->assertCount($globalCaresCount, $globalCares->getValue($graph));
// There should be no global_cares on SiteTree at the moment
$this->assertArrayNotHasKey(SiteTree::class, $globalCares->getValue($graph));

// Check that our edges are still the same as our cache
$this->assertCount($edgesCount, $edges->getValue($graph));
// CaredBelongsToModel was removed in our config change, but we expect it to be present from our cache rebuild
// There should be two Edges for this class
$this->assertCount(2, $graph->getEdgesFrom(CaredBelongsToModel::class));

// Now lets flush our cache and rebuild, we should end up with a different graph
$graph::flush();

// Check that our global cares have changed
$this->assertNotCount($globalCaresCount, $globalCares->getValue($graph));
$values = $globalCares->getValue($graph);
// CaresPage now has a global_cares for SiteTree, so it should now be present
$this->assertArrayHasKey(SiteTree::class, $values);
$this->assertEquals(
[
CaresPage::class,
ExtendedCaresPage::class,
],
$values[SiteTree::class],
'',
0.0,
10,
true
);

// Check that our Edges have changed
$this->assertNotCount($edgesCount, $edges->getValue($graph));
// CaredBelongsToModel should no longer be represented
$this->assertCount(0, $graph->getEdgesFrom(CaredBelongsToModel::class));
}

public function testGetValidClasses(): void
{
$graph = Graph::singleton();
Expand Down

0 comments on commit e23e781

Please sign in to comment.