Skip to content

Commit 1a56e4d

Browse files
committed
Add of tests
1 parent 073379d commit 1a56e4d

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor/
2+
tests/reports/
23
composer.lock
34
.php-cs-fixer.cache

phpunit.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<include>
1818
<directory suffix=".php">src/</directory>
1919
</include>
20+
<report>
21+
<html outputDirectory="tests/reports/report" lowUpperBound="35" highLowerBound="70"/>
22+
</report>
2023
</coverage>
2124
<testsuites>
2225
<testsuite name="Unit">
@@ -26,6 +29,9 @@
2629
<directory suffix="Test.php">./tests/Feature</directory>
2730
</testsuite>
2831
</testsuites>
32+
<logging>
33+
<testdoxText outputFile="tests/reports/testdox.txt"/>
34+
</logging>
2935
<php>
3036
<env name="DB_CONNECTION" value="testing"/>
3137
<env name="APP_KEY" value="base64:bWVpbmRlci1hL2ZpbHRlcnMKc3RhciBpdCE="/>

tests/Feature/PeopleRepositoryTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Tests\Example\Entities\PeopleEntityList;
88
use Illuminate\Foundation\Testing\DatabaseTransactions;
99

10-
class AbstractFilterableRepositoryTest extends TestCase
10+
class PeopleRepositoryTest extends TestCase
1111
{
1212
use DatabaseTransactions;
1313

@@ -33,6 +33,7 @@ public function testCan_get_old_people(): void
3333

3434
$people = $peopleRepository
3535
->addFilter(OldPeopleFilter::class)
36+
->limit(1)
3637
->get(['age' => 60]);
3738

3839
$this->assertInstanceOf(PeopleEntityList::class, $people);

tests/Unit/AbstractFilterTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tests\TestCase;
6+
use DevMakerLab\LaravelFilters\AbstractFilter;
7+
use DevMakerLab\LaravelFilters\NoApplyFilterRuleException;
8+
9+
class AbstractFilterTest extends TestCase
10+
{
11+
public function testExceptionThrownWhenNoApplyFunctionOverride(): void
12+
{
13+
$filter = new class extends AbstractFilter {};
14+
15+
$this->expectException(NoApplyFilterRuleException::class);
16+
17+
$filter->apply($this->app['db']->query());
18+
}
19+
20+
public function testCanFeedPropertiesByConstruct(): void
21+
{
22+
$filter = new class extends AbstractFilter {
23+
public string $foo;
24+
};
25+
26+
$filterInstance = new $filter(['foo' => 'hey', 'bar' => 'nope']);
27+
28+
$this->assertSame('hey', $filterInstance->foo);
29+
$this->expectException(Exception::class);
30+
$this->assertNull($filterInstance->bar);
31+
}
32+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Tests\TestCase;
6+
use DevMakerLab\LaravelFilters\AbstractFilter;
7+
use DevMakerLab\LaravelFilters\FilterClassNotFound;
8+
use DevMakerLab\LaravelFilters\IncorrectFilterException;
9+
use DevMakerLab\LaravelFilters\AbstractFilterableRepository;
10+
11+
class AbstractFilterableRepositoryTest extends TestCase
12+
{
13+
public function testExceptionThrownWhenAddingNonExistingFilter(): void
14+
{
15+
$abstractRepository = new class extends AbstractFilterableRepository {};
16+
17+
$this->expectException(FilterClassNotFound::class);
18+
$abstractRepository->addFilter(FooFilter::class);
19+
}
20+
21+
public function testExceptionThrownWhenAddingFilterWhichIsNotExtendingAbstractFilter(): void
22+
{
23+
$abstractRepository = new class extends AbstractFilterableRepository {};
24+
$filter = new class {};
25+
26+
$this->expectException(IncorrectFilterException::class);
27+
$abstractRepository->addFilter(get_class($filter));
28+
}
29+
30+
public function testCanAddFilter(): void
31+
{
32+
$abstractRepository = new class extends AbstractFilterableRepository {
33+
public function getFilters(): array
34+
{
35+
return $this->filters;
36+
}
37+
};
38+
39+
$filter = new class extends AbstractFilter {};
40+
41+
$abstractRepository->addFilter(get_class($filter));
42+
43+
$this->assertCount(1, $abstractRepository->getFilters());
44+
$this->assertSame(get_class($filter), $abstractRepository->getFilters()[0]);
45+
}
46+
47+
public function testCanResetFilters(): void
48+
{
49+
$abstractRepository = new class extends AbstractFilterableRepository {
50+
public function getFilters(): array
51+
{
52+
return $this->filters;
53+
}
54+
};
55+
56+
$filter = new class extends AbstractFilter {};
57+
58+
$abstractRepository->addFilter(get_class($filter));
59+
$this->assertCount(1, $abstractRepository->getFilters());
60+
61+
$abstractRepository->resetFilters();
62+
$this->assertCount(0, $abstractRepository->getFilters());
63+
}
64+
65+
public function testCanSpecifyLimit(): void
66+
{
67+
$abstractRepository = new class extends AbstractFilterableRepository {
68+
public function getLimit(): ?int
69+
{
70+
return $this->limit;
71+
}
72+
};
73+
74+
$this->assertNull($abstractRepository->getLimit());
75+
76+
$abstractRepository->limit(10);
77+
$this->assertSame(10, $abstractRepository->getLimit());
78+
}
79+
80+
public function testCanResetLimit(): void
81+
{
82+
$abstractRepository = new class extends AbstractFilterableRepository {
83+
public function getLimit(): ?int
84+
{
85+
return $this->limit;
86+
}
87+
};
88+
89+
$this->assertNull($abstractRepository->getLimit());
90+
91+
$abstractRepository->limit(10);
92+
$this->assertSame(10, $abstractRepository->getLimit());
93+
94+
$abstractRepository->resetLimit();
95+
$this->assertNull($abstractRepository->getLimit());
96+
}
97+
}

0 commit comments

Comments
 (0)