-
-
Notifications
You must be signed in to change notification settings - Fork 24
More tests #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
More tests #254
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
231c4e2
More tests
samdark 59f6a03
Apply fixes from StyleCI
StyleCIBot 49f969a
More
samdark 6d88540
Apply fixes from StyleCI
StyleCIBot 70c22f5
More
samdark 62f2861
Apply fixes from StyleCI
StyleCIBot 58de13d
Apply Rector changes (CI)
samdark 07017db
Fix
samdark 1c6cad9
More
samdark 6c40f4c
Apply fixes from StyleCI
StyleCIBot a190105
Fix
samdark 117e526
Fix
samdark a0bb49b
Apply fixes from StyleCI
StyleCIBot 9909aeb
More
samdark f8b4246
Apply fixes from StyleCI
StyleCIBot e0e82f5
Fix
samdark 99723d2
Apply fixes from StyleCI
StyleCIBot 6d8213e
Add annotations
samdark ebbca41
Remove annotation
samdark 201d1c3
Less mocks
samdark 03059ef
Cleanup
samdark 14fe6ca
cleanup
samdark fb9a36d
Cleanup
samdark 7a92951
Cleanup
samdark d1baf9b
Fixes
samdark 9493a52
Merge branch 'master' into more-tests
vjik 53d8645
Fixes and cleanup
samdark f85126f
Remove dummy tests
samdark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\DataView\Tests\Column\Base; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Data\Reader\Iterable\IterableDataReader; | ||
use Yiisoft\Yii\DataView\Column\Base\GlobalContext; | ||
use Yiisoft\Yii\DataView\Tests\Support\Mock; | ||
|
||
/** | ||
* @covers \Yiisoft\Yii\DataView\Column\Base\GlobalContext | ||
*/ | ||
final class GlobalContextTest extends TestCase | ||
{ | ||
public function testConstructor(): void | ||
{ | ||
$data = [ | ||
['id' => 1, 'name' => 'John', 'age' => 20], | ||
['id' => 2, 'name' => 'Mary', 'age' => 21], | ||
]; | ||
$dataReader = new IterableDataReader($data); | ||
$translator = Mock::translator('en'); | ||
|
||
$pathArguments = ['id' => '42']; | ||
$queryParameters = ['sort' => 'name']; | ||
$translationCategory = 'grid'; | ||
|
||
$context = new GlobalContext( | ||
$dataReader, | ||
$pathArguments, | ||
$queryParameters, | ||
$translator, | ||
$translationCategory | ||
); | ||
|
||
$this->assertSame($dataReader, $context->dataReader); | ||
$this->assertSame($pathArguments, $context->pathArguments); | ||
$this->assertSame($queryParameters, $context->queryParameters); | ||
} | ||
|
||
public function testTranslate(): void | ||
{ | ||
$data = [ | ||
['id' => 1, 'name' => 'John', 'age' => 20], | ||
['id' => 2, 'name' => 'Mary', 'age' => 21], | ||
]; | ||
$dataReader = new IterableDataReader($data); | ||
$translator = Mock::translator('en'); | ||
|
||
$context = new GlobalContext( | ||
$dataReader, | ||
[], | ||
[], | ||
$translator, | ||
'grid' | ||
); | ||
|
||
$result = $context->translate('test.message'); | ||
|
||
$this->assertSame('test.message', $result); | ||
} | ||
|
||
public function testTranslateWithStringable(): void | ||
{ | ||
$data = [ | ||
['id' => 1, 'name' => 'John', 'age' => 20], | ||
['id' => 2, 'name' => 'Mary', 'age' => 21], | ||
]; | ||
$dataReader = new IterableDataReader($data); | ||
$translator = Mock::translator('en'); | ||
|
||
$context = new GlobalContext( | ||
$dataReader, | ||
[], | ||
[], | ||
$translator, | ||
'grid' | ||
); | ||
|
||
$stringable = new class () implements \Stringable { | ||
public function __toString(): string | ||
{ | ||
return 'stringable.message'; | ||
} | ||
}; | ||
|
||
$result = $context->translate($stringable); | ||
|
||
$this->assertSame('stringable.message', $result); | ||
} | ||
} |
This file contains hidden or 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,238 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Yii\DataView\Tests\Column\Base; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Data\Paginator\PageToken; | ||
use Yiisoft\Data\Reader\Sort; | ||
use Yiisoft\Html\Tag\A; | ||
use Yiisoft\Translator\TranslatorInterface; | ||
use Yiisoft\Yii\DataView\Column\Base\Cell; | ||
use Yiisoft\Yii\DataView\Column\Base\HeaderContext; | ||
use Yiisoft\Yii\DataView\Tests\Support\Mock; | ||
use Yiisoft\Yii\DataView\UrlConfig; | ||
|
||
/** | ||
* @covers \Yiisoft\Yii\DataView\Column\Base\HeaderContext | ||
* @covers \Yiisoft\Yii\DataView\Column\Base\Cell | ||
* @covers \Yiisoft\Yii\DataView\UrlConfig | ||
* @covers \Yiisoft\Yii\DataView\UrlParametersFactory | ||
*/ | ||
final class HeaderContextTest extends TestCase | ||
{ | ||
public function testTranslate(): void | ||
{ | ||
$translator = Mock::translator('en'); | ||
|
||
$headerContext = $this->createHeaderContext(translator: $translator); | ||
|
||
$result = $headerContext->translate('test.message'); | ||
|
||
$this->assertSame('test.message', $result); | ||
} | ||
|
||
public function testTranslateWithStringable(): void | ||
{ | ||
$translator = Mock::translator('en'); | ||
|
||
$headerContext = $this->createHeaderContext(translator: $translator); | ||
|
||
$stringable = new class () { | ||
public function __toString(): string | ||
{ | ||
return 'Stringable Message'; | ||
} | ||
}; | ||
|
||
$result = $headerContext->translate($stringable); | ||
|
||
$this->assertSame('Stringable Message', $result); | ||
} | ||
|
||
public function testPrepareSortableWithEmptyProperty(): void | ||
{ | ||
$cell = new Cell(); | ||
$headerContext = $this->createHeaderContext(); | ||
|
||
$result = $headerContext->prepareSortable($cell, 'nonexistent'); | ||
|
||
$this->assertSame($cell, $result[0]); | ||
$this->assertNull($result[1]); | ||
$this->assertSame('', $result[2]); | ||
$this->assertSame('', $result[3]); | ||
} | ||
|
||
public function testPrepareSortableWithNullSort(): void | ||
{ | ||
$cell = new Cell(); | ||
$headerContext = $this->createHeaderContext( | ||
sort: null, | ||
originalSort: null | ||
); | ||
|
||
$result = $headerContext->prepareSortable($cell, 'name'); | ||
|
||
$this->assertSame($cell, $result[0]); | ||
$this->assertNull($result[1]); | ||
$this->assertSame('', $result[2]); | ||
$this->assertSame('', $result[3]); | ||
} | ||
|
||
public function testPrepareSortableWithPropertyNotInConfig(): void | ||
{ | ||
$sort = Sort::any(); | ||
$cell = new Cell(); | ||
$headerContext = $this->createHeaderContext( | ||
sort: $sort, | ||
originalSort: $sort, | ||
orderProperties: ['name' => 'name'] | ||
); | ||
|
||
$result = $headerContext->prepareSortable($cell, 'age'); | ||
|
||
$this->assertSame($cell, $result[0]); | ||
$this->assertNull($result[1]); | ||
$this->assertSame('', $result[2]); | ||
$this->assertSame('', $result[3]); | ||
} | ||
|
||
public function testPrepareSortableWithNoOrder(): void | ||
{ | ||
$sort = Sort::any(['name' => []]); | ||
$cell = new Cell(); | ||
$headerContext = $this->createHeaderContext( | ||
sort: $sort, | ||
originalSort: $sort, | ||
orderProperties: ['name' => 'name'], | ||
sortableHeaderClass: 'sortable', | ||
sortableHeaderPrepend: '↕', | ||
sortableHeaderAppend: '!' | ||
); | ||
|
||
$result = $headerContext->prepareSortable($cell, 'name'); | ||
|
||
$this->assertInstanceOf(Cell::class, $result[0]); | ||
$this->assertStringContainsString('sortable', $result[0]->getAttributes()['class']); | ||
$this->assertInstanceOf(A::class, $result[1]); | ||
$this->assertSame('↕', $result[2]); | ||
$this->assertSame('!', $result[3]); | ||
} | ||
|
||
public function testPrepareSortableWithAscOrder(): void | ||
{ | ||
$sort = Sort::any(['name' => []])->withOrder(['name' => 'asc']); | ||
$cell = new Cell(); | ||
$headerContext = $this->createHeaderContext( | ||
sort: $sort, | ||
originalSort: $sort, | ||
orderProperties: ['name' => 'name'], | ||
sortableHeaderAscClass: 'asc', | ||
sortableHeaderAscPrepend: '↑', | ||
sortableHeaderAscAppend: '!', | ||
sortableLinkAscClass: 'link-asc' | ||
); | ||
|
||
$result = $headerContext->prepareSortable($cell, 'name'); | ||
|
||
$this->assertInstanceOf(Cell::class, $result[0]); | ||
$this->assertStringContainsString('asc', $result[0]->getAttributes()['class']); | ||
$this->assertInstanceOf(A::class, $result[1]); | ||
$this->assertSame('↑', $result[2]); | ||
$this->assertSame('!', $result[3]); | ||
} | ||
|
||
public function testPrepareSortableWithDescOrder(): void | ||
{ | ||
$sort = Sort::any(['name' => []])->withOrder(['name' => 'desc']); | ||
$cell = new Cell(); | ||
$headerContext = $this->createHeaderContext( | ||
sort: $sort, | ||
originalSort: $sort, | ||
orderProperties: ['name' => 'name'], | ||
sortableHeaderDescClass: 'desc', | ||
sortableHeaderDescPrepend: '↓', | ||
sortableHeaderDescAppend: '!', | ||
sortableLinkDescClass: 'link-desc' | ||
); | ||
|
||
$result = $headerContext->prepareSortable($cell, 'name'); | ||
|
||
$this->assertInstanceOf(Cell::class, $result[0]); | ||
$this->assertStringContainsString('desc', $result[0]->getAttributes()['class']); | ||
$this->assertInstanceOf(A::class, $result[1]); | ||
$this->assertSame('↓', $result[2]); | ||
$this->assertSame('!', $result[3]); | ||
} | ||
|
||
private function createHeaderContext( | ||
?Sort $sort = null, | ||
?Sort $originalSort = null, | ||
array $orderProperties = ['name' => 'name'], | ||
?string $sortableHeaderClass = null, | ||
string|Stringable $sortableHeaderPrepend = '', | ||
string|Stringable $sortableHeaderAppend = '', | ||
?string $sortableHeaderAscClass = null, | ||
string|Stringable $sortableHeaderAscPrepend = '', | ||
string|Stringable $sortableHeaderAscAppend = '', | ||
?string $sortableHeaderDescClass = null, | ||
string|Stringable $sortableHeaderDescPrepend = '', | ||
string|Stringable $sortableHeaderDescAppend = '', | ||
array $sortableLinkAttributes = [], | ||
?string $sortableLinkAscClass = null, | ||
?string $sortableLinkDescClass = null, | ||
?PageToken $pageToken = null, | ||
?int $pageSize = null, | ||
bool $multiSort = false, | ||
?TranslatorInterface $translator = null | ||
): HeaderContext { | ||
if ($sort === null) { | ||
$sort = Sort::any(); | ||
} | ||
|
||
if ($originalSort === null) { | ||
$originalSort = Sort::any(); | ||
} | ||
|
||
if ($translator === null) { | ||
$translator = Mock::translator('en'); | ||
} | ||
|
||
$urlConfig = new UrlConfig( | ||
pageParameterName: 'page', | ||
previousPageParameterName: 'prev', | ||
pageSizeParameterName: 'per-page', | ||
sortParameterName: 'sort' | ||
); | ||
|
||
$urlCreator = function () { | ||
return '#'; | ||
}; | ||
|
||
return new HeaderContext( | ||
originalSort: $originalSort, | ||
sort: $sort, | ||
orderProperties: $orderProperties, | ||
sortableHeaderClass: $sortableHeaderClass, | ||
sortableHeaderPrepend: $sortableHeaderPrepend, | ||
sortableHeaderAppend: $sortableHeaderAppend, | ||
sortableHeaderAscClass: $sortableHeaderAscClass, | ||
sortableHeaderAscPrepend: $sortableHeaderAscPrepend, | ||
sortableHeaderAscAppend: $sortableHeaderAscAppend, | ||
sortableHeaderDescClass: $sortableHeaderDescClass, | ||
sortableHeaderDescPrepend: $sortableHeaderDescPrepend, | ||
sortableHeaderDescAppend: $sortableHeaderDescAppend, | ||
sortableLinkAttributes: $sortableLinkAttributes, | ||
sortableLinkAscClass: $sortableLinkAscClass, | ||
sortableLinkDescClass: $sortableLinkDescClass, | ||
pageToken: $pageToken, | ||
pageSize: $pageSize, | ||
multiSort: $multiSort, | ||
urlConfig: $urlConfig, | ||
urlCreator: $urlCreator, | ||
translator: $translator, | ||
translationCategory: 'grid' | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.