Skip to content

Commit

Permalink
Add test case to cover "cares" relationship for NonStaged models #55 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSceneman authored Nov 30, 2022
1 parent 4a10c31 commit d4c1624
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tests/Mocks/Models/CaredHasOneVersionedNonStaged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Terraformers\KeysForCache\Tests\Mocks\Models;

use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\HasManyList;
use SilverStripe\Versioned\Versioned;
use Terraformers\KeysForCache\Extensions\CacheKeyExtension;
use Terraformers\KeysForCache\Tests\Mocks\Pages\CaresPage;

/**
* This model is referenced by CaresPage as a has_one, meaning that this model has a has_many back to CaresPage
*
* @property string $Title
* @method HasManyList|CaresPage CaresPages()
* @mixin Versioned
* @mixin CacheKeyExtension
*/
class CaredHasOneVersionedNonStaged extends DataObject implements TestOnly
{
private static array $db = [
'Title' => 'Varchar',
];

private static array $has_many = [
'CaresPages' => CaresPage::class,
];

private static array $extensions = [
Versioned::class.'.versioned',
];

private static string $table_name = 'CaredHasOneVersionedNonStaged';

private static bool $has_cache_key = false;
}
3 changes: 3 additions & 0 deletions tests/Mocks/Pages/CaresPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOne;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneNonVersioned;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneVersionedNonStaged;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredManyMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\PolymorphicCaredHasMany;
use Terraformers\KeysForCache\Tests\Mocks\Relations\CaredThrough;
Expand Down Expand Up @@ -40,6 +41,7 @@ class CaresPage extends Page implements TestOnly
'CaredBelongsTo' => CaredBelongsTo::class,
'CaredHasOne' => CaredHasOne::class,
'CaredHasOneNonVersioned' => CaredHasOneNonVersioned::class,
'CaredHasOneVersionedNonStaged' => CaredHasOneVersionedNonStaged::class,
'PolymorphicHasOne' => DataObject::class,
];

Expand All @@ -62,6 +64,7 @@ class CaresPage extends Page implements TestOnly
'CaredBelongsTo',
'CaredHasOne',
'CaredHasOneNonVersioned',
'CaredHasOneVersionedNonStaged',
'CaredHasMany',
'CaredManyMany',
'CaredThrough',
Expand Down
2 changes: 2 additions & 0 deletions tests/RelationshipGraph/GraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOne;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneNonVersioned;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneVersionedNonStaged;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredManyMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\PolymorphicCaredHasMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\PolymorphicTouchedHasMany;
Expand Down Expand Up @@ -169,6 +170,7 @@ public function testGetRelationshipConfig(): void
'CaredBelongsTo' => CaredBelongsTo::class,
'CaredHasOne' => CaredHasOne::class,
'CaredHasOneNonVersioned' => CaredHasOneNonVersioned::class,
'CaredHasOneVersionedNonStaged' => CaredHasOneVersionedNonStaged::class,
'CaredHasMany' => CaredHasMany::class,
'CaredManyMany' => CaredManyMany::class,
'CaredThrough' => [
Expand Down
30 changes: 30 additions & 0 deletions tests/Scenarios/CaresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOne;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneNonVersioned;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneVersionedNonStaged;
use Terraformers\KeysForCache\Tests\Mocks\Models\CaredManyMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\PolymorphicCaredHasMany;
use Terraformers\KeysForCache\Tests\Mocks\Models\PolymorphicCaredHasOne;
Expand All @@ -32,6 +33,7 @@ class CaresTest extends SapphireTest
CaredHasMany::class,
CaredHasOne::class,
CaredHasOneNonVersioned::class,
CaredHasOneVersionedNonStaged::class,
CaredManyMany::class,
CaredThrough::class,
PolymorphicCaredHasOne::class,
Expand Down Expand Up @@ -150,6 +152,34 @@ public function testCaresHasOneNonVersioned(): void
$this->assertNotEquals($originalKey, $newKey);
}

public function testCaresHasOneVersionedNonStaged(): void
{
// Updates are processed as part of scaffold, so we need to flush before we kick off
ProcessedUpdatesService::singleton()->flush();

$page = $this->objFromFixture(CaresPage::class, 'page1');
$model = $this->objFromFixture(CaredHasOneVersionedNonStaged::class, 'model1');

// Check that we're set up correctly
$this->assertEquals(CaredHasOneVersionedNonStaged::class, $model->ClassName);
$this->assertEquals($page->CaredHasOneVersionedNonStagedID, $model->ID);

$originalKey = $page->getCacheKey();

$this->assertNotNull($originalKey);
$this->assertNotEmpty($originalKey);

// Begin changes
$model->forceChange();
$model->write();

$newKey = $page->getCacheKey();

$this->assertNotNull($newKey);
$this->assertNotEmpty($newKey);
$this->assertNotEquals($originalKey, $newKey);
}

public function testPolymorphicCaresHasOne(): void
{
// Updates are processed as part of scaffold, so we need to flush before we kick off
Expand Down
5 changes: 5 additions & 0 deletions tests/Scenarios/CaresTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneNonVersioned:
model1:
Title: Has One Non Versioned Model 1

Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneVersionedNonStaged:
model1:
Title: Has One Versioned Non Staged Model 1

Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasMany:
model1:
Title: Has Many Model 1
Expand All @@ -36,6 +40,7 @@ Terraformers\KeysForCache\Tests\Mocks\Pages\CaresPage:
CaredBelongsTo: =>Terraformers\KeysForCache\Tests\Mocks\Models\CaredBelongsTo.model1
CaredHasOne: =>Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOne.model1
CaredHasOneNonVersioned: =>Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneNonVersioned.model1
CaredHasOneVersionedNonStaged: =>Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasOneVersionedNonStaged.model1
CaredHasMany:
- =>Terraformers\KeysForCache\Tests\Mocks\Models\CaredHasMany.model1
CaredManyMany:
Expand Down

0 comments on commit d4c1624

Please sign in to comment.