diff --git a/src/Pagination/OffsetPagination.php b/src/Pagination/OffsetPagination.php index bd9a0e472..b7f74d9c9 100644 --- a/src/Pagination/OffsetPagination.php +++ b/src/Pagination/OffsetPagination.php @@ -238,9 +238,6 @@ public function render(): string } $items = $this->renderItems(); - if ($items === []) { - return ''; - } $result .= implode("\n", $items); diff --git a/tests/Pagination/ImmutableTest.php b/tests/Pagination/ImmutableTest.php index 8c5e6b4a0..80282ea7e 100644 --- a/tests/Pagination/ImmutableTest.php +++ b/tests/Pagination/ImmutableTest.php @@ -15,6 +15,16 @@ final class ImmutableTest extends TestCase public function testOffsetPagination(): void { $offsetPagination = OffsetPagination::widget(); + $this->assertNotSame($offsetPagination, $offsetPagination->addLinkClass('')); + $this->assertNotSame($offsetPagination, $offsetPagination->containerAttributes([])); + $this->assertNotSame($offsetPagination, $offsetPagination->containerTag('div')); + $this->assertNotSame($offsetPagination, $offsetPagination->currentLinkClass('')); + $this->assertNotSame($offsetPagination, $offsetPagination->disabledLinkClass('')); + $this->assertNotSame($offsetPagination, $offsetPagination->labelFirst('')); + $this->assertNotSame($offsetPagination, $offsetPagination->labelLast('')); + $this->assertNotSame($offsetPagination, $offsetPagination->labelNext('')); + $this->assertNotSame($offsetPagination, $offsetPagination->labelPrevious('')); + $this->assertNotSame($offsetPagination, $offsetPagination->linkClass('')); $this->assertNotSame($offsetPagination, $offsetPagination->maxNavLinkCount(10)); } } diff --git a/tests/Pagination/OffsetPaginationTest.php b/tests/Pagination/OffsetPaginationTest.php index 53d85dc0a..1988dda13 100644 --- a/tests/Pagination/OffsetPaginationTest.php +++ b/tests/Pagination/OffsetPaginationTest.php @@ -4,14 +4,17 @@ namespace Yiisoft\Yii\DataView\Tests\Pagination; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Yiisoft\Definitions\Exception\CircularReferenceException; use Yiisoft\Definitions\Exception\InvalidConfigException; use Yiisoft\Definitions\Exception\NotInstantiableException; use Yiisoft\Factory\NotFoundException; -use Yiisoft\Yii\DataView\Pagination\PaginatorNotSetException; use Yiisoft\Yii\DataView\GridView; use Yiisoft\Yii\DataView\Pagination\OffsetPagination; +use Yiisoft\Yii\DataView\Pagination\PaginationContext; +use Yiisoft\Yii\DataView\Pagination\PaginatorNotSetException; +use Yiisoft\Yii\DataView\Pagination\PaginatorNotSupportedException; use Yiisoft\Yii\DataView\Tests\Support\Assert; use Yiisoft\Yii\DataView\Tests\Support\TestTrait; @@ -60,4 +63,361 @@ public function testNotSetPaginator(): void $this->expectExceptionMessage('Failed to create widget because "paginator" is not set.'); Assert::invokeMethod($pagination, 'getPaginator'); } + + public function testThrowExceptionForUnsupportedPaginator(): void + { + $pagination = OffsetPagination::widget(); + $keysetPaginator = $this->createKeysetPaginator([], 10); + + $this->expectException(PaginatorNotSupportedException::class); + $this->expectExceptionMessage(sprintf('Paginator "%s" is not supported.', $keysetPaginator::class)); + + $pagination->withPaginator($keysetPaginator); + } + + public function testThrowExceptionForContainerTagEmptyValue(): void + { + $pagination = OffsetPagination::widget(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Tag name cannot be empty.'); + + $pagination->containerTag(''); + } + + public function testThrowExceptionForListTagEmptyValue(): void + { + $pagination = OffsetPagination::widget(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Tag name cannot be empty.'); + + $pagination->listTag(''); + } + + public function testThrowExceptionForItemTagEmptyValue(): void + { + $pagination = OffsetPagination::widget(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Tag name cannot be empty.'); + + $pagination->itemTag(''); + } + + public function testRenderWithEmptyItems(): void + { + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + OffsetPagination::widget() + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)) + ->render(), + ); + } + + public function testAddLinkClass(): void + { + $offsetPagination = OffsetPagination::widget() + ->addLinkClass('test-class') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->render(), + ); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->addLinkClass('test-class-1', null, 'test-class-2')->render(), + ); + } + + public function testContainerTagWithAttributes(): void + { + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + OffsetPagination::widget() + ->containerTag('div') + ->containerAttributes(['class' => 'test-class']) + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)) + ->render(), + ); + } + + public function testCurrentLinkClass(): void + { + $offsetPagination = OffsetPagination::widget() + ->currentLinkClass('test-class') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->render(), + ); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->currentLinkClass('test-class-1')->render(), + ); + } + + public function testDisabledLinkClass(): void + { + $offsetPagination = OffsetPagination::widget() + ->disabledLinkClass('test-class') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->render(), + ); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->disabledLinkClass('test-class-1')->render(), + ); + } + + public function testLabelFirst(): void + { + Assert::equalsWithoutLE( + << + First + + 1 + + + + HTML, + OffsetPagination::widget() + ->labelFirst('First') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)) + ->render(), + ); + } + + public function testLabelLast(): void + { + Assert::equalsWithoutLE( + << + + + 1 + + Last + + HTML, + OffsetPagination::widget() + ->labelLast('Last') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)) + ->render(), + ); + } + + public function testLabelNext(): void + { + Assert::equalsWithoutLE( + << + + + 1 + Next + + + HTML, + OffsetPagination::widget() + ->labelNext('Next') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)) + ->render(), + ); + } + + public function testLabelPrevious(): void + { + Assert::equalsWithoutLE( + << + + Previous + 1 + + + + HTML, + OffsetPagination::widget() + ->labelPrevious('Previous') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)) + ->render(), + ); + } + + public function testLinkClass(): void + { + $offsetPagination = OffsetPagination::widget() + ->LinkClass('test-class') + ->withContext( + new PaginationContext( + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/?page=' . PaginationContext::URL_PLACEHOLDER, + 'http://example.com/', + ), + ) + ->withPaginator($this->createOffsetPaginator([], 10)); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->render(), + ); + + Assert::equalsWithoutLE( + << + + + 1 + + + + HTML, + $offsetPagination->linkClass('test-class-1')->render(), + ); + } }