From 6274d9ac7cdd7f524f6201a469a0c5fb909ebeda Mon Sep 17 00:00:00 2001 From: thierrydallacroce Date: Wed, 4 Dec 2019 14:12:20 -0800 Subject: [PATCH] Extract common mock chain from dkan_metastore service test (#267) --- .../tests/src/Unit/ServiceTest.php | 78 ++++++------------- 1 file changed, 23 insertions(+), 55 deletions(-) diff --git a/modules/custom/dkan_metastore/tests/src/Unit/ServiceTest.php b/modules/custom/dkan_metastore/tests/src/Unit/ServiceTest.php index 8c0d9cb53..d27087d38 100644 --- a/modules/custom/dkan_metastore/tests/src/Unit/ServiceTest.php +++ b/modules/custom/dkan_metastore/tests/src/Unit/ServiceTest.php @@ -20,13 +20,8 @@ class ServiceTest extends TestCase { * */ public function testGetSchemas() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "getAllIds", ["1"]) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")); + $container = $this->getCommonMockChain() + ->add(SchemaRetriever::class, "getAllIds", ["1"]); $service = Service::create($container->getMock()); $this->assertEquals(json_encode(["1" => "blah"]), json_encode($service->getSchemas())); @@ -36,12 +31,7 @@ public function testGetSchemas() { * */ public function testGetSchema() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")); + $container = $this->getCommonMockChain(); $service = Service::create($container->getMock()); $this->assertEquals(json_encode("blah"), json_encode($service->getSchema("1"))); @@ -51,12 +41,7 @@ public function testGetSchema() { * */ public function testGetAll() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")) + $container = $this->getCommonMockChain() ->add(Sae::class, "getInstance", Engine::class) ->add(Engine::class, "get", [json_encode("blah")]); @@ -69,12 +54,7 @@ public function testGetAll() { * */ public function testGet() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")) + $container = $this->getCommonMockChain() ->add(Sae::class, "getInstance", Engine::class) ->add(Engine::class, "get", json_encode("blah")); @@ -94,12 +74,7 @@ public function testGetResources() { ], ]; - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")) + $container = $this->getCommonMockChain() ->add(Sae::class, "getInstance", Engine::class) ->add(Engine::class, "get", json_encode($dataset)); @@ -113,12 +88,7 @@ public function testGetResources() { * */ public function testPost() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")) + $container = $this->getCommonMockChain() ->add(Sae::class, "getInstance", Engine::class) ->add(Engine::class, "post", "1"); @@ -131,12 +101,7 @@ public function testPost() { * */ public function testPut() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")) + $container = $this->getCommonMockChain() ->add(Sae::class, "getInstance", Engine::class) ->add(Engine::class, "put", "1") ->add(Engine::class, "get", "1"); @@ -151,12 +116,7 @@ public function testPut() { * */ public function testPatch() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")) + $container = $this->getCommonMockChain() ->add(Sae::class, "getInstance", Engine::class) ->add(Engine::class, "patch", "1") ->add(Engine::class, "get", "1"); @@ -170,12 +130,7 @@ public function testPatch() { * */ public function testDelete() { - $container = (new Chain($this)) - ->add(Container::class, "get", (new Options()) - ->add('dkan_schema.schema_retriever', SchemaRetriever::class) - ->add('dkan_metastore.sae_factory', Sae::class) - ) - ->add(SchemaRetriever::class, "retrieve", json_encode("blah")) + $container = $this->getCommonMockChain() ->add(Sae::class, "getInstance", Engine::class) ->add(Engine::class, "delete", "1") ->add(Engine::class, "get", "1"); @@ -185,4 +140,17 @@ public function testDelete() { $this->assertEquals("1", $service->delete("dataset", "1")); } + /** + * @return \Drupal\dkan_common\Tests\Mock\Chain + */ + public function getCommonMockChain() { + $options = (new Options()) + ->add('dkan_schema.schema_retriever', SchemaRetriever::class) + ->add('dkan_metastore.sae_factory', Sae::class); + + return (new Chain($this)) + ->add(Container::class, "get", $options) + ->add(SchemaRetriever::class, "retrieve", json_encode("blah")); + } + }