Add new page type creator task, Faker dependency

This commit is contained in:
Aaron Carlino 2017-11-29 11:45:42 +13:00
parent bcc123b905
commit fbd30fac98
56 changed files with 599 additions and 3 deletions

View File

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

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class AliquidPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class AperiamPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class AtquePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class AutPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class AutemPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class BeataePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class ConsequaturPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class CorruptiPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class CulpaPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class CumPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class DelenitiPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class DolorPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class DoloremquePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class EaPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class EligendiPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class EosPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class EstPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class EtPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class EvenietPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class ExpeditaPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class FacerePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class FacilisPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class FugiatPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class IdPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class InventorePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class IurePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class LaborePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class MinimaPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class NecessitatibusPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class NumquamPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class OmnisPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class PraesentiumPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class QuaeratPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class QuasPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class QuiPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class QuodPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class QuosPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class RepellatPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class ReprehenderitPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class RerumPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class SapientePage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class SedPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class SintPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class SuntPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class SuscipitPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class TemporibusPage extends Page implements TestPageInterface
{
}

View File

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

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class UllamPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class VelitPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class VeritatisPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class VoluptatemPage extends Page implements TestPageInterface
{
}

View File

@ -0,0 +1,9 @@
<?php
/**
* Generated by FTPageTypeCreatorTask
*/
class VoluptatesPage extends Page implements TestPageInterface
{
}

View File

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