/
NonPositiveIntegerTypeTest.php
149 lines (124 loc) · 5.28 KB
/
NonPositiveIntegerTypeTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Unit\Type\Types;
use AssertionError;
use CuyZ\Valinor\Tests\Fake\Type\FakeType;
use CuyZ\Valinor\Tests\Traits\TestIsSingleton;
use CuyZ\Valinor\Type\Types\MixedType;
use CuyZ\Valinor\Type\Types\NativeIntegerType;
use CuyZ\Valinor\Type\Types\NonPositiveIntegerType;
use CuyZ\Valinor\Type\Types\PositiveIntegerType;
use CuyZ\Valinor\Type\Types\UnionType;
use PHPUnit\Framework\TestCase;
use stdClass;
final class NonPositiveIntegerTypeTest extends TestCase
{
use TestIsSingleton;
private NonPositiveIntegerType $nonPositiveIntegerType;
protected function setUp(): void
{
parent::setUp();
$this->nonPositiveIntegerType = new NonPositiveIntegerType();
}
public function test_accepts_correct_values(): void
{
self::assertTrue($this->nonPositiveIntegerType->accepts(0));
self::assertTrue($this->nonPositiveIntegerType->accepts(-404));
}
public function test_does_not_accept_incorrect_values(): void
{
self::assertFalse($this->nonPositiveIntegerType->accepts(null));
self::assertFalse($this->nonPositiveIntegerType->accepts('Schwifty!'));
self::assertFalse($this->nonPositiveIntegerType->accepts(404));
self::assertFalse($this->nonPositiveIntegerType->accepts(42.1337));
self::assertFalse($this->nonPositiveIntegerType->accepts(['foo' => 'bar']));
self::assertFalse($this->nonPositiveIntegerType->accepts(false));
self::assertFalse($this->nonPositiveIntegerType->accepts(new stdClass()));
}
public function test_can_cast_integer_value(): void
{
self::assertTrue($this->nonPositiveIntegerType->canCast(0));
self::assertTrue($this->nonPositiveIntegerType->canCast(-404));
self::assertTrue($this->nonPositiveIntegerType->canCast('-404'));
self::assertTrue($this->nonPositiveIntegerType->canCast(-404.00));
}
public function test_cannot_cast_other_types(): void
{
self::assertFalse($this->nonPositiveIntegerType->canCast(null));
self::assertFalse($this->nonPositiveIntegerType->canCast(42));
self::assertFalse($this->nonPositiveIntegerType->canCast(-42.1337));
self::assertFalse($this->nonPositiveIntegerType->canCast(42.1337));
self::assertFalse($this->nonPositiveIntegerType->canCast(['foo' => 'bar']));
self::assertFalse($this->nonPositiveIntegerType->canCast('Schwifty!'));
self::assertFalse($this->nonPositiveIntegerType->canCast(false));
self::assertFalse($this->nonPositiveIntegerType->canCast(new stdClass()));
}
/**
* @dataProvider cast_value_returns_correct_result_data_provider
*/
public function test_cast_value_returns_correct_result(mixed $value, int $expected): void
{
self::assertSame($expected, $this->nonPositiveIntegerType->cast($value));
}
public function cast_value_returns_correct_result_data_provider(): array
{
return [
'Integer from float' => [
'value' => -404.00,
'expected' => -404,
],
'Integer from string' => [
'value' => '-42',
'expected' => -42,
],
'Integer from integer' => [
'value' => -1337,
'expected' => -1337,
],
'Zero from string' => [
'value' => '0',
'expected' => 0,
],
];
}
public function test_cast_invalid_value_throws_exception(): void
{
$this->expectException(AssertionError::class);
$this->nonPositiveIntegerType->cast('foo');
}
public function test_cast_invalid_positive_value_throws_exception(): void
{
$this->expectException(AssertionError::class);
$this->nonPositiveIntegerType->cast(1337);
}
public function test_string_value_is_correct(): void
{
self::assertSame('non-positive-int', $this->nonPositiveIntegerType->toString());
}
public function test_matches_valid_integer_type(): void
{
self::assertTrue($this->nonPositiveIntegerType->matches(new NativeIntegerType()));
self::assertTrue($this->nonPositiveIntegerType->matches($this->nonPositiveIntegerType));
self::assertFalse($this->nonPositiveIntegerType->matches(new PositiveIntegerType()));
}
public function test_does_not_match_other_type(): void
{
self::assertFalse($this->nonPositiveIntegerType->matches(new FakeType()));
}
public function test_matches_mixed_type(): void
{
self::assertTrue($this->nonPositiveIntegerType->matches(new MixedType()));
}
public function test_matches_union_type_containing_integer_type(): void
{
$union = new UnionType(new FakeType(), new NativeIntegerType(), new FakeType());
$unionWithSelf = new UnionType(new FakeType(), new NonPositiveIntegerType(), new FakeType());
self::assertTrue($this->nonPositiveIntegerType->matches($union));
self::assertTrue($this->nonPositiveIntegerType->matches($unionWithSelf));
}
public function test_does_not_match_union_type_not_containing_integer_type(): void
{
$unionType = new UnionType(new FakeType(), new FakeType());
self::assertFalse($this->nonPositiveIntegerType->matches($unionType));
}
}