Commit f6a568ff authored by Tino Goratsch's avatar Tino Goratsch

increased the unit tests coverage for the breadcrumb Steps class + minor code refactoring

parent a7b8ede1
......@@ -64,8 +64,7 @@ class Steps
RequestInterface $request,
RouterInterface $router,
EventDispatcherInterface $eventDispatcher
)
{
) {
$this->container = $container;
$this->translator = $translator;
$this->request = $request;
......@@ -73,26 +72,6 @@ class Steps
$this->eventDispatcher = $eventDispatcher;
}
/**
* Ersetzt die aktuell letzte Brotkrume mit neuen Werten
*
* @param string $title
* @param string $path
* @param bool $dbSteps
*
* @return $this
*/
public function replaceAncestor($title, $path = '', $dbSteps = false)
{
if ($dbSteps === false) {
$index = count($this->steps) - (!empty($this->steps) ? 1 : 0);
$this->steps[$index]['title'] = $title;
$this->steps[$index]['uri'] = !empty($path) ? $this->router->route($path) : '';
}
return $this;
}
/**
* Returns the breadcrumb
*
......@@ -143,7 +122,7 @@ class Steps
);
$this->appendControllerActionBreadcrumbs();
} else { // Prepend breadcrumb steps, if there have already been some steps set
} else {
$this->prepend(
$this->translator->t($this->request->getModule(), $this->request->getModule()),
'acp/' . $this->request->getModule()
......@@ -220,6 +199,38 @@ class Steps
$this->breadcrumbCache = $this->steps;
}
/**
* Ersetzt die aktuell letzte Brotkrume mit neuen Werten
*
* @param string $title
* @param string $path
* @param bool $dbSteps
*
* @return $this
*/
public function replaceAncestor($title, $path = '', $dbSteps = false)
{
if ($dbSteps === false) {
end($this->steps);
$this->steps[key($this->steps)] = $this->buildStepItem($title, $path);
}
return $this;
}
/**
* @param string $title
* @param string $path
* @return array
*/
protected function buildStepItem($title, $path)
{
return [
'title' => $title,
'uri' => !empty($path) ? $this->router->route($path) : ''
];
}
/**
* Zuweisung einer neuen Stufe zur Brotkrümelspur
*
......@@ -231,10 +242,7 @@ class Steps
public function append($title, $path = '')
{
if (!$this->stepAlreadyExists($path)) {
$this->steps[] = [
'title' => $title,
'uri' => !empty($path) ? $this->router->route($path) : ''
];
$this->steps[] = $this->buildStepItem($title, $path);
}
return $this;
......@@ -251,10 +259,7 @@ class Steps
public function prepend($title, $path)
{
if (!$this->stepAlreadyExists($path)) {
$step = [
'title' => $title,
'uri' => $this->router->route($path)
];
$step = $this->buildStepItem($title, $path);
array_unshift($this->steps, $step);
}
......
......@@ -106,13 +106,13 @@ class StepsTest extends \PHPUnit_Framework_TestCase
$this->requestMock->expects($this->atLeastOnce())
->method('getArea')
->willReturn($area);
$this->requestMock->expects($this->atLeastOnce())
$this->requestMock->expects($this->any())
->method('getModule')
->willReturn($moduleName);
$this->requestMock->expects($this->atLeastOnce())
$this->requestMock->expects($this->any())
->method('getController')
->willReturn($controller);
$this->requestMock->expects($this->atLeastOnce())
$this->requestMock->expects($this->any())
->method('getAction')
->willReturn($action);
$this->requestMock->expects($this->any())
......@@ -143,9 +143,9 @@ class StepsTest extends \PHPUnit_Framework_TestCase
});
}
private function setUpTranslatorMockExpectations()
private function setUpTranslatorMockExpectations($callCount = 1)
{
$this->translatorMock->expects($this->atLeastOnce())
$this->translatorMock->expects($this->atLeast($callCount))
->method('t')
->willReturnCallback(function($module, $phrase) {
return strtoupper ('{' . $module . '_' . $phrase . '}');
......@@ -181,33 +181,163 @@ class StepsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->steps->getBreadcrumb());
}
// public function testGetBreadcrumbForAdminWithExistingSteps()
// {
// $this->markTestSkipped('To be implemented');
// }
//
// public function testGetBreadcrumbForFrontendControllerIndex()
// {
// $this->markTestSkipped('To be implemented');
// }
//
// public function testGetBreadcrumbForFrontendController()
// {
// $this->markTestSkipped('To be implemented');
// }
//
// public function testGetBreadcrumbForFrontendWithExistingSteps()
// {
// $this->markTestSkipped('To be implemented');
// }
//
// public function testAddMultipleSameSteps()
// {
// $this->markTestSkipped('To be implemented');
// }
//
// public function testReplaceAncestor()
// {
// $this->markTestSkipped('To be implemented');
// }
public function testGetBreadcrumbForAdminWithExistingSteps()
{
$this->setUpRequestMockExpectations(
AreaEnum::AREA_ADMIN,
'foo',
'details',
'index'
);
$this->setUpRouterMockExpectations();
$this->setUpTranslatorMockExpectations();
$this->steps->append('FooBarBaz', 'acp/foo/bar/baz');
$expected = [
[
'title' => '{FOO_FOO}',
'uri' => '/acp/foo',
],
[
'title' => 'FooBarBaz',
'uri' => '/acp/foo/bar/baz',
'last' => true
]
];
$this->assertEquals($expected, $this->steps->getBreadcrumb());
}
public function testGetBreadcrumbForFrontendControllerIndex()
{
$this->setUpRequestMockExpectations(
AreaEnum::AREA_FRONTEND,
'foo',
'index',
'index'
);
$this->setUpRouterMockExpectations();
$this->setUpTranslatorMockExpectations();
$expected = [
[
'title' => '{FOO_FOO}',
'uri' => '/foo',
'last' => true
]
];
$this->assertEquals($expected, $this->steps->getBreadcrumb());
}
public function testGetBreadcrumbForFrontendController()
{
$this->setUpContainerMockExpectations(
'foo.controller.frontend.details.index',
true
);
$this->setUpRequestMockExpectations(
AreaEnum::AREA_FRONTEND,
'foo',
'details',
'index'
);
$this->setUpRouterMockExpectations();
$this->setUpTranslatorMockExpectations();
$expected = [
[
'title' => '{FOO_FOO}',
'uri' => '/foo',
],
[
'title' => '{FOO_FRONTEND_DETAILS_INDEX}',
'uri' => '/foo/details',
'last' => true
]
];
$this->assertEquals($expected, $this->steps->getBreadcrumb());
}
public function testGetBreadcrumbForFrontendWithExistingSteps()
{
$this->setUpRequestMockExpectations(
AreaEnum::AREA_FRONTEND,
'foo',
'details',
'index'
);
$this->setUpRouterMockExpectations();
$this->setUpTranslatorMockExpectations(0);
$this->steps->append('FooBarBaz', 'foo/bar/baz');
$expected = [
[
'title' => 'FooBarBaz',
'uri' => '/foo/bar/baz',
'last' => true
]
];
$this->assertEquals($expected, $this->steps->getBreadcrumb());
}
public function testAddMultipleSameSteps()
{
$this->setUpRequestMockExpectations(
AreaEnum::AREA_FRONTEND,
'foo',
'details',
'index'
);
$this->setUpRouterMockExpectations();
$this->setUpTranslatorMockExpectations(0);
$this->steps->append('FooBarBaz', 'foo/bar/baz');
$this->steps->append('FooBarBaz', 'foo/bar/baz');
$this->steps->append('FooBarBaz', 'foo/bar/baz');
$expected = [
[
'title' => 'FooBarBaz',
'uri' => '/foo/bar/baz',
'last' => true
]
];
$this->assertEquals($expected, $this->steps->getBreadcrumb());
}
public function testReplaceAncestor()
{
$this->setUpRequestMockExpectations(
AreaEnum::AREA_FRONTEND,
'foo',
'details',
'index'
);
$this->setUpRouterMockExpectations();
$this->setUpTranslatorMockExpectations(0);
$this->steps->append('FooBarBaz', 'foo/bar/baz');
$this->steps->append('FooBarBaz2', 'foo/bar/baz2');
$this->steps->append('FooBarBaz3', 'foo/bar/baz3');
$this->steps->replaceAncestor('Lorem Ipsum', 'lorem/ipsum/dolor');
$expected = [
[
'title' => 'FooBarBaz',
'uri' => '/foo/bar/baz',
],
[
'title' => 'FooBarBaz2',
'uri' => '/foo/bar/baz2',
],
[
'title' => 'Lorem Ipsum',
'uri' => '/lorem/ipsum/dolor',
'last' => true
],
];
$this->assertEquals($expected, $this->steps->getBreadcrumb());
}
}
......@@ -86,10 +86,7 @@ class Steps extends Core\Breadcrumb\Steps
*/
protected function appendFromDB($title, $path = '')
{
$this->stepsFromDb[] = [
'title' => $title,
'uri' => !empty($path) ? $this->router->route($path) : ''
];
$this->stepsFromDb[] = $this->buildStepItem($title, $path);
return $this;
}
......@@ -100,9 +97,8 @@ class Steps extends Core\Breadcrumb\Steps
public function replaceAncestor($title, $path = '', $dbSteps = false)
{
if ($dbSteps === true) {
$index = count($this->stepsFromDb) - (!empty($this->stepsFromDb) ? 1 : 0);
$this->stepsFromDb[$index]['title'] = $title;
$this->stepsFromDb[$index]['uri'] = !empty($path) ? $this->router->route($path) : '';
end($this->stepsFromDb);
$this->stepsFromDb[key($this->stepsFromDb)] = $this->buildStepItem($title, $path);
}
return parent::replaceAncestor($title, $path, $dbSteps);
......
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