Skip to content

Commit

Permalink
Support for Silverstripe 5 (#51)
Browse files Browse the repository at this point in the history
* Updated minimum composer requirements
* PHP8: Update method signatures. Fix unit tests

---------

Co-authored-by: Chris Penny <chris.penny@gmail.com>
  • Loading branch information
catharsisjelly and chrispenny committed Jul 2, 2023
1 parent 7797c69 commit af2a44c
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 106 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,3 @@ jobs:
with:
# Turn phpcoverage off because it causes a segfault
phpcoverage_force_off: true
dynamic_matrix: false
extra_jobs: |
- php: 7.4
phplinting: true
- php: 7.4
phpunit: true
- php: 8.0
phpunit: true
- php: 8.1
db: mysql57pdo
phpunit: true
- php: 8.1
db: mysql80
phpunit: true
- php: 8.1
phpunit: true
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ objects can be defined in YAML and shared around developers. This extends the

## Requirements

* PHP 7.1
* SilverStripe [Framework ^4](https://github.com/silverstripe/silverstripe-framework)
* SilverStripe [Versioned ^1](https://github.com/silverstripe/silverstripe-versioned)
* PHP 8.1
* SilverStripe [Framework ^5](https://github.com/silverstripe/silverstripe-framework)
* SilverStripe [Versioned ^2](https://github.com/silverstripe/silverstripe-versioned)

## Installation Instructions

Expand Down
38 changes: 7 additions & 31 deletions code/Populate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,37 @@
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;

/**
* @package populate
*/
class Populate
{
use Configurable;
use Extensible;

/**
* @config
*
* @var array
*/
private static $include_yaml_fixtures = [];
private static array $include_yaml_fixtures = [];

/**
* @config
*
* An array of classes to clear from the database before importing. While
* populating SiteTree it may be worth clearing the 'SiteTree' table.
*
* @var array
*/
private static $truncate_classes = [];
private static array $truncate_classes = [];

/**
* @config
*
* @var array - Tables that will be truncated
*/
private static $truncate_tables = [];
private static array $truncate_tables = [];

/**
* Flag to determine if we're already run for this session (i.e to prevent
* parent calls invoking {@link requireRecords} twice).
*
* @var bool
*/
private static $ran = false;
private static bool $ran = false;

/**
* @var string[] - Used internally to not truncate multiple tables multiple times
* Used internally to not truncate multiple tables multiple times
*/
private static $clearedTables = [];
private static array $clearedTables = [];

/**
* @param bool $force - allows you to bypass the ran check to run this multiple times
* @return bool
* @throws Exception
*/
public static function requireRecords($force = false): bool
public static function requireRecords(bool $force = false): bool
{
if (self::$ran && !$force) {
return true;
Expand Down Expand Up @@ -107,8 +87,6 @@ public static function requireRecords($force = false): bool

/**
* Delete all the associated tables for a class
*
* @param string $className
*/
private static function truncateObject(string $className): void
{
Expand Down Expand Up @@ -182,8 +160,6 @@ function ($next) {
/**
* Attempts to truncate a table. Outputs messages to indicate if table has
* already been truncated or cannot be truncated
*
* @param string $table
*/
private static function truncateTable(string $table): void
{
Expand Down
51 changes: 26 additions & 25 deletions code/PopulateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use SilverStripe\Dev\FixtureBlueprint;
use SilverStripe\Dev\FixtureFactory;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\ValidationException;
use SilverStripe\Versioned\Versioned;
use function basename;
use function dirname;
Expand All @@ -21,29 +23,26 @@
use function sizeof;
use function str_replace;

/**
* @package populate
*/
class PopulateFactory extends FixtureFactory
{
/**
* List of fixtures that failed to be created due to YAML fixture lookup failures (e.g. because of a dependency that
* isn't met at the time of creation). We re-try creation of these after all other fixtures have been created.
*
* @var array
*/
private $failedFixtures = [];
private array $failedFixtures = [];

/**
* Creates the object in the database as the original object will be wiped.
*
* @param string $class
* @param string $identifier
* @param array $data
* @param string $name Name of the {@link FixtureBlueprint} to use, usually a DataObject subclass.
* @param string $identifier Unique identifier for this fixture type
* @param array $data Map of properties. Overrides default data
* @return bool|DataObject|null
* @throws ValidationException
*/
public function createObject($class, $identifier, $data = null)
public function createObject($name, $identifier, $data = null)
{
DB::alteration_message("Creating $identifier ($class)", "created");
DB::alteration_message("Creating $identifier ($name)", "created");

if ($data) {
foreach ($data as $k => $v) {
Expand All @@ -65,11 +64,12 @@ public function createObject($class, $identifier, $data = null)

if ($file) {
// Skip the rest of this method (populateFile sets all other values on the object), just return the created file
if (!isset($this->fixtures[$class])) {
$this->fixtures[$class] = [];
if (!isset($this->fixtures[$name])) {
$this->fixtures[$name] = [];
}

$this->fixtures[$class][$identifier] = $file->ID;
$this->fixtures[$name][$identifier] = $file->ID;

return $file;
}
}
Expand All @@ -79,7 +79,7 @@ public function createObject($class, $identifier, $data = null)
$lookup = null;

if (isset($data['PopulateMergeWhen'])) {
$lookup = DataList::create($class)->where(
$lookup = DataList::create($name)->where(
$data['PopulateMergeWhen']
);

Expand All @@ -95,11 +95,11 @@ public function createObject($class, $identifier, $data = null)
throw new Exception('Not a valid PopulateMergeMatch filter');
}

$lookup = DataList::create($class)->filter($filter);
$lookup = DataList::create($name)->filter($filter);

unset($data['PopulateMergeMatch']);
} elseif (isset($data['PopulateMergeAny'])) {
$lookup = DataList::create($class);
$lookup = DataList::create($name);

unset($data['PopulateMergeAny']);
}
Expand All @@ -121,7 +121,7 @@ public function createObject($class, $identifier, $data = null)
$old->delete();
}

$blueprint = new FixtureBlueprint($class);
$blueprint = new FixtureBlueprint($name);
$obj = $blueprint->createObject($identifier, $data, $this->fixtures);
$latest = $obj->toMap();

Expand All @@ -132,24 +132,24 @@ public function createObject($class, $identifier, $data = null)

$obj->delete();

$this->fixtures[$class][$identifier] = $existing->ID;
$this->fixtures[$name][$identifier] = $existing->ID;

$obj = $existing;
$obj->flushCache();
} else {
try {
$obj = parent::createObject($class, $identifier, $data);
$obj = parent::createObject($name, $identifier, $data);
} catch (InvalidArgumentException $e) {
$this->failedFixtures[] = [
'class' => $class,
'class' => $name,
'id' => $identifier,
'data' => $data,
];

DB::alteration_message(sprintf('Exception: %s', $e->getMessage()), 'error');

DB::alteration_message(
sprintf('Failed to create %s (%s), queueing for later', $identifier, $class),
sprintf('Failed to create %s (%s), queueing for later', $identifier, $name),
'error'
);

Expand All @@ -174,7 +174,7 @@ public function createObject($class, $identifier, $data = null)
* @param bool $recurse Marker for whether we are recursing - should be false when calling from outside this method
* @throws Exception
*/
public function processFailedFixtures($recurse = false)
public function processFailedFixtures(bool $recurse = false): void
{
if (!$this->failedFixtures) {
DB::alteration_message('No failed fixtures to process', 'created');
Expand Down Expand Up @@ -225,10 +225,10 @@ public function processFailedFixtures($recurse = false)

/**
* @param array $data
* @return File|bool The created (or updated) File object
* @return File|bool The created (or updated) File object, or true if the file already existed
* @throws Exception If anything is missing and the file can't be processed
*/
private function populateFile($data)
private function populateFile(array $data): File|bool
{
if (!isset($data['Filename']) || !isset($data['PopulateFileFrom'])) {
throw new Exception('When passing "PopulateFileFrom", you must also pass "Filename" with the path that you want to file to be stored at (e.g. assets/test.jpg)');
Expand Down Expand Up @@ -285,6 +285,7 @@ private function populateFile($data)
$file->write();
$file->publishRecursive();
} catch (Exception $e) {
throw $e;
DB::alteration_message($e->getMessage(), "error");
}

Expand Down
7 changes: 2 additions & 5 deletions code/PopulateTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;

/**
* @package populate
*/
class PopulateTask extends BuildTask
{
private static $segment = 'PopulateTask';
private static string $segment = 'PopulateTask';

/**
* @param HTTPRequest $request
* @throws Exception
Expand Down
2 changes: 0 additions & 2 deletions code/extensions/PopulateMySQLExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
* extensions
* - PopulateMySQLExportExtension
* </code>
*
* @package populate
*/
class PopulateMySQLExportExtension extends Extension
{
Expand Down
14 changes: 10 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"email": "will@fullscreen.io"
}],
"require": {
"php": "^7.4 || ^8.0",
"silverstripe/framework": "^4",
"silverstripe/versioned": "^1"
"php": "^8.1",
"silverstripe/framework": "^5",
"silverstripe/versioned": "^2"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"silverstripe/recipe-testing": "^3",
"squizlabs/php_codesniffer": "^3.0"
},
"autoload": {
Expand All @@ -28,5 +28,11 @@
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"config": {
"allow-plugins": {
"composer/installers": true,
"silverstripe/vendor-plugin": true
}
}
}
27 changes: 14 additions & 13 deletions tests/php/PopulateFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@
use DNADesign\Populate\Tests\PopulateFactoryTest\PopulateFactoryTestVersionedObject;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Image;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Versioned\Versioned;

/**
* @package populate
*/
class PopulateFactoryTest extends SapphireTest implements TestOnly
{
/**
Expand Down Expand Up @@ -46,7 +42,7 @@ public function testVersionedObjects(): void
{
$versioned = $this->objFromFixture(PopulateFactoryTestVersionedObject::class, 'objV1');

$versioned->publish('Stage', 'Live');
$versioned->publishSingle();

$obj = $this->factory->createObject(PopulateFactoryTestVersionedObject::class, 'test', [
'Content' => 'Updated Version Foo',
Expand Down Expand Up @@ -153,14 +149,19 @@ public function testCreatingFileOrImage()

// Create a file/image, check if data stored in database with expected DataObject class and file exists
foreach ($files as $name => $class) {
$this->factory->createObject(File::class, $name, [
'Filename' => $name,
'PopulateFileFrom' => sprintf('tests/assets/%s', $name),
]);

$file = Injector::inst()->get($class)->get()->filter('Name', $name)->first();
$this->assertEquals($class, get_class($file));
$this->assertTrue($file->exists());
// BASE_PATH is prepended to the file path during populateFile(), so we need to remove it here
$filePath = str_replace(BASE_PATH, '', sprintf('%s/../assets/%s', dirname(__FILE__), $name));
$file = $this->factory->createObject(
File::class,
$name,
[
'Filename' => $name,
'PopulateFileFrom' => $filePath,
]
);

$this->assertTrue((bool) $file?->exists());
$this->assertEquals($class, $file->ClassName);
}
}
}
Loading

0 comments on commit af2a44c

Please sign in to comment.