-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various helpers & assertions for UiComponents
- Loading branch information
1 parent
6c7d1c3
commit bbe2d1c
Showing
5 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
Test/Integration/Adminhtml/AbstractBackendControllerTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\IntegrationTestHelper\Test\Integration\Adminhtml; | ||
|
||
use Magento\Framework\App\ObjectManager; | ||
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProviderInterface; | ||
use Magento\Framework\View\Element\UiComponentFactory; | ||
use Magento\Framework\View\Element\UiComponentInterface; | ||
use Magento\TestFramework\TestCase\AbstractBackendController; | ||
use Magento\Ui\Component\DataSource; | ||
use Magento\Ui\Component\Form as UiComponentForm; | ||
use Magento\Ui\Component\Listing as UiComponentListing; | ||
use Yireo\IntegrationTestHelper\Test\Integration\Traits\Adminhtml\AssertAclResourceExists; | ||
|
||
/** | ||
* @magentoAppArea adminhtml | ||
*/ | ||
class AbstractBackendControllerTestCase extends AbstractBackendController | ||
{ | ||
use AssertAclResourceExists; | ||
|
||
protected function getUiComponent(string $uiComponentName): UiComponentInterface | ||
{ | ||
$uiComponentFactory = ObjectManager::getInstance()->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; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
Test/Integration/Adminhtml/MuiBackendControllerTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\IntegrationTestHelper\Test\Integration\Adminhtml; | ||
|
||
class MuiBackendControllerTestCase extends AbstractBackendControllerTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
$this->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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Test/Integration/Traits/Adminhtml/AssertAbstractBackendControllerTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\IntegrationTestHelper\Test\Integration\Traits\Adminhtml; | ||
|
||
use Magento\TestFramework\TestCase\AbstractBackendController; | ||
|
||
trait AssertAbstractBackendControllerTestCase | ||
{ | ||
private function assertAbstractBackendControllerTestCase() | ||
{ | ||
$this->assertInstanceOf(AbstractBackendController::class, $this); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Test/Integration/Traits/Adminhtml/AssertAclResourceExists.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Yireo\IntegrationTestHelper\Test\Integration\Traits\Adminhtml; | ||
|
||
use Magento\Framework\Acl\Builder; | ||
use Magento\Framework\App\ObjectManager; | ||
|
||
trait AssertAclResourceExists | ||
{ | ||
public function assertAclResourceExists(string $aclResourceId) | ||
{ | ||
$aclBuilder = ObjectManager::getInstance()->get(Builder::class); | ||
$aclBuilder->resetRuntimeAcl(); | ||
$acl = $aclBuilder->getAcl(); | ||
$msg = 'No ACL "' . $aclResourceId . '" found. Existing resources: ' . implode(', ', $acl->getResources()); | ||
$this->assertTrue($acl->has($aclResourceId), $msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters