-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for
CompatController::createSortControl()
- Loading branch information
1 parent
17675cc
commit 71644a1
Showing
2 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |