Skip to content

Commit

Permalink
Merge pull request #8 from creative-commoners/pulls/1.0/disable-snaps…
Browse files Browse the repository at this point in the history
…hots-in-tests

NEW Allow pausing snapshots globally
  • Loading branch information
Aaron Carlino committed Jul 15, 2019
2 parents 5c56aa8 + bb37c1a commit dde4965
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
9 changes: 9 additions & 0 deletions _config/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
Name: snapshotssapphiretest
Before: '#sapphiretest'
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Dev\State\SapphireTestState:
properties:
States:
disablesnapshots: '%$SilverStripe\Snapshots\Dev\State\DisableSnapshots'
63 changes: 63 additions & 0 deletions src/Dev/State/DisableSnapshots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace SilverStripe\Snapshots\Dev\State;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\State\TestState;
use SilverStripe\Snapshots\SnapshotPublishable;

/**
* Disable snapshots in tests by default. Snapshots will analyze a relationship tree for objects when they are saved but
* fixtures will not necessarily scaffold all required tables for this when the test state is scaffolded.
*
* Tests that rely on snapshot functionality should explicitly opt-in to snapshots by calling
* `SnapshotPublishable::resume`.
*/
class DisableSnapshots implements TestState
{

/**
* Called on setup
*
* @param SapphireTest $test
*/
public function setUp(SapphireTest $test)
{
// Skip tests in this modules' namespace
if (strpos(get_class($test), 'SilverStripe\\Snapshots\\') === 0) {
return;
}

SnapshotPublishable::pause();
}

/**
* Called on tear down
*
* @param SapphireTest $test
*/
public function tearDown(SapphireTest $test)
{
SnapshotPublishable::resume();
}

/**
* Called once on setup
*
* @param string $class Class being setup
*/
public function setUpOnce($class)
{
// noop
}

/**
* Called once on tear down
*
* @param string $class Class being torn down
*/
public function tearDownOnce($class)
{
// noop
}
}
37 changes: 37 additions & 0 deletions src/SnapshotPublishable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class SnapshotPublishable extends RecursivePublishable
*/
protected $activeSnapshot = null;

/**
* Indicates if snapshotting is currently active. Control this state with the ::pause and ::resume methods
*
* @var bool
*/
protected static $active = true;

/**
* @param $class
* @param $id
Expand Down Expand Up @@ -63,6 +70,10 @@ public static function get_at_snapshot($class, $id, $snapshot)
*/
public function publishRecursive()
{
if (!self::$active) {
return parent::publishRecursive();
}

$this->openSnapshot();
$result = parent::publishRecursive();
$this->closeSnapshot();
Expand All @@ -72,6 +83,10 @@ public function publishRecursive()

public function rollbackRelations($version)
{
if (!self::$active) {
return parent::rollbackRelations($version);
}

$this->openSnapshot();
parent::rollbackRelations($version);
$this->closeSnapshot();
Expand Down Expand Up @@ -537,6 +552,23 @@ public function rollbackOwned($snapshot)
}
}

/**
* Pause snapshotting - disabling tracking version changes across a versioned owner relationship tree. This should
* only be done in cases where you are manually creating a snapshot (or you are _really_ sure that you don't want a
* change to be tracked own "owner"s of a record.
*/
public static function pause()
{
self::$active = false;
}

/**
* Resume snapshotting after previously calling `SnapshotPublishable::pause`.
*/
public static function resume()
{
self::$active = true;
}

/**
* @param array $snapShotIDs
Expand Down Expand Up @@ -573,6 +605,11 @@ protected function publishableItemsQuery($snapShotIDs)
*/
protected function requiresSnapshot()
{
// Check if snapshotting is currently "paused"
if (!self::$active) {
return false;
}

$owner = $this->owner;

// Explicitly blacklist these two, since they get so many writes in this context,
Expand Down

0 comments on commit dde4965

Please sign in to comment.