Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Jan 12, 2022
1 parent 9d53fe4 commit 9d71588
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 79 deletions.
5 changes: 4 additions & 1 deletion src/Schema/BulkLoader/AbstractBulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public function applyConfig(array $config): self
* @param Collection $collection
* @return Collection
*/
abstract public function collect(Collection $collection): Collection;
public function collect(Collection $collection): Collection
{
return Collection::create($collection->getManifest());
}

/**
* @return string
Expand Down
71 changes: 56 additions & 15 deletions src/Schema/BulkLoader/BulkLoaderSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@

use InvalidArgumentException;
use SilverStripe\Core\ClassInfo;
use ReflectionException;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\GraphQL\Schema\Exception\SchemaBuilderException;
use SilverStripe\GraphQL\Schema\Interfaces\ConfigurationApplier;
use SilverStripe\GraphQL\Schema\Schema;

class BulkLoaderSet
/**
* Composed with a list of bulk loaders to be executed in serial and return the aggregate result
* of all their collect() calls
*/
class BulkLoaderSet implements ConfigurationApplier
{
use Injectable;

/**
* @var AbstractBulkLoader[]
*/
Expand All @@ -16,36 +27,71 @@ class BulkLoaderSet
/**
* @var Collection
*/
private $collection;
private $initialCollection;

/**
* BulkLoaderSet constructor.
* @param AbstractBulkLoader[] ...$loaders
* @param Collection|null $collection
* @param AbstractBulkLoader[] $loaders
* @param Collection|null $initialCollection
* @throws ReflectionException
*/
public function __construct(...$loaders, ?Collection $collection = null)
public function __construct(array $loaders = [], ?Collection $initialCollection = null)
{
$this->setLoaders($loaders);
if ($collection) {
$this->setCollection($collection);
if ($initialCollection) {
$this->initialCollection = $initialCollection;
} else {
$this->collection = Collection::create(ClassInfo::allClasses());
$this->initialCollection = Collection::createFromClassList(ClassInfo::allClasses());
}
}

/**
* @param array $config
* @return mixed|void
* @throws SchemaBuilderException
*/
public function applyConfig(array $config): self
{
$registry = Registry::inst();
foreach ($config as $loaderID => $loaderConfig) {
/* @var AbstractBulkLoader $loader */
$loader = $registry->getByID($loaderID);
Schema::invariant($loader, 'Loader "%s" does not exist', $loaderID);
$loader->applyConfig($loaderConfig);
$this->addLoader($loader);
}

return $this;
}

public function addLoader(AbstractBulkLoader $loader)
/**
* @param AbstractBulkLoader $loader
* @return $this
*/
public function addLoader(AbstractBulkLoader $loader): self
{
$this->loaders[] = $loader;

return $this;
}

/**
* @return Collection
*/
public function process(): Collection
{
$collection = $this->initialCollection;
foreach ($this->loaders as $loader) {
$this->collection = $loader->collect($this->collection);
$collection = $loader->collect($collection);
}

return $collection;
}

/**
* @param $loaders
* @return $this
*/
public function setLoaders($loaders): self
{
foreach ($loaders as $loader) {
Expand All @@ -58,11 +104,6 @@ public function setLoaders($loaders): self
}
}
$this->loaders = $loaders;
}

public function setCollection(Collection $collection): self
{
$this->collection = $collection;

return $this;
}
Expand Down
60 changes: 39 additions & 21 deletions src/Schema/BulkLoader/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace SilverStripe\GraphQL\Schema\BulkLoader;

use Composer\Autoload\ClassLoader;
use SilverStripe\Core\Injector\Injectable;
use Exception;
use ReflectionClass;
use ReflectionException;

/**
* Defines a collection of class names paired with file paths
Expand All @@ -22,36 +22,21 @@ class Collection

/**
* Collection constructor.
* @param array $classList
* @param array $manifest An array of classname keys to filepath values ['My\Class' => '/path/to/Class.php']
* @throws Exception
*/
public function __construct(array $classList)
public function __construct(array $manifest)
{
$this->setClassList($classList);
$this->setManifest($manifest);
}

/**
* An expensive operation that rebuilds the index of className -> filePath
* @param array $classList
* @param array $manifest
* @return $this
* @throws Exception
*/
public function setClassList(array $classList): self
public function setManifest(array $manifest): self
{
$manifest = [];
foreach ($classList as $class) {
if (!class_exists($class)) {
continue;
}
$reflection = new ReflectionClass($class);
$filePath = $reflection->getFileName();
if (!$filePath) {
continue;
}

$manifest[$class] = $filePath;
}

$this->manifest = $manifest;

return $this;
Expand Down Expand Up @@ -95,4 +80,37 @@ public function getFiles(): array
{
return array_values($this->manifest);
}

/**
* @return array
*/
public function getManifest(): array
{
return $this->manifest;
}

/**
* @param array $classList
* @return Collection
* @throws ReflectionException
* @throws Exception
*/
public static function createFromClassList(array $classList): Collection
{
$manifest = [];
foreach ($classList as $class) {
if (!class_exists($class)) {
continue;
}
$reflection = new ReflectionClass($class);
$filePath = $reflection->getFileName();
if (!$filePath) {
continue;
}

$manifest[$class] = $filePath;
}

return new static($manifest);
}
}
15 changes: 11 additions & 4 deletions src/Schema/BulkLoader/ExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,29 @@ public static function getIdentifier(): string
*/
public function collect(Collection $collection): Collection
{
$newCollection = parent::collect($collection);

foreach ($collection->getClasses() as $class) {
$isIncluded = false;
foreach ($this->includeList as $pattern) {
if (!Extensible::has_extension($class, $pattern)) {
$collection->removeClass($class);
if (Extensible::has_extension($class, $pattern)) {
$isIncluded = true;
break;
}
}
foreach ($this->excludeList as $pattern) {
if (Extensible::has_extension($class, $pattern)) {
$collection->removeClass($class);
$isIncluded = false;
break;
}
}

if (!$isIncluded) {
$newCollection->removeClass($class);
}
}

return $collection;
return $newCollection;
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/Schema/BulkLoader/FilepathLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public static function getIdentifier(): string
*/
public function collect(Collection $collection): Collection
{
$newCollection = parent::collect($collection);
$includedFiles = [];
$excludedFiles = [];

foreach ($this->includeList as $include) {
foreach (glob(Path::join(BASE_PATH, $include)) as $path) {
$includedFiles[$path] = true;
Expand All @@ -34,18 +36,15 @@ public function collect(Collection $collection): Collection
foreach (glob(Path::join(BASE_PATH, $exclude)) as $path) {
$excludedFiles[$path] = true;
}

}

foreach ($collection->getFiles() as $file) {
if (!isset($includedFiles[$file])) {
$collection->removeFile($file);
}
if (isset($excludedFiles[$file])) {
$collection->removeFile($file);
$isIncluded = isset($includedFiles[$file]) && !isset($excludedFiles[$file]);
if (!$isIncluded) {
$newCollection->removeFile($file);
}
}

return $collection;
return $newCollection;
}
}
15 changes: 11 additions & 4 deletions src/Schema/BulkLoader/InheritanceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,29 @@ public static function getIdentifier(): string
*/
public function collect(Collection $collection): Collection
{
$newCollection = parent::collect($collection);

foreach ($collection->getClasses() as $class) {
$isIncluded = false;
foreach ($this->includeList as $pattern) {
if ($class !== $pattern && !is_subclass_of($class, $pattern)) {
$collection->removeClass($class);
if ($class === $pattern || is_subclass_of($class, $pattern)) {
$isIncluded = true;
break;
}
}
foreach ($this->excludeList as $pattern) {
if ($class === $pattern || is_subclass_of($class, $pattern)) {
$collection->removeClass($class);
$isIncluded = false;
break;
}
}

if (!$isIncluded) {
$newCollection->removeClass($class);
}
}

return $collection;
return $newCollection;
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/Schema/BulkLoader/NamespaceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@ public static function getIdentifier(): string
*/
public function collect(Collection $collection): Collection
{
$newCollection = parent::collect($collection);

foreach ($collection->getClasses() as $class) {
$isIncluded = false;
foreach ($this->includeList as $pattern) {
if (!fnmatch($pattern, $class, FNM_NOESCAPE)) {
$collection->removeClass($class);
if (fnmatch($pattern, $class, FNM_NOESCAPE)) {
$isIncluded = true;
break;
}
}
foreach ($this->excludeList as $pattern) {
if (fnmatch($pattern, $class, FNM_NOESCAPE)) {
$collection->removeClass($class);
$isIncluded = false;
break;
}
}

if (!$isIncluded) {
$newCollection->removeClass($class);
}
}

return $collection;
return $newCollection;
}
}
Loading

0 comments on commit 9d71588

Please sign in to comment.