From bbe2d1c8be1de23d0bbdf675dfb1475ad19d59e4 Mon Sep 17 00:00:00 2001 From: jisse Reitsma Date: Tue, 13 Sep 2022 15:01:31 +0200 Subject: [PATCH] Various helpers & assertions for UiComponents --- .../AbstractBackendControllerTestCase.php | 67 +++++++++++++++++++ .../MuiBackendControllerTestCase.php | 33 +++++++++ ...ssertAbstractBackendControllerTestCase.php | 13 ++++ .../Adminhtml/AssertAclResourceExists.php | 18 +++++ composer.json | 2 +- 5 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 Test/Integration/Adminhtml/AbstractBackendControllerTestCase.php create mode 100644 Test/Integration/Adminhtml/MuiBackendControllerTestCase.php create mode 100644 Test/Integration/Traits/Adminhtml/AssertAbstractBackendControllerTestCase.php create mode 100644 Test/Integration/Traits/Adminhtml/AssertAclResourceExists.php diff --git a/Test/Integration/Adminhtml/AbstractBackendControllerTestCase.php b/Test/Integration/Adminhtml/AbstractBackendControllerTestCase.php new file mode 100644 index 0000000..1bb92ab --- /dev/null +++ b/Test/Integration/Adminhtml/AbstractBackendControllerTestCase.php @@ -0,0 +1,67 @@ +get(UiComponentFactory::class); + $uiComponent = $uiComponentFactory->create($uiComponentName); + $this->assertEquals($uiComponentName, $uiComponent->getName()); + + $data = $uiComponent->getData(); + $this->assertNotEmpty($data); + + return $uiComponent; + } + + protected function getDataProviderFromUiComponent(UiComponentInterface $uiComponent): DataProviderInterface + { + $data = $uiComponent->getData(); + $this->assertNotEmpty($data); + $this->assertArrayHasKey('js_config', $data); + $this->assertArrayHasKey('provider', $data['js_config']); + $this->assertNotEmpty($data['js_config']['provider']); + $providerName = $data['js_config']['provider']; + $providerName = str_replace($uiComponent->getName() . '.', '', $providerName); + + $childComponents = $uiComponent->getChildComponents(); + $this->assertArrayHasKey($providerName, $childComponents); + /** @var DataSource $providerComponent */ + $providerComponent = $childComponents[$providerName]; + $this->assertInstanceOf(DataSource::class, $providerComponent); + return $providerComponent->getDataProvider(); + } + + protected function getDataFromUiComponentDataProvider(UiComponentInterface $uiComponent): ?array + { + $dataProvider = $this->getDataProviderFromUiComponent($uiComponent); + $data = $dataProvider->getData(); + + if ($uiComponent instanceof UiComponentForm) { + } + + if ($uiComponent instanceof UiComponentListing) { + $this->assertArrayHasKey('totalRecords', $data); + $this->assertArrayHasKey('items', $data); + } + + return $data; + } +} diff --git a/Test/Integration/Adminhtml/MuiBackendControllerTestCase.php b/Test/Integration/Adminhtml/MuiBackendControllerTestCase.php new file mode 100644 index 0000000..e382831 --- /dev/null +++ b/Test/Integration/Adminhtml/MuiBackendControllerTestCase.php @@ -0,0 +1,33 @@ +uri = 'backend/mui/index/render'; + $this->resource = 'Magento_Backend::admin'; + parent::setUp(); + } + + public function testAclNoAccess() + { + } + + protected function getMuiRenderData(string $uiComponentName): array + { + $this->getRequest() + ->getHeaders() + ->addHeaderLine('Accept', 'application/json'); + + $url = 'backend/mui/index/render/?namespace=' . $uiComponentName . '&search=&isAjax=true'; + $this->dispatch($url); + + $response = $this->getResponse(); + $data = json_decode($response->getBody(), true); + + $this->assertNotEmpty($data); + return $data; + } +} diff --git a/Test/Integration/Traits/Adminhtml/AssertAbstractBackendControllerTestCase.php b/Test/Integration/Traits/Adminhtml/AssertAbstractBackendControllerTestCase.php new file mode 100644 index 0000000..a918d48 --- /dev/null +++ b/Test/Integration/Traits/Adminhtml/AssertAbstractBackendControllerTestCase.php @@ -0,0 +1,13 @@ +assertInstanceOf(AbstractBackendController::class, $this); + } +} diff --git a/Test/Integration/Traits/Adminhtml/AssertAclResourceExists.php b/Test/Integration/Traits/Adminhtml/AssertAclResourceExists.php new file mode 100644 index 0000000..18039c5 --- /dev/null +++ b/Test/Integration/Traits/Adminhtml/AssertAclResourceExists.php @@ -0,0 +1,18 @@ +get(Builder::class); + $aclBuilder->resetRuntimeAcl(); + $acl = $aclBuilder->getAcl(); + $msg = 'No ACL "' . $aclResourceId . '" found. Existing resources: ' . implode(', ', $acl->getResources()); + $this->assertTrue($acl->has($aclResourceId), $msg); + } +} diff --git a/composer.json b/composer.json index e7a9e28..7b5745f 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "yireo/magento2-integration-test-helper", "license": "OSL-3.0", - "version": "0.0.4", + "version": "0.0.5", "type": "magento2-module", "homepage": "https://www.yireo.com/software/magento-extensions/", "description": "Magento 2 module to support integration tests in other modules",