Skip to content

Commit

Permalink
enhance test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Nov 15, 2024
1 parent 279f619 commit fd9945f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
16 changes: 16 additions & 0 deletions src/Util/Ui/PopupWizardLinkOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,21 @@ public function setLinkText(string $linkText): PopupWizardLinkOptions
return $this;
}

/**
* Set the title of the popup.
*/
public function setPopupTitle(string $string): PopupWizardLinkOptions
{
$this->popupTitle = $string;
return $this;
}

/**
* Set the width of the popup.
*/
public function setWidth(int $width): PopupWizardLinkOptions
{
$this->width = $width;
return $this;
}
}
6 changes: 6 additions & 0 deletions src/Util/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function array(): ArrayUtil
return $this->locator->get(ArrayUtil::class);
}

public function backendUi(): BackendUiUtil
{
return $this->locator->get(BackendUiUtil::class);
}

public function class(): ClassUtil
{
return $this->locator->get(ClassUtil::class);
Expand Down Expand Up @@ -118,6 +123,7 @@ public static function getSubscribedServices()
return [
AccordionUtil::class,
ArrayUtil::class,
BackendUiUtil::class,
ClassUtil::class,
ContainerUtil::class,
DatabaseUtil::class,
Expand Down
62 changes: 37 additions & 25 deletions tests/Util/BackendUiUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function getTestInstance(array $parameters = [], ?MockBuilder $mockBuilde
{
$parameters['routingUtil'] = $parameters['routingUtil'] ?? $this->createMock(RoutingUtil::class);
$parameters['framework'] = $parameters['framework'] ?? $this->createMock(ContaoFramework::class);
$parameters['htmlUtil'] = $parameters['htmlUtil'] ?? $this->createMock(HtmlUtil::class);
$parameters['htmlUtil'] = $parameters['htmlUtil'] ?? new HtmlUtil();

return new BackendUiUtil($parameters['routingUtil'], $parameters['framework'], $parameters['htmlUtil']);
}
Expand All @@ -31,43 +31,53 @@ public function testPopupWizardLinkReturnsCorrectUrlOnly()
$backendUiUtil = $this->getTestInstance(['routingUtil' => $routingUtil]);

$config = new PopupWizardLinkOptions();
$config->urlOnly = true;
$config->setUrlOnly(true);

$result = $backendUiUtil->popupWizardLink(['param' => 'value'], $config);

$this->assertEquals('generated_url', $result);
$this->assertEquals(
'generated_url',
$backendUiUtil->popupWizardLink(['param' => 'value'], $config)
);
}

public function testPopupWizardLinkGeneratesCorrectLink()
{
$routingUtil = $this->createMock(RoutingUtil::class);
$htmlUtil = $this->createMock(HtmlUtil::class);

$routingUtil->method('generateBackendRoute')->willReturn('generated_url');
$htmlUtil->method('generateAttributeString')->willReturn('title="Test Title" style="Test Style" onclick="Test Onclick"');

$backendUiUtil = $this->getTestInstance(['routingUtil' => $routingUtil, 'htmlUtil' => $htmlUtil]);

$config = new PopupWizardLinkOptions();
$config->title = 'Test Title';
$config->style = 'Test Style';
$config->popupTitle = 'Test Popup Title';
$config->width = 800;
$config->linkText = 'Test Link Text';

$result = $backendUiUtil->popupWizardLink(['param' => 'value'], $config);
$backendUiUtil = $this->getTestInstance(['routingUtil' => $routingUtil]);

$this->assertStringContainsString('<a href="generated_url" title="Test Title" style="Test Style" onclick="Test Onclick">Test Link Text</a>', $result);
$config = (new PopupWizardLinkOptions())
->setTitle('Test Title')
->setStyle('border: 0;')
->setPopupTitle('Test Popup Title')
->setWidth(800)
->setLinkText('Test Link Text');
;

$this->assertStringContainsString(
'<a href="generated_url" title="Test Title" style="border: 0;" onclick="Backend.openModalIframe({\'width\':800,\'title\':\'Test Popup Title\',\'url\':this.href});return false">Test Link Text</a>',
$backendUiUtil->popupWizardLink(['param' => 'value'], $config)
);

$GLOBALS['TL_LANG']['tl_content']['edit'][0] = 'Edit';
$config->title = '';
$this->assertStringContainsString(
'<a href="generated_url" title="Edit" style="border: 0;" onclick="Backend.openModalIframe({\'width\':800,\'title\':\'Test Popup Title\',\'url\':this.href});return false">Test Link Text</a>',
$backendUiUtil->popupWizardLink(['param' => 'value'], $config)
);

$config->popupTitle = '';
$this->assertStringContainsString(
'<a href="generated_url" title="Edit" style="border: 0;" onclick="Backend.openModalIframe({\'width\':800,\'title\':\'Edit\',\'url\':this.href});return false">Test Link Text</a>',
$backendUiUtil->popupWizardLink(['param' => 'value'], $config)
);
}

public function testPopupWizardLinkGeneratesLinkWithIcon()
{
$routingUtil = $this->createMock(RoutingUtil::class);
$framework = $this->createMock(ContaoFramework::class);
$htmlUtil = $this->createMock(HtmlUtil::class);

$routingUtil->method('generateBackendRoute')->willReturn('generated_url');
$htmlUtil->method('generateAttributeString')->willReturn('title="Test Title" style="Test Style" onclick="Test Onclick"');

$image = $this->mockAdapter(['getHtml']);
$image->method('getHtml')->willReturn('<img src="alias.svg" alt="Test Title" style="vertical-align:top">');
Expand All @@ -81,9 +91,11 @@ public function testPopupWizardLinkGeneratesLinkWithIcon()
$config->linkText = 'Test Link Text';
$config->icon = 'alias.svg';

$backendUiUtil = new BackendUiUtil($routingUtil, $framework, $htmlUtil);
$result = $backendUiUtil->popupWizardLink(['param' => 'value'], $config);
$backendUiUtil = $this->getTestInstance(['routingUtil' => $routingUtil, 'framework' => $framework]);

$this->assertStringContainsString('<a href="generated_url" title="Test Title" style="Test Style" onclick="Test Onclick"><img src="alias.svg" alt="Test Title" style="vertical-align:top"> Test Link Text</a>', $result);
$this->assertStringContainsString(
'<a href="generated_url" title="Test Title" style="Test Style" onclick="Backend.openModalIframe({\'width\':800,\'title\':\'Test Popup Title\',\'url\':this.href});return false"><img src="alias.svg" alt="Test Title" style="vertical-align:top"> Test Link Text</a>',
$backendUiUtil->popupWizardLink(['param' => 'value'], $config)
);
}
}
9 changes: 9 additions & 0 deletions tests/Util/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace HeimrichHannot\UtilsBundle\Tests\Util;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\Util\BackendUiUtil;
use HeimrichHannot\UtilsBundle\Util\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\Dca\DcaUtil;
use HeimrichHannot\UtilsBundle\Util\Html\HtmlUtil;
Expand Down Expand Up @@ -38,6 +39,9 @@ public function getTestInstance(array $parameter = [])
case ArrayUtil::class:
return $this->createMock(ArrayUtil::class);

case BackendUiUtil::class:
return $this->createMock(BackendUiUtil::class);

case ContainerUtil::class:
return $this->createMock(ContainerUtil::class);

Expand Down Expand Up @@ -86,6 +90,11 @@ public function testArray()
$this->assertInstanceOf(ArrayUtil::class, $this->getTestInstance()->array());
}

public function testBackendUi()
{
$this->assertInstanceOf(BackendUiUtil::class, $this->getTestInstance()->backendUi());
}

public function testContainer()
{
$this->assertInstanceOf(ContainerUtil::class, $this->getTestInstance()->container());
Expand Down

0 comments on commit fd9945f

Please sign in to comment.