Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ method.

## Fluent support

It is my intention to support Fluent and exporting Localised fields in the future, but at this time, there is no
support provided.
I would like to add Fluent support, but realistically, I don't have the time to do so at the moment. If anyone has the
capacity to add Fluent support, I would be happy to review and merge a PR for that.

## Future features

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"issues": "https://github.com/chrispenny/silverstripe-dataobject-to-fixture/issues"
},
"require": {
"php": "^8.1",
"silverstripe/framework": "^5",
"silverstripe/cms": "^5"
"php": "^8.3",
"silverstripe/framework": "^6",
"silverstripe/cms": "^6"
},
"require-dev": {
"silverstripe/recipe-testing": "^3",
"silverstripe/recipe-testing": "^4",
"slevomat/coding-standard": "^8"
},
"autoload": {
Expand Down
22 changes: 12 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<testsuites>
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</source>
</phpunit>
70 changes: 0 additions & 70 deletions src/Helper/FluentHelper.php

This file was deleted.

83 changes: 57 additions & 26 deletions src/Task/GenerateFixtureFromDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use ChrisPenny\DataObjectToFixture\Service\FixtureService;
use Exception;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Model\List\PaginatedList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\PolyExecution\PolyOutput;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Throwable;

/**
Expand All @@ -18,42 +21,38 @@
class GenerateFixtureFromDataObject extends BuildTask
{

protected $title = 'Generate Fixture From DataObject'; // phpcs:ignore
protected static string $commandName = 'generate-fixture-from-dataobject';

protected $description = 'Generate a text fixture from a DataObject in your Database'; // phpcs:ignore
protected string $title = 'Generate Fixture From DataObject';

private static string $segment = 'generate-fixture-from-dataobject'; // phpcs:ignore
protected static string $description = 'Generate a text fixture from a DataObject in your Database';

private ?int $previousExecution = null;

/**
* @param HTTPRequest|mixed $request
* @throws Exception
*/
public function run($request): void // phpcs:ignore
protected function execute(InputInterface $input, PolyOutput $output): int
{
try {
$this->previousExecution = ini_get('max_execution_time');
ini_set('max_execution_time', 60);

$this->outputStyles();

$className = $request->getVar('ClassName');
$id = $request->getVar('ID');
$className = $input->getOption('ClassName');
$id = $input->getOption('ID');

// We have both a ClassName and ID, which means we can render out the fixture for a particular record
if ($className && $id) {
$this->outputFixture($request, $className, (int)$id);
$this->outputFixture($className, (int) $id);

return;
return Command::SUCCESS;
}

// We have a ClassName, but not ID yet, this means the user needs to be provided with a list of available
// records for that particular class
if ($className) {
$this->outputClassForm($request, $className);
$this->outputClassForm($input, $className);

return;
return Command::SUCCESS;
}

// The initial form is a list of available classes
Expand All @@ -62,6 +61,8 @@ public function run($request): void // phpcs:ignore
throw $e;
} finally {
ini_set('max_execution_time', $this->previousExecution);

return Command::SUCCESS;
}
}

Expand Down Expand Up @@ -100,10 +101,10 @@ protected function outputInitialForm(): void
echo '</form>';
}

protected function outputClassForm(HTTPRequest $request, string $className): void
protected function outputClassForm(InputInterface $input, string $className): void
{
$dbFields = Config::inst()->get($className, 'db');
$start = $request->getVar('start') ?? 0;
$start = $input->getOption('start') ?? 0;

/** @var PaginatedList|DataObject[] $paginatedList */
$paginatedList = PaginatedList::create($className::get());
Expand All @@ -117,7 +118,10 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi
return;
}

echo '<p><a href="/dev/tasks/generate-fixture-from-dataobject">< Back to the beginning</a></p>';
echo sprintf(
'<p><a href="/dev/tasks/%s">< Back to the beginning</a></p>',
self::$commandName
);

echo '<div class="pagination">';
echo '<p><strong>Pagination:</strong></p>';
Expand Down Expand Up @@ -149,7 +153,7 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi

// Remove Datatime and Boolean fields, as they're (likely) not that useful for trying to find a specific record
$dbFields = array_diff($dbFields, ['Datetime', 'Boolean']);
$linkTemplate = '<td><a href="/dev/tasks/generate-fixture-from-dataobject?ClassName=%s&ID=%s">Link</a></td>';
$linkTemplate = '<td><a href="/dev/tasks/%s?ClassName=%s&ID=%s">Link</a></td>';

echo '<table>';
echo '<thead>';
Expand All @@ -168,7 +172,7 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi

foreach ($paginatedList as $dataObject) {
echo '<tr>';
echo sprintf($linkTemplate, $className, $dataObject->ID);
echo sprintf($linkTemplate, self::$commandName, $className, $dataObject->ID);
echo sprintf('<td>%s</td>', $dataObject->ID);

foreach (array_keys($dbFields) as $fieldName) {
Expand All @@ -184,12 +188,9 @@ protected function outputClassForm(HTTPRequest $request, string $className): voi
}

/**
* @param HTTPRequest $request
* @param string $className
* @param int $id
* @throws Exception
*/
protected function outputFixture(HTTPRequest $request, string $className, int $id): void
protected function outputFixture(string $className, int $id): void
{
if (!$className) {
echo '<p>No ClassName provided</p>';
Expand All @@ -208,6 +209,11 @@ protected function outputFixture(HTTPRequest $request, string $className, int $i

if ($dataObject === null) {
echo sprintf('<p>DataObject not found. ClassName: %s, ID: %s</p>', $className, $id);
echo sprintf(
'<p><a href="/dev/tasks/%s?ClassName=%s">< Back to list of records</a></p>',
self::$commandName,
$className
);

return;
}
Expand All @@ -220,7 +226,8 @@ protected function outputFixture(HTTPRequest $request, string $className, int $i
}

echo sprintf(
'<p><a href="/dev/tasks/generate-fixture-from-dataobject?ClassName=%s">< Back to list of records</a></p>',
'<p><a href="/dev/tasks/%s?ClassName=%s">< Back to list of records</a></p>',
self::$commandName,
$className
);

Expand Down Expand Up @@ -348,4 +355,28 @@ protected function outputStyles(): void
echo '</style>';
}

public function getOptions(): array
{
return [
new InputOption(
'ClassName',
null,
InputOption::VALUE_OPTIONAL,
'Specify class of the DataObject to be exported'
),
new InputOption(
'ID',
null,
InputOption::VALUE_OPTIONAL,
'Specify the ID of the DataObject to be exported'
),
new InputOption(
'start',
null,
InputOption::VALUE_OPTIONAL,
'Pagination value'
),
];
}

}
2 changes: 1 addition & 1 deletion tests/Mocks/Pages/MockNativePoly.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use SilverStripe\ORM\DataObject;

/**
* Uses the Silverstripe 5 array-format polymorphic has_one definition.
* Uses the Silverstripe array-format polymorphic has_one definition.
*
* @property string $Title
* @property int $OwnerID
Expand Down
Loading