Issues (26)

src/Checks/JsonCheck.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\Json\MatchesSchema;
12
13
/**
14
 * Class JsonCheck
15
 *
16
 * @package Pitchart\Phlunit\Checks
17
 *
18
 * @author Julien VITTE <[email protected]>
19
 *
20
 * @implements FluentCheck<string>
21
 */
22
class JsonCheck implements FluentCheck
23
{
24 1
    use WithMessage, ConstraintCheck, TypeCheck, FluentChecks;
25
26
    /**
27
     * @var string
28
     */
29
    private $value;
30
31
    /**
32
     * JsonCheck constructor.
33
     *
34
     * @param $value
35
     */
36 11
    public function __construct(string $value)
37
    {
38 11
        Assert::assertJson($value);
39 11
        $this->value = $value;
40 11
    }
41
42 1
    public function isEqualTo(string $expected): self
43
    {
44 1
        Assert::assertJsonStringEqualsJsonString($expected, $this->value, $this->message);
45 1
        return $this;
46
    }
47
48 1
    public function isNotEqualTo(string $expected): self
49
    {
50 1
        Assert::assertJsonStringNotEqualsJsonString($expected, $this->value, $this->message);
51 1
        return $this;
52
    }
53
54 1
    public function isEqualToFileContent(string $path): self
55
    {
56 1
        Assert::assertJsonStringEqualsJsonFile($path, $this->value, $this->message);
57 1
        return $this;
58
    }
59
60 1
    public function isNotEqualToFileContent(string $path): self
61
    {
62 1
        Assert::assertJsonStringNotEqualsJsonFile($path, $this->value, $this->message);
63 1
        return $this;
64
    }
65
66
    /**
67
     * @param string|array|object $schema
68
     *
69
     * @return JsonCheck
70
     */
71 2
    public function matchesSchema($schema): self
72
    {
73 2
        Assert::assertThat($this->value, new MatchesSchema($schema), $this->message);
74 2
        return $this;
75
    }
76
}
77