Skip to content

Commit eced07f

Browse files
authored
Merge pull request #5 from devmakerlab/(feat)tests
Add of tests
2 parents f9789b0 + b9591f1 commit eced07f

File tree

5 files changed

+159
-20
lines changed

5 files changed

+159
-20
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/AbstractFilterableRepositoryTest.php renamed to tests/Feature/PeopleRepositoryTest.php

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,35 @@
55
use Tests\TestCase;
66
use Tests\Example\Filters\OldPeopleFilter;
77
use Tests\Example\Entities\PeopleEntityList;
8+
use Illuminate\Foundation\Testing\DatabaseTransactions;
89

9-
class AbstractFilterableRepositoryTest extends TestCase
10+
class PeopleRepositoryTest extends TestCase
1011
{
11-
use \Illuminate\Foundation\Testing\DatabaseTransactions;
12-
13-
protected function setUp(): void
14-
{
15-
parent::setUp();
16-
}
12+
use DatabaseTransactions;
1713

1814
/** */
1915
public function testCan_get_old_people(): void
2016
{
2117
$peopleRepository = $this->instantiatePeopleRepository();
2218

2319
$this->createPeople([
24-
[
25-
'firstname' => 'Snoop',
26-
'lastname' => 'Dogg',
27-
'gender' => 'male',
28-
'age' => 49,
29-
],
30-
[
31-
'firstname' => 'Danielle',
32-
'lastname' => 'Studio',
33-
'gender' => 'female',
34-
'age' => 67,
35-
],
36-
]);
20+
[
21+
'firstname' => 'Snoop',
22+
'lastname' => 'Dogg',
23+
'gender' => 'male',
24+
'age' => 49,
25+
],
26+
[
27+
'firstname' => 'Danielle',
28+
'lastname' => 'Studio',
29+
'gender' => 'female',
30+
'age' => 67,
31+
],
32+
]);
3733

3834
$people = $peopleRepository
3935
->addFilter(OldPeopleFilter::class)
36+
->limit(1)
4037
->get(['age' => 60]);
4138

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

tests/Unit/AbstractFilterTest.php

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

0 commit comments

Comments
 (0)