From 71644a14f3885c64a4c7e46eab07b2860599c57b Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Wed, 22 Jan 2025 10:58:55 +0100 Subject: [PATCH] Add tests for `CompatController::createSortControl()` --- tests/CompatControllerTest.php | 120 +++++++++++++++++++++++++++++++++ tests/SortControl.php | 71 +++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 tests/CompatControllerTest.php create mode 100644 tests/SortControl.php diff --git a/tests/CompatControllerTest.php b/tests/CompatControllerTest.php new file mode 100644 index 00000000..0bd01ab9 --- /dev/null +++ b/tests/CompatControllerTest.php @@ -0,0 +1,120 @@ +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'] + ); + } +} diff --git a/tests/SortControl.php b/tests/SortControl.php new file mode 100644 index 00000000..6b7c36e6 --- /dev/null +++ b/tests/SortControl.php @@ -0,0 +1,71 @@ +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; + } +}