From 6050e9def902da0d9923ceac46a916cdd38ca696 Mon Sep 17 00:00:00 2001 From: Aaron Carlino Date: Thu, 13 Jan 2022 23:46:13 +1300 Subject: [PATCH] More tests --- src/Schema/BulkLoader/BulkLoaderSet.php | 8 ++ src/Schema/BulkLoader/Collection.php | 2 +- tests/Fake/Extensions/FakeExtension1.php | 11 +++ tests/Fake/Extensions/FakeExtension2.php | 11 +++ tests/Fake/FakeBulkLoader.php | 10 ++- tests/Schema/BulkLoader/BulkLoaderSetTest.php | 60 ++++++++++++++ tests/Schema/BulkLoader/CollectionTest.php | 66 ++++++++++++++++ .../Schema/BulkLoader/ExtensionLoaderTest.php | 78 +++++++++++++++++++ .../Schema/BulkLoader/FilepathLoaderTest.php | 62 +++++++++++++++ .../BulkLoader/InheritanceLoaderTest.php | 58 ++++++++++++++ .../Schema/BulkLoader/NamespaceLoaderTest.php | 50 ++++++++++++ 11 files changed, 412 insertions(+), 4 deletions(-) create mode 100644 tests/Fake/Extensions/FakeExtension1.php create mode 100644 tests/Fake/Extensions/FakeExtension2.php create mode 100644 tests/Schema/BulkLoader/BulkLoaderSetTest.php create mode 100644 tests/Schema/BulkLoader/CollectionTest.php create mode 100644 tests/Schema/BulkLoader/ExtensionLoaderTest.php create mode 100644 tests/Schema/BulkLoader/FilepathLoaderTest.php create mode 100644 tests/Schema/BulkLoader/InheritanceLoaderTest.php create mode 100644 tests/Schema/BulkLoader/NamespaceLoaderTest.php diff --git a/src/Schema/BulkLoader/BulkLoaderSet.php b/src/Schema/BulkLoader/BulkLoaderSet.php index d493bb55..6359e252 100644 --- a/src/Schema/BulkLoader/BulkLoaderSet.php +++ b/src/Schema/BulkLoader/BulkLoaderSet.php @@ -107,4 +107,12 @@ public function setLoaders($loaders): self return $this; } + + /** + * @return AbstractBulkLoader[] + */ + public function getLoaders(): array + { + return $this->loaders; + } } diff --git a/src/Schema/BulkLoader/Collection.php b/src/Schema/BulkLoader/Collection.php index c81e7c01..bd56cd8b 100644 --- a/src/Schema/BulkLoader/Collection.php +++ b/src/Schema/BulkLoader/Collection.php @@ -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); } diff --git a/tests/Fake/Extensions/FakeExtension1.php b/tests/Fake/Extensions/FakeExtension1.php new file mode 100644 index 00000000..61bb7d74 --- /dev/null +++ b/tests/Fake/Extensions/FakeExtension1.php @@ -0,0 +1,11 @@ +shouldReturn = $shouldReturn; @@ -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; } } diff --git a/tests/Schema/BulkLoader/BulkLoaderSetTest.php b/tests/Schema/BulkLoader/BulkLoaderSetTest.php new file mode 100644 index 00000000..b94c1e07 --- /dev/null +++ b/tests/Schema/BulkLoader/BulkLoaderSetTest.php @@ -0,0 +1,60 @@ +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()); + } +} diff --git a/tests/Schema/BulkLoader/CollectionTest.php b/tests/Schema/BulkLoader/CollectionTest.php new file mode 100644 index 00000000..8cd3ad01 --- /dev/null +++ b/tests/Schema/BulkLoader/CollectionTest.php @@ -0,0 +1,66 @@ + '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() + ); + + } + + +} diff --git a/tests/Schema/BulkLoader/ExtensionLoaderTest.php b/tests/Schema/BulkLoader/ExtensionLoaderTest.php new file mode 100644 index 00000000..c5cc49fe --- /dev/null +++ b/tests/Schema/BulkLoader/ExtensionLoaderTest.php @@ -0,0 +1,78 @@ +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); + + } +} diff --git a/tests/Schema/BulkLoader/FilepathLoaderTest.php b/tests/Schema/BulkLoader/FilepathLoaderTest.php new file mode 100644 index 00000000..da566267 --- /dev/null +++ b/tests/Schema/BulkLoader/FilepathLoaderTest.php @@ -0,0 +1,62 @@ +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); + + } +} diff --git a/tests/Schema/BulkLoader/InheritanceLoaderTest.php b/tests/Schema/BulkLoader/InheritanceLoaderTest.php new file mode 100644 index 00000000..32dbcae8 --- /dev/null +++ b/tests/Schema/BulkLoader/InheritanceLoaderTest.php @@ -0,0 +1,58 @@ +collect($collection)->getClasses(); + + $this->assertEquals([ + A1::class, + A1a::class, + B1::class, + B1a::class, + C::class, + C1::class, + ], $result); + } +} diff --git a/tests/Schema/BulkLoader/NamespaceLoaderTest.php b/tests/Schema/BulkLoader/NamespaceLoaderTest.php new file mode 100644 index 00000000..7c8d2f01 --- /dev/null +++ b/tests/Schema/BulkLoader/NamespaceLoaderTest.php @@ -0,0 +1,50 @@ +collect($collection)->getClasses(); + + $this->assertEquals([ + A::class, + A1::class, + A2::class, + A1a::class, + ], $result); + } +}