Issues (26)

src/Checks/CollectionCheck.php (1 issue)

Labels
Severity
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Checks;
5
6
use PHPUnit\Framework\Assert;
7
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck;
8
use Pitchart\Phlunit\Checks\Mixin\FluentChecks;
9
use Pitchart\Phlunit\Checks\Mixin\TypeCheck;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Pitchart\Phlunit\Checks\TypeCheck. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use Pitchart\Phlunit\Checks\Mixin\WithMessage;
11
use Pitchart\Phlunit\Constraint\Arrays\ContainsExactly;
12
use Pitchart\Phlunit\Constraint\Arrays\ContainsNoDuplicateItem;
13
use Pitchart\Phlunit\Constraint\Arrays\ContainsSet;
14
use Pitchart\Phlunit\Constraint\Arrays\IsSubset;
15
16
/**
17
 * Class CollectionCheck
18
 *
19
 * @package Pitchart\Phlunit\Checks
20
 *
21
 * @author Julien VITTE <[email protected]>
22
 *
23
 * @implements FluentCheck<iterable>
24
 */
25
class CollectionCheck implements FluentCheck
26
{
27 1
    use TypeCheck, FluentChecks, ConstraintCheck, WithMessage;
28
29
    /**
30
     * @var iterable
31
     */
32
    protected $value;
33
34
    /**
35
     * CollectionCheck constructor.
36
     *
37
     * @template T of iterable
38
     * @param iterable $actual
39
     */
40 42
    public function __construct(iterable $collection)
41
    {
42 42
        $this->value = $collection;
43 42
    }
44
45 1
    public function isEmpty(): self
46
    {
47 1
        Assert::assertEmpty($this->value, $this->message);
48 1
        $this->resetMessage();
49 1
        return $this;
50
    }
51
52 1
    public function isNotEmpty(): self
53
    {
54 1
        Assert::assertNotEmpty($this->value, $this->message);
55 1
        $this->resetMessage();
56 1
        return $this;
57
    }
58
59 2
    public function isACollectionOf(string $type): self
60
    {
61 2
        Assert::assertContainsOnly($type, $this->value, null, $this->message);
62 2
        $this->resetMessage();
63 2
        return $this;
64
    }
65
66 3
    public function containsOnlyInstancesOf(string $className): self
67
    {
68 3
        Assert::assertContainsOnlyInstancesOf($className, $this->value, $this->message);
69 3
        $this->resetMessage();
70 3
        return $this;
71
    }
72
73
    /**
74
     * @param array<mixed> ...$expected
75
     *
76
     * @return CollectionCheck
77
     */
78 3
    public function contains(...$expected): self
79
    {
80 3
        foreach ($expected as $expectedElement) {
81 3
            Assert::assertContains($expectedElement, $this->value, $this->message);
82
        }
83 3
        $this->resetMessage();
84 3
        return $this;
85
    }
86
87
    /**
88
     * @param array<mixed> ...$expected
89
     *
90
     * @return CollectionCheck
91
     */
92 2
    public function containsExactly(...$expected): self
93
    {
94 2
        Assert::assertThat($this->value, new ContainsExactly(...$expected), $this->message);
95 2
        return $this;
96
    }
97
98
    /**
99
     * @param array<mixed> ...$subset
100
     *
101
     * @return CollectionCheck
102
     */
103 2
    public function containsSet(...$subset): self
104
    {
105 2
        $constraint = new ContainsSet($subset);
106 2
        Assert::assertThat($this->value, $constraint, $this->message);
107 2
        $this->resetMessage();
108 2
        return $this;
109
    }
110
111 2
    public function containsNoDuplicateItem(): self
112
    {
113 2
        Assert::assertThat($this->value, new ContainsNoDuplicateItem(), $this->message);
114 2
        $this->resetMessage();
115 2
        return $this;
116
    }
117
118 6
    public function hasLength(int $length): self
119
    {
120 6
        Assert::assertCount($length, $this->value, $this->message);
121 6
        $this->resetMessage();
122 6
        return $this;
123
    }
124
125 3
    public function hasNotLength(int $length): self
126
    {
127 3
        Assert::assertNotCount($length, $this->value, $this->message);
128 3
        $this->resetMessage();
129 3
        return $this;
130
    }
131
132
    /**
133
     * @param array<mixed> ...$set
134
     *
135
     * @return CollectionCheck
136
     */
137 2
    public function isSubsetOf(...$set): self
138
    {
139 2
        $constraint = new IsSubset($set);
140 2
        Assert::assertThat($this->value, $constraint, $this->message);
141 2
        $this->resetMessage();
142 2
        return $this;
143
    }
144
145 9
    public function isEqualTo(iterable $iterable): self
146
    {
147 9
        Assert::assertEquals($iterable, $this->value, $this->message);
148 9
        $this->resetMessage();
149 9
        return $this;
150
    }
151
152 1
    public function isNotEqualTo(iterable $iterable): self
153
    {
154 1
        Assert::assertNotEquals($iterable, $this->value, $this->message);
155 1
        $this->resetMessage();
156 1
        return $this;
157
    }
158
}
159