Skip to content

Commit

Permalink
tests: add tests for some core classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kreyu committed Mar 15, 2024
1 parent 0db8752 commit 4ea8ab0
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/Fixtures/PersistenceSubjectUser.php
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';
}
}
22 changes: 22 additions & 0 deletions tests/Fixtures/SimpleUser.php
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';
}
}
155 changes: 155 additions & 0 deletions tests/Unit/Pagination/PaginationTest.php
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());
}
}
34 changes: 34 additions & 0 deletions tests/Unit/Persistence/CachePersistenceClearerTest.php
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 tests/Unit/Persistence/StaticPersistenceSubjectProviderTest.php
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);
}
}
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);
}
}


39 changes: 39 additions & 0 deletions tests/Unit/Query/ResultSetTest.php
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());
}
}
Loading

0 comments on commit 4ea8ab0

Please sign in to comment.