generated from gocanto/php-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AttributesTest.php
118 lines (97 loc) · 2.82 KB
/
AttributesTest.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
<?php
declare(strict_types=1);
namespace Gocanto\Relay\Tests;
use Gocanto\Relay\AttributesException;
use Gocanto\Relay\Promoter;
use Gocanto\Relay\Tests\Stubs\Payload;
use Gocanto\Relay\Types\Any;
use Gocanto\Relay\Types\Text;
use Gocanto\Relay\Types\Url;
use PHPUnit\Framework\TestCase;
class AttributesTest extends TestCase
{
/**
* @test
* @throws AttributesException
*/
public function itReturnsValidValues()
{
$url = 'https://github.com/gocanto';
$payload = new Payload([
'website' => $url,
], [
'website' => Promoter::make(Url::class),
]);
$website = $payload->get('website');
$this->assertInstanceOf(Url::class, $website);
$this->assertSame($url, $website->get());
$this->assertEquals(['website' => Url::make($url)], $payload->toArray());
}
/**
* @test
* @throws AttributesException
*/
public function itReturnsNullForValuesThatAreNotRequired()
{
$payload = new Payload([
'name' => 'gustavo',
], [
'name' => Promoter::make(Text::class),
]);
$this->assertNull($payload->get('foo'));
}
/**
* @test
* @throws AttributesException
*/
public function itThrowsExceptionsIfAttributesDontContainGivenRulesFields()
{
$this->expectException(AttributesException::class);
$this->expectExceptionMessageMatches('/required/');
$payload = new Payload([
'last_name' => 'gustavo',
], [
'name' => Promoter::make(Text::class),
]);
$payload->get('name');
}
/**
* @test
* @throws AttributesException
*/
public function itWrapsUntypedValuesWithinAMixedType()
{
$payload = new Payload([
'name' => 'gustavo',
'last_name' => 'ocanto',
], [
'last_name' => Promoter::make(Text::class),
]);
$lastName = $payload->get('last_name');
$this->assertInstanceOf(Text::class, $lastName);
$this->assertSame('ocanto', $lastName->get());
$name = $payload->get('name');
$this->assertInstanceOf(Any::class, $name);
$this->assertSame('gustavo', $name->get());
}
/**
* @test
* @throws AttributesException
*/
public function itAllowsOptionalsValues()
{
$data = [
'name' => 'Gustavo',
];
$rules = [
'name' => Promoter::make(Text::class),
'website' => Promoter::optional(Url::class),
];
$payload = new Payload($data, $rules);
$this->assertNull($payload->get('website'));
/** @var Any $name */
$name = $payload->get('name');
$this->assertInstanceOf(Text::class, $name);
$this->assertSame('Gustavo', $name->get());
}
}