Commit 9f90296c authored by Tino Goratsch's avatar Tino Goratsch

refactored the \ACP3\Core\Assets\PageCssClasses class and covered it with unit tests

parent fe974e07
......@@ -9,6 +9,10 @@ use ACP3\Core;
*/
class PageCssClasses
{
/**
* @var Core\Helpers\StringFormatter
*/
protected $stringFormatter;
/**
* @var \ACP3\Core\Breadcrumb\Title
*/
......@@ -24,15 +28,18 @@ class PageCssClasses
protected $details;
/**
* @param \ACP3\Core\Breadcrumb\Title $title
* @param Core\Helpers\StringFormatter $stringFormatter
* @param \ACP3\Core\Breadcrumb\Title $title
* @param \ACP3\Core\Http\RequestInterface $request
*/
public function __construct(
Core\Helpers\StringFormatter $stringFormatter,
Core\Breadcrumb\Title $title,
Core\Http\RequestInterface $request
) {
$this->title = $title;
$this->request = $request;
$this->stringFormatter = $stringFormatter;
}
/**
......@@ -57,21 +64,7 @@ class PageCssClasses
public function getDetails()
{
if ($this->details === null) {
$pageTitle = preg_replace(
'=[^a-z0-9\-]=',
'',
\Patchwork\Utf8::toAscii(
html_entity_decode(
str_replace(
' ',
'-',
strtolower($this->title->getPageTitle())
),
ENT_QUOTES,
'UTF-8'
)
)
);
$pageTitle = $this->stringFormatter->makeStringUrlSafe($this->title->getPageTitle());
$this->details = $this->request->getModule() . '-' . $this->request->getController() . '-' . $pageTitle;
}
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Test\Assets;
use ACP3\Core\Assets\PageCssClasses;
use ACP3\Core\Breadcrumb\Title;
use ACP3\Core\Helpers\StringFormatter;
use ACP3\Core\Http\Request;
class PageCssClassesTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PageCssClasses
*/
private $pageCssClasses;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $titleMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $stringFormatterMock;
protected function setUp()
{
$this->setUpMockObjects();
$this->pageCssClasses = new PageCssClasses(
$this->stringFormatterMock,
$this->titleMock,
$this->requestMock
);
}
private function setUpMockObjects()
{
$this->titleMock = $this->getMockBuilder(Title::class)
->disableOriginalConstructor()
->getMock();
$this->requestMock = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();
$this->stringFormatterMock = $this->getMockBuilder(StringFormatter::class)
->disableOriginalConstructor()
->getMock();
}
public function testGetModule()
{
$this->requestMock->expects($this->once())
->method('getModule')
->willReturn('foo');
$this->assertEquals('foo', $this->pageCssClasses->getModule());
}
public function testGetControllerAction()
{
$this->requestMock->expects($this->once())
->method('getModule')
->willReturn('foo');
$this->requestMock->expects($this->once())
->method('getController')
->willReturn('bar');
$this->requestMock->expects($this->once())
->method('getAction')
->willReturn('baz');
$this->assertEquals('foo-bar-baz', $this->pageCssClasses->getControllerAction());
}
public function testGetDetails()
{
$this->requestMock->expects($this->once())
->method('getModule')
->willReturn('foo');
$this->requestMock->expects($this->once())
->method('getController')
->willReturn('bar');
$this->titleMock->expects($this->once())
->method('getPageTitle')
->willReturn('speciäl-chörs_0tßst');
$this->stringFormatterMock->expects($this->once())
->method('makeStringUrlSafe')
->with('speciäl-chörs_0tßst')
->willReturn('speciael-choers-0tssst');
$this->assertEquals('foo-bar-speciael-choers-0tssst', $this->pageCssClasses->getDetails());
}
}
......@@ -43,6 +43,7 @@ HTML;
return [
'german_umlauts' => ['äüöumß', 'aeueoeumss'],
'german_umlauts_source_entities' => ['&auml;&ouml;&szlig;', 'aeoess'],
'underscore' => ['foo_bar', 'foo-bar'],
'complex_characters' => ['ピックアップ', ''],
'preserve_numbers' => ['ピ23ッ6ク7アップ', '23-6-7'],
'to_lower_case' => ['ÄÜÖumß', 'aeueoeumss'],
......
......@@ -45,5 +45,6 @@ services:
core.assets.page_css_classes:
class: ACP3\Core\Assets\PageCssClasses
arguments:
- '@core.helpers.string_formatter'
- '@core.breadcrumb.title'
- '@core.http.request'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment