/
SetTest.php
146 lines (122 loc) · 3.76 KB
/
SetTest.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
<?php declare(strict_types = 1);
namespace JSKOS;
use PHPUnit\Framework\Error\Notice;
use InvalidArgumentException;
/**
* @covers JSKOS\Set
* @covers JSKOS\Container
*/
class SetTest extends \PHPUnit\Framework\TestCase
{
public function testEmpty()
{
$set = new Set();
$this->assertEquals("[]", "$set");
$this->assertTrue($set->isEmpty());
$this->assertTrue($set->isClosed());
$this->assertEquals(0, count($set));
$this->assertFalse(isset($set[0]));
$this->assertEquals($set, new Set([]));
}
public function testClosed()
{
$set = new Set([null]);
$this->assertEquals("[null]", "$set");
$this->assertFalse($set->isClosed());
$this->assertFalse($set->isEmpty());
$this->assertEquals(0, count($set));
$this->assertFalse(isset($set[0]));
$set->setClosed(true);
$this->assertEquals($set, new Set());
$set->setClosed(false);
$this->assertEquals($set, new Set([null, null]));
}
public function testDuplicates()
{
$set = new Set([
new Concept(['uri'=>'x:y']),
new Concept(['identifier'=>['foo']]),
new Concept(['uri'=>'x:y']),
new Concept(['uri'=>'a:b'])
]);
$set[] = new Concept(['identifier'=>['foo']]);
$set[] = new Concept(['uri'=>'a:b']);
$uris = $set->map(function($m) { return $m->uri; });
$this->assertEquals(['x:y',null,'a:b',null], $uris);
$this->assertTrue($set->isValid());
$set[0]->uri = 'a:b';
$this->assertFalse($set->isValid());
}
public function testFindURI()
{
$set = new Set();
$this->assertEquals(-1, $set->findURI('x:y'));
$set[] = new Concept(['identifier'=>['x:y']]);
$this->assertEquals(-1, $set->findURI('x:y'));
$concept = new Concept(['uri'=>'x:y']);
$set[] = $concept;
$this->assertSame(1, $set->findURI('x:y'));
}
/**
* @dataProvider provideSampleSet
*/
public function testSetIterator($set)
{
$callback = function($m) { return $m->uri; };
$expect = ['a:b',null,'x:y'];
$uris = array_map($callback, iterator_to_array($set));
$this->assertEquals($expect, $uris);
$uris = $set->map($callback);
$this->assertEquals($expect, $uris);
}
/**
* @dataProvider provideSampleSet
*/
public function testArrayAccess($set)
{
$this->assertEquals(new Concept(['uri'=>'a:b']), $set[0]);
$this->assertTrue(isset($set[2]));
$concept = new Concept(['uri'=>'foo:bar']);
$set[1] = $concept;
$this->assertSame($concept, $set[1]);
}
/**
* @dataProvider provideSampleSet
*/
public function testJSON($set)
{
$this->assertFalse(strpos('context',"$set"));
}
public function provideSampleSet() {
return [ [ new Set([
new Concept(['uri'=>'a:b']),
new Concept(['identifier'=>['c:d']]),
new Concept(['uri'=>'x:y']),
]) ] ];
}
/**
* @dataProvider provideExceptionTests
*/
public function testExceptions($set, $call, $exception, $message)
{
$this->expectException($exception);
$this->expectExceptionMessage($message);
$call($set);
}
public function provideExceptionTests()
{
$set = new Set();
return [
[
new Set(),
function($s) { $s[0]; },
Notice::class, 'Undefined offset: 0'
], [
new Set(),
function($s) {$s[0] = 42; },
InvalidArgumentException::class,
'JSKOS\Set may only contain JSKOS Resources'
],
];
}
}