Skip to content

Commit

Permalink
Various helpers & assertions for UiComponents
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Sep 13, 2022
1 parent 6c7d1c3 commit bbe2d1c
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
67 changes: 67 additions & 0 deletions Test/Integration/Adminhtml/AbstractBackendControllerTestCase.php
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 Test/Integration/Adminhtml/MuiBackendControllerTestCase.php
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;
}
}
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 Test/Integration/Traits/Adminhtml/AssertAclResourceExists.php
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);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit bbe2d1c

Please sign in to comment.