-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTapFunctionTest.php
More file actions
117 lines (97 loc) · 2.93 KB
/
Copy pathTapFunctionTest.php
File metadata and controls
117 lines (97 loc) · 2.93 KB
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
<?php
namespace Tests\Koded\Stdlib;
use Error;
use Koded\Stdlib\Arguments;
use Koded\Stdlib\Config;
use Koded\Stdlib\ExtendedArguments;
use Koded\Stdlib\Tapped;
use PHPUnit\Framework\TestCase;
use function Koded\Stdlib\tap;
class TapFunctionTest extends TestCase
{
use ObjectPropertyTrait;
public function test_object_with_callback()
{
$conf = tap(new Config, function(Config $conf) {
$conf->silent(true);
$conf->foo = 'bar';
});
$this->assertInstanceOf(Config::class, $conf);
$this->assertTrue($this->property($conf, 'silent'));
$this->assertSame('bar', $conf->foo);
}
public function test_object_without_callback()
{
$conf = tap(new Config);
$conf->foo = 'bar';
$this->assertInstanceOf(Tapped::class, $conf);
$this->assertSame('bar', $conf->foo);
}
public function test_extended_arguments()
{
$args = tap(new ExtendedArguments, function(ExtendedArguments $args) {
$args
->import((array)null)
->set('foo', 'bar');
});
$this->assertSame('bar', $args->get('foo'));
}
public function test_with_array_value()
{
$arr = tap([], function(&$arr) {
$arr['foo'] = 'bar';
});
$this->assertSame(['foo' => 'bar'], $arr);
}
public function test_array_value_without_callback()
{
$original = [];
$arr = tap($original);
$original['foo'] = 'bar';
$this->assertSame(['foo' => 'bar'], $original);
$this->assertInstanceOf(Tapped::class, $arr);
}
public function test_chainable_methods()
{
$data = tap(new Arguments(['foo' => 'bar']), function ($data) {
$data
->set('bar', [1, 2 , 3])
->delete('bar.1')
->import([
100 => true,
101 => false,
102 => true
]);
});
$this->assertSame(
[
'foo' => 'bar',
'bar' => [1, 2 ,3],
100 => true,
101 => false,
102 => true
],
$data->toArray()
);
}
public function test_primitive_by_value()
{
$value = tap(42, function($value) { $value = 'fubar'; });
$this->assertSame(42, $value,
'The tapped value is not changed (passed by value)');
}
public function test_primitive_by_reference()
{
$value = tap(42, function(&$value) { $value = 'fubar'; });
$this->assertSame('fubar', $value,
'The tapped value is changed (passed as reference)');
}
public function test_unreasonable_use()
{
$this->expectException(Error::class);
$this->expectExceptionMessage('Attempt to assign property "foo" on int');
tap(42, function($v) {
$v->foo = 'bar';
});
}
}