Merge pull request #45 from open-sausages/feature/test-page-types

ENHANCEMENT: Add page type creator task
This commit is contained in:
Damian Mooyman 2017-12-13 15:36:49 +13:00 committed by GitHub
commit fe4aa6e623
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 132 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules/
/**/*.js.map
code/test-pages/*Page.php

View File

@ -1,6 +1,8 @@
<?php
use SilverStripe\Dev\BuildTask;
use SilverStripe\Core\ClassInfo;
/**
* Creates sample page structure, useful to test tree performance,
* UI behaviour on deeply nested pages etc.
@ -30,16 +32,20 @@ class FTPageMakerTask extends BuildTask
{
$maxDepth = count($this->pageCountByDepth);
$pageCount = $this->pageCountByDepth[$depth];
$testPageClasses = ClassInfo::implementorsOf('TestPageInterface');
$testPageClasses[] = 'Page';
for ($i=1; $i<=$pageCount; $i++) {
$fullPrefix = $prefix ? "{$prefix}-{$i}" : $i;
$page = new Page();
$randomIndex = array_rand($testPageClasses);
$pageClass = $testPageClasses[$randomIndex];
$page = new $pageClass();
$page->ParentID = $parentID;
$page->Title = "Test page {$fullPrefix}";
$page->write();
$page->publish('Stage', 'Live');
echo "Created '$page->Title'\n";
echo "Created '$page->Title' ($page->ClassName)\n";
$pageID = $page->ID;
unset($page);

View File

@ -0,0 +1,111 @@
<?php
use SilverStripe\Dev\BuildTask;
use Faker\Factory;
use SilverStripe\Control\HTTPRequest;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use SilverStripe\Core\Manifest\ModuleLoader;
use Symfony\Component\Filesystem\Exception\IOException;
class FTPageTypeCreatorTask extends BuildTask
{
/**
* @var \Faker\Factory
*/
protected $faker;
/**
* @var Filesystem
*/
protected $fs;
/**
* @var Finder
*/
protected $finder;
/**
* FTPageTypeCreatorTask constructor.
*/
public function __construct()
{
parent::__construct();
$this->fs = new Filesystem();
$this->faker = Factory::create();
$this->finder = new Finder();
}
/**
* @param HTTPRequest $request
*/
public function run($request)
{
$count = $request->getVar('count') ?: 20;
$module = ModuleLoader::getModule('silverstripe/frameworktest');
$testPageDir = $module->getPath() . '/code/test-pages';
if (!$this->fs->exists($testPageDir)) {
throw new RuntimeException("Test page directory $testPageDir does not exist!");
}
$pageTypes = $this->getExistingClassNames($testPageDir);
$created = 0;
while ($created < $count) {
$className = null;
while (
!$className ||
in_array($className, $pageTypes) ||
class_exists(basename($className, 'php'))
) {
$className = $this->generateClassName();
}
$pageTypes[] = $className;
$code = $this->generateClassCode($className);
$filePath = sprintf('%s/%s.php', $testPageDir, $className);
try {
$this->fs->dumpFile($filePath, $code);
$created++;
} catch (IOException $e) {
echo "Could not write to file $filePath. Got error: {$e->getMessage()}\n";
die();
}
echo "Created page type $className\n";
}
}
private function getExistingClassNames($dir)
{
$files = $this->finder
->in($dir)
->files()
->name('*Page.php');
return array_map(function ($file) {
/* @var SplFileInfo $file */
return $file->getBasename();
}, iterator_to_array($files));
}
private function generateClassName()
{
return ucfirst($this->faker->word . 'Page');
}
private function generateClassCode($className)
{
$self = __CLASS__;
$code = <<<PHP
<?php
/**
* Generated by $self
*/
class $className extends Page implements TestPageInterface
{
}
PHP;
return $code;
}
}

View File

@ -0,0 +1,10 @@
<?php
/**
* Classes that implement this interface are identified as generated
* page types for testing
*/
interface TestPageInterface
{
}

View File

@ -14,6 +14,7 @@
{
"silverstripe/framework": "~4.0",
"silverstripe/cms": "~4.0",
"guzzlehttp/guzzle": "~6.0"
"guzzlehttp/guzzle": "~6.0",
"fzaninotto/faker": "^1.7"
}
}