Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Jan 13, 2022
1 parent 9d71588 commit 6050e9d
Show file tree
Hide file tree
Showing 11 changed files with 412 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/Schema/BulkLoader/BulkLoaderSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@ public function setLoaders($loaders): self

return $this;
}

/**
* @return AbstractBulkLoader[]
*/
public function getLoaders(): array
{
return $this->loaders;
}
}
2 changes: 1 addition & 1 deletion src/Schema/BulkLoader/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Collection
* @param array $manifest An array of classname keys to filepath values ['My\Class' => '/path/to/Class.php']
* @throws Exception
*/
public function __construct(array $manifest)
public function __construct(array $manifest = [])
{
$this->setManifest($manifest);
}
Expand Down
11 changes: 11 additions & 0 deletions tests/Fake/Extensions/FakeExtension1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace SilverStripe\GraphQL\Tests\Fake\Extensions;

use SilverStripe\ORM\DataExtension;

class FakeExtension1 extends DataExtension
{

}
11 changes: 11 additions & 0 deletions tests/Fake/Extensions/FakeExtension2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace SilverStripe\GraphQL\Tests\Fake\Extensions;

use SilverStripe\ORM\DataExtension;

class FakeExtension2 extends DataExtension
{

}
10 changes: 7 additions & 3 deletions tests/Fake/FakeBulkLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class FakeBulkLoader extends AbstractBulkLoader

/**
* FakeBulkLoader constructor.
* @param array $shouldReturn
* @param array|null $shouldReturn
*/
public function __construct($shouldReturn = [])
public function __construct(?array $shouldReturn = null)
{
parent::__construct();
$this->shouldReturn = $shouldReturn;
Expand All @@ -32,6 +32,10 @@ public static function getIdentifier(): string
*/
public function collect(Collection $collection): Collection
{
return new Collection($this->shouldReturn);
if ($this->shouldReturn) {
return new Collection($this->shouldReturn);
}

return $collection;
}
}
60 changes: 60 additions & 0 deletions tests/Schema/BulkLoader/BulkLoaderSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php


namespace SilverStripe\GraphQL\Tests\Schema\BulkLoader;


use Fake\FakeBulkLoader;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\BulkLoader\AbstractBulkLoader;
use SilverStripe\GraphQL\Schema\BulkLoader\BulkLoaderSet;
use SilverStripe\GraphQL\Schema\BulkLoader\Collection;

class BulkLoaderSetTest extends SapphireTest
{
public function testApplyConfig()
{
$set = new BulkLoaderSet();
$set->applyConfig([
'fake' => [
'include' => [],
]
]);

foreach ($set->getLoaders() as $loader) {
$this->assertInstanceOf(AbstractBulkLoader::class, $loader);
}
}

public function testInvalidLoader()
{
$this->expectExceptionMessage('Loader "fail" does not exist');
$set = new BulkLoaderSet();
$set->applyConfig([
'fail' => [
'include' => [],
]
]);

}

public function testProcess()
{
$set = new BulkLoaderSet([
new FakeBulkLoader(['one' => 'one', 'two' => 'two']),
new FakeBulkLoader(['three' => 'three', 'four' => 'four']),
], new Collection());
$result = $set->process();
$this->assertEquals(['three', 'four'], $result->getClasses());
}

public function testInitialCollection()
{
$set = new BulkLoaderSet([
new FakeBulkLoader(),
new FakeBulkLoader(),
], new Collection(['foo' => 'foo', 'bar' => 'bar']));
$result = $set->process();
$this->assertEquals(['foo', 'bar'], $result->getClasses());
}
}
66 changes: 66 additions & 0 deletions tests/Schema/BulkLoader/CollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php


namespace SilverStripe\GraphQL\Tests\Schema\BulkLoader;


use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\BulkLoader\Collection;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B1;

class CollectionTest extends SapphireTest
{
public function testCollection()
{
$collection = new Collection([
'class1' => 'file1',
'class2' => 'file2',
'class3' => 'file3',
'class4' => 'file4',
]);

$collection->removeClass('class2')
->removeFile('file3');

$this->assertEquals(
['class1', 'class4'],
$collection->getClasses()
);

$this->assertEquals(
['file1', 'file4'],
$collection->getFiles()
);

$this->assertEquals(
[
'class1' => 'file1',
'class4' => 'file4',
],
$collection->getManifest()
);

}

public function testCreateFromClassList()
{
$collection = Collection::createFromClassList([
A1::class,
B1::class,
]);
$mod = ModuleLoader::inst()->getManifest()->getModule('silverstripe/graphql');
$path = $mod->getPath();
$this->assertEquals(
[
A1::class => $path . '/tests/Fake/Inheritance/A1.php',
B1::class => $path . '/tests/Fake/Inheritance/B1.php',
],
$collection->getManifest()
);

}


}
78 changes: 78 additions & 0 deletions tests/Schema/BulkLoader/ExtensionLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php


namespace SilverStripe\GraphQL\Tests\Schema\BulkLoader;


use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\BulkLoader\Collection;
use SilverStripe\GraphQL\Schema\BulkLoader\ExtensionLoader;
use SilverStripe\GraphQL\Tests\Fake\Extensions\FakeExtension1;
use SilverStripe\GraphQL\Tests\Fake\Extensions\FakeExtension2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A1a;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B1a;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C2a;

class ExtensionLoaderTest extends SapphireTest
{
public function testExtensionLoader()
{
A1::add_extension(FakeExtension1::class);
A2::add_extension(FakeExtension1::class);

B1::add_extension(FakeExtension2::class);
B2::add_extension(FakeExtension2::class);

C1::add_extension(FakeExtension1::class);
C1::add_extension(FakeExtension2::class);

$collection = Collection::createFromClassList([
A::class,
A1::class,
A2::class,
A1a::class,
B::class,
B1::class,
B2::class,
B1a::class,
C::class,
C1::class,
C2::class,
C2a::class,
]);

$loader = new ExtensionLoader(
[FakeExtension1::class],
[FakeExtension2::class]
);

$result = $loader->collect($collection)->getClasses();
$this->assertEquals([
A1::class,
A2::class,
A1a::class,
], $result);

$loader = new ExtensionLoader(
[FakeExtension2::class]
);

$result = $loader->collect($collection)->getClasses();
$this->assertEquals([
B1::class,
B2::class,
B1a::class,
C1::class,
], $result);

}
}
62 changes: 62 additions & 0 deletions tests/Schema/BulkLoader/FilepathLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php


namespace SilverStripe\GraphQL\Tests\Schema\BulkLoader;


use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\BulkLoader\Collection;
use SilverStripe\GraphQL\Schema\BulkLoader\FilepathLoader;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A1a;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B1a;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C2a;

class FilepathLoaderTest extends SapphireTest
{
public function testFilepathLoader()
{
$path = ModuleLoader::inst()
->getManifest()
->getModule('silverstripe/graphql')
->getRelativePath();

$collection = Collection::createFromClassList([
A::class,
A1::class,
A2::class,
A1a::class,
B::class,
B1::class,
B2::class,
B1a::class,
C::class,
C1::class,
C2::class,
C2a::class,
]);

$loader = new FilepathLoader(
[$path . '/tests/Fake/Inheritance/B*.php'],
[$path . '/tests/Fake/Inheritance/*a.php']
);

$result = $loader->collect($collection)->getClasses();

$this->assertEquals([
B::class,
B1::class,
B2::class,
], $result);

}
}
58 changes: 58 additions & 0 deletions tests/Schema/BulkLoader/InheritanceLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php


namespace SilverStripe\GraphQL\Tests\Schema\BulkLoader;


use SilverStripe\Dev\SapphireTest;
use SilverStripe\GraphQL\Schema\BulkLoader\Collection;
use SilverStripe\GraphQL\Schema\BulkLoader\InheritanceLoader;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A1a;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\A2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B1a;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\B2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C1;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C2;
use SilverStripe\GraphQL\Tests\Fake\Inheritance\C2a;

class InheritanceLoaderTest extends SapphireTest
{
public function testInheritanceLoader()
{
$collection = Collection::createFromClassList([
A::class,
A1::class,
A2::class,
A1a::class,
B::class,
B1::class,
B2::class,
B1a::class,
C::class,
C1::class,
C2::class,
C2a::class,
]);

$loader = new InheritanceLoader(
[A1::class, B1::class, C::class],
[C2::class]
);

$result = $loader->collect($collection)->getClasses();

$this->assertEquals([
A1::class,
A1a::class,
B1::class,
B1a::class,
C::class,
C1::class,
], $result);
}
}
Loading

0 comments on commit 6050e9d

Please sign in to comment.