Skip to content

Commit

Permalink
Add tests for CompatController::createSortControl()
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jan 22, 2025
1 parent 17675cc commit 71644a1
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
120 changes: 120 additions & 0 deletions tests/CompatControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace ipl\Tests\Web;

use Icinga\Web\UrlParams;
use InvalidArgumentException;
use ipl\Orm\Model;
use ipl\Orm\Query;
use ipl\Web\Compat\CompatController;
use Zend_Controller_Response_Abstract;
use Zend_Controller_Request_Abstract;

/**
* @runTestsInSeparateProcesses
*/
class CompatControllerTest extends TestCase
{
protected $controller;

protected $query;

public function setUp(): void
{
class_alias('ipl\Tests\Web\SortControl', 'ipl\Web\Control\SortControl');

$this->controller = new class extends CompatController {

protected $params;

public function __construct(
Zend_Controller_Request_Abstract $request = null,
Zend_Controller_Response_Abstract $response = null,
array $invokeArgs = []
) {
$this->params = new UrlParams();
}
};

$self = (new self());
$model = $self->createMock(Model::class);
$model->method('getDefaultSort')->willReturn(['age']);

$this->query = $self->createMock(Query::class);
$this->query->method('getModel')->willReturn($model);
}

public function testCreateSortControlUsesDefaultSortFromModel(): void
{
$sortControl = $this->controller->createSortControl(
$this->query,
[
'name' => 'Name',
'age' => 'Age',
'city' => 'City'
]
);

$this->assertSame('age', $sortControl->getDefault());
}

public function testCreateSortControlUsesDefaultSortFromModelWhichIsNotPresentInProvidedColumns(): void
{
$sortControl = $this->controller->createSortControl(
$this->query,
[
'name' => 'Name',
'surname' => 'Surname',
'city' => 'City'
]
);

$this->assertSame('age', $sortControl->getDefault());
}

public function testCreateSortControlUsesProvidedThirdParamAsString(): void
{
$sortControl = $this->controller->createSortControl(
$this->query,
[
'name' => 'Name',
'age' => 'age',
'city' => 'City'
],
'city'
);

$this->assertSame('city', $sortControl->getDefault());
}

public function testCreateSortControlUsesProvidedThirdParamAsArray(): void
{
$sortControl = $this->controller->createSortControl(
$this->query,
[
'name' => 'Name',
'age' => 'age',
'city' => 'City'
],
['city']
);

$this->assertSame('city', $sortControl->getDefault());
}

public function testCreateSortControlThrowsExceptionWhenProvidedThirdParamIsNotPresentInProvidedColumns(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid default sort "car" given');

$this->controller->createSortControl(
$this->query,
[
'name' => 'Name',
'age' => 'age',
'city' => 'City'
],
['car']
);
}
}
71 changes: 71 additions & 0 deletions tests/SortControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace ipl\Tests\Web;

use ipl\Orm\Common\SortUtil;

/**
* Imitates the behavior of {@see \ipl\Web\Control\SortControl} class
*
* Reduces the {@see self::__construct()} and {@see self::create()} method to only required functionality
*/
class SortControl {

protected $columns = [];

protected $default;

private function __construct(array $columns)
{
$this->setColumns($columns);
}

public static function create(array $options)
{
$normalized = [];
foreach ($options as $spec => $label) {
$normalized[SortUtil::normalizeSortSpec($spec)] = $label;
}

return new static($normalized);
}

public function setColumns($columns)
{
$this->columns = array_change_key_case($columns, CASE_LOWER);

return $this;
}

public function getColumns()
{
return $this->columns;
}

public function getDefault(): ?string
{
return $this->default;
}

public function setDefault(string $default): self
{
// We're working with lowercase keys throughout the sort control
$this->default = strtolower($default);

return $this;
}

public function getSortParam()
{
return '';
}

public function handleRequest()
{
}

public function apply()
{
return $this;
}
}

0 comments on commit 71644a1

Please sign in to comment.