-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: add tests for some core classes
- Loading branch information
Showing
9 changed files
with
442 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,28 @@ | ||
<?php | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Fixtures; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class PersistenceSubjectUser implements UserInterface, PersistenceSubjectInterface | ||
{ | ||
public function getRoles(): array | ||
{ | ||
return ['foo', 'bar']; | ||
} | ||
|
||
public function eraseCredentials(): void | ||
{ | ||
} | ||
|
||
public function getUserIdentifier(): string | ||
{ | ||
return 'jane-doe'; | ||
} | ||
|
||
public function getDataTablePersistenceIdentifier(): string | ||
{ | ||
return 'jane-doe-but-different'; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Fixtures; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class SimpleUser implements UserInterface | ||
{ | ||
public function getRoles(): array | ||
{ | ||
return ['foo', 'bar']; | ||
} | ||
|
||
public function eraseCredentials(): void | ||
{ | ||
} | ||
|
||
public function getUserIdentifier(): string | ||
{ | ||
return 'john-doe'; | ||
} | ||
} |
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,155 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Pagination; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Pagination\CurrentPageOutOfRangeException; | ||
use Kreyu\Bundle\DataTableBundle\Pagination\Pagination; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PaginationTest extends TestCase | ||
{ | ||
public function testCurrentPageOutOfRange() | ||
{ | ||
$this->expectException(CurrentPageOutOfRangeException::class); | ||
|
||
new Pagination( | ||
currentPageNumber: 2, | ||
currentPageItemCount: 10, | ||
totalItemCount: 10, | ||
); | ||
} | ||
|
||
public function testGetPageCountWithItemNumber() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 1, | ||
currentPageItemCount: 10, | ||
totalItemCount: 50, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertSame(5, $pagination->getPageCount()); | ||
} | ||
|
||
public function testGetPageCountWithItemNumberPerPageLessThanOne() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 1, | ||
currentPageItemCount: 10, | ||
totalItemCount: 10, | ||
itemNumberPerPage: 0, | ||
); | ||
|
||
$this->assertSame(1, $pagination->getPageCount()); | ||
} | ||
|
||
public function testHasPreviousPage() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 2, | ||
currentPageItemCount: 10, | ||
totalItemCount: 50, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertTrue($pagination->hasPreviousPage()); | ||
|
||
$pagination = new Pagination( | ||
currentPageNumber: 1, | ||
currentPageItemCount: 10, | ||
totalItemCount: 50, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertFalse($pagination->hasPreviousPage()); | ||
} | ||
|
||
public function testHasNextPage() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 4, | ||
currentPageItemCount: 10, | ||
totalItemCount: 50, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertTrue($pagination->hasNextPage()); | ||
|
||
$pagination = new Pagination( | ||
currentPageNumber: 5, | ||
currentPageItemCount: 10, | ||
totalItemCount: 50, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertFalse($pagination->hasNextPage()); | ||
} | ||
|
||
public function testGetFirstVisiblePageNumber() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 10, | ||
currentPageItemCount: 10, | ||
totalItemCount: 250, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertSame(7, $pagination->getFirstVisiblePageNumber()); | ||
|
||
$pagination = new Pagination( | ||
currentPageNumber: 1, | ||
currentPageItemCount: 10, | ||
totalItemCount: 250, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertSame(1, $pagination->getFirstVisiblePageNumber()); | ||
} | ||
|
||
public function testGetLastVisiblePageNumber() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 10, | ||
currentPageItemCount: 10, | ||
totalItemCount: 250, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertSame(13, $pagination->getLastVisiblePageNumber()); | ||
|
||
$pagination = new Pagination( | ||
currentPageNumber: 25, | ||
currentPageItemCount: 10, | ||
totalItemCount: 250, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertSame(25, $pagination->getLastVisiblePageNumber()); | ||
} | ||
|
||
public function testGetCurrentPageFirstItemIndex() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 10, | ||
currentPageItemCount: 10, | ||
totalItemCount: 250, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertSame(91, $pagination->getCurrentPageFirstItemIndex()); | ||
} | ||
|
||
public function testGetCurrentPageLastItemIndex() | ||
{ | ||
$pagination = new Pagination( | ||
currentPageNumber: 10, | ||
currentPageItemCount: 10, | ||
totalItemCount: 250, | ||
itemNumberPerPage: 10, | ||
); | ||
|
||
$this->assertSame(100, $pagination->getCurrentPageLastItemIndex()); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Persistence; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Persistence\CachePersistenceClearer; | ||
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectAggregate; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Contracts\Cache\CacheInterface; | ||
use Symfony\Contracts\Cache\TagAwareCacheInterface; | ||
|
||
class CachePersistenceClearerTest extends TestCase | ||
{ | ||
public function testClear() | ||
{ | ||
$cache = $this->createMock(TagAwareCacheInterface::class); | ||
$cache->expects($this->once())->method('invalidateTags')->with(['kreyu_data_table_persistence_foo']); | ||
|
||
$clearer = new CachePersistenceClearer($cache); | ||
$clearer->clear(new PersistenceSubjectAggregate('foo', new \stdClass())); | ||
} | ||
|
||
public function testClearWithNonTagAwareCache() | ||
{ | ||
$cache = $this->createMock(CacheInterface::class); | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage(sprintf('Cache instance must be an instance of %s', TagAwareCacheInterface::class)); | ||
|
||
$clearer = new CachePersistenceClearer($cache); | ||
$clearer->clear(new PersistenceSubjectAggregate('foo', new \stdClass())); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/Unit/Persistence/StaticPersistenceSubjectProviderTest.php
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Persistence; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectInterface; | ||
use Kreyu\Bundle\DataTableBundle\Persistence\StaticPersistenceSubjectProvider; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class StaticPersistenceSubjectProviderTest extends TestCase | ||
{ | ||
public function testGetDataTablePersistenceIdentifier(): void | ||
{ | ||
$provider = new StaticPersistenceSubjectProvider('foo'); | ||
|
||
$this->assertSame('foo', $provider->getDataTablePersistenceIdentifier()); | ||
} | ||
|
||
public function testProvide(): void | ||
{ | ||
$provider = new StaticPersistenceSubjectProvider('foo'); | ||
|
||
$subject = $provider->provide(); | ||
|
||
$this->assertSame($provider, $subject); | ||
$this->assertInstanceOf(PersistenceSubjectInterface::class, $subject); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/Unit/Persistence/TokenStoragePersistenceSubjectProviderTest.php
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Persistence; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectAggregate; | ||
use Kreyu\Bundle\DataTableBundle\Persistence\PersistenceSubjectNotFoundException; | ||
use Kreyu\Bundle\DataTableBundle\Persistence\TokenStoragePersistenceSubjectProvider; | ||
use Kreyu\Bundle\DataTableBundle\Tests\Fixtures\PersistenceSubjectUser; | ||
use Kreyu\Bundle\DataTableBundle\Tests\Fixtures\SimpleUser; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class TokenStoragePersistenceSubjectProviderTest extends TestCase | ||
{ | ||
public function testProvideWithSimpleUser() | ||
{ | ||
$subject = $this->createProvider($user = new SimpleUser())->provide(); | ||
|
||
$this->assertInstanceOf(PersistenceSubjectAggregate::class, $subject); | ||
$this->assertSame($user, $subject->getSubject()); | ||
$this->assertSame('john-doe', $subject->getDataTablePersistenceIdentifier()); | ||
} | ||
|
||
public function testProvideWithPersistenceSubjectUser() | ||
{ | ||
$subject = $this->createProvider($user = new PersistenceSubjectUser())->provide(); | ||
|
||
$this->assertInstanceOf(PersistenceSubjectAggregate::class, $subject); | ||
$this->assertSame($user, $subject->getSubject()); | ||
$this->assertSame('jane-doe-but-different', $subject->getDataTablePersistenceIdentifier()); | ||
} | ||
|
||
public function testProvideWithNoUser() | ||
{ | ||
$this->expectException(PersistenceSubjectNotFoundException::class); | ||
|
||
$this->createProvider()->provide()->getDataTablePersistenceIdentifier(); | ||
} | ||
|
||
private function createProvider(?UserInterface $user = null): TokenStoragePersistenceSubjectProvider | ||
{ | ||
$tokenStorage = $this->createMock(TokenStorageInterface::class); | ||
|
||
if (null !== $user) { | ||
$token = $this->createMock(TokenInterface::class); | ||
$token->method('getUser')->willReturn($user); | ||
|
||
$tokenStorage->method('getToken')->willReturn($token); | ||
} | ||
|
||
return new TokenStoragePersistenceSubjectProvider($tokenStorage); | ||
} | ||
} | ||
|
||
|
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Kreyu\Bundle\DataTableBundle\Tests\Unit\Query; | ||
|
||
use Kreyu\Bundle\DataTableBundle\Query\ResultSet; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ResultSetTest extends TestCase | ||
{ | ||
public function testGetTotalItemCount(): void | ||
{ | ||
$resultSet = new ResultSet(new \ArrayIterator(), 10, 20); | ||
|
||
$this->assertSame(20, $resultSet->getTotalItemCount()); | ||
} | ||
|
||
public function testGetTotalItemCountWhenNotSet(): void | ||
{ | ||
$resultSet = new ResultSet(new \ArrayIterator(), 10); | ||
|
||
$this->assertSame(10, $resultSet->getTotalItemCount()); | ||
} | ||
|
||
public function testCount(): void | ||
{ | ||
$resultSet = new ResultSet(new \ArrayIterator(), 10, 20); | ||
|
||
$this->assertSame(10, $resultSet->count()); | ||
} | ||
|
||
public function testCountWhenCurrentPageItemCountNotSet(): void | ||
{ | ||
$resultSet = new ResultSet(new \ArrayIterator(), null, 20); | ||
|
||
$this->assertSame(20, $resultSet->count()); | ||
} | ||
} |
Oops, something went wrong.