Passed
Push — release/2.0 ( 96da48 )
by Josh
06:23
created

FilterAssertions   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 69
ccs 23
cts 23
cp 1
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertHasNoFilters() 0 5 1
A assertHasValidFilters() 0 12 1
A assertHasFilter() 0 9 1
A assertFilterMissing() 0 9 1
1
<?php
2
3
namespace JoshGaber\NovaUnit\Traits;
4
5
use Illuminate\Http\Request;
6
use JoshGaber\NovaUnit\Constraints\ArrayHasInstanceOf;
7
use Laravel\Nova\Filters\Filter;
8
use PHPUnit\Framework\Assert as PHPUnit;
9
use PHPUnit\Framework\Constraint\IsType;
10
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
11
12
trait FilterAssertions
13
{
14
    /**
15
     * Asserts that this component has the specified field.
16
     *
17
     * @param string $action The class path of the Filter
18
     * @param string $message
19
     * @return $this
20
     */
21 2
    public function assertHasFilter(string $action, string $message = ''): self
22
    {
23 2
        PHPUnit::assertThat(
24 2
            $this->component->filters(Request::createFromGlobals()),
25 2
            new ArrayHasInstanceOf($action),
26 2
            $message
27
        );
28
29 1
        return $this;
30
    }
31
32
    /**
33
     * Asserts that this component does not have the specified field.
34
     *
35
     * @param string $action The class path of the Filter
36
     * @param string $message
37
     * @return $this
38
     */
39 2
    public function assertFilterMissing(string $action, string $message = ''): self
40
    {
41 2
        PHPUnit::assertThat(
42 2
            $this->component->filters(Request::createFromGlobals()),
43 2
            PHPUnit::logicalNot(new ArrayHasInstanceOf($action)),
44 2
            $message
45
        );
46
47 1
        return $this;
48
    }
49
50
    /**
51
     * Asserts that this component has no Filters specified.
52
     *
53
     * @param string $message
54
     * @return $this
55
     */
56 2
    public function assertHasNoFilters(string $message = ''): self
57
    {
58 2
        PHPUnit::assertCount(0, $this->component->filters(Request::createFromGlobals()), $message);
59
60 1
        return $this;
61
    }
62
63
    /**
64
     * Asserts that all filters on this component are valid Filters.
65
     *
66
     * @param string $message
67
     * @return $this
68
     */
69 3
    public function assertHasValidFilters(string $message = ''): self
70
    {
71 3
        PHPUnit::assertThat(
72 3
            $this->component->filters(Request::createFromGlobals()),
73 3
            PHPUnit::logicalAnd(
74 3
                new IsType(IsType::TYPE_ARRAY),
75 3
                new TraversableContainsOnly(Filter::class, false)
76
            ),
77 3
            $message
78
        );
79
80 1
        return $this;
81
    }
82
}
83