-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFieldTest.php
More file actions
executable file
·417 lines (363 loc) · 12.7 KB
/
FieldTest.php
File metadata and controls
executable file
·417 lines (363 loc) · 12.7 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/*
* (c) Carsten Klee <mailme.klee@yahoo.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CK\MARCspec\Test;
use CK\MARCspec\Field;
use CK\MARCspec\MARCspec;
use CK\MARCspec\SubSpec;
use PHPUnit\Framework\TestCase;
class FieldTest extends TestCase
{
/**
* @dataProvider invalidFromTestSuiteProvider
*
* @expectedException Exception
*/
public function testInvalidFromTestSuite($test)
{
new Field($test);
}
public function invalidFromTestSuiteProvider()
{
$invalidTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/invalid/invalidFieldTag.json'));
$data = [];
foreach ($invalidTests->{'tests'} as $test) {
$data[0][] = $test->{'data'};
}
return $data;
}
public function testValidFromTestSuite()
{
$validTests = json_decode(file_get_contents(__DIR__.'/../'.'vendor/ck/marcspec-test-suite/valid/validFieldTag.json'));
foreach ($validTests->{'tests'} as $test) {
$this->assertInstanceOf('CK\MARCspec\FieldInterface', new Field($test->{'data'}));
}
}
public function fieldspec($arg)
{
return new Field($arg);
}
/**
* @expectedException InvalidArgumentException
*/
public function testInvalidArgument2Decode()
{
$this->fieldspec(['245']);
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec13()
{
$this->fieldspec('007/');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec14()
{
$this->fieldspec('007/1-2-');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec15()
{
$this->fieldspec('24#');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec16()
{
$this->fieldspec('007/-2');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec17()
{
$this->fieldspec('245[-2]');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec18()
{
$this->fieldspec('245[1-2-]');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec19()
{
$this->fieldspec('245[1-2');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec110()
{
$this->fieldspec('007/1-X');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec111()
{
$this->fieldspec('007/#-');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec112()
{
$this->fieldspec('245[0-2a]');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec113()
{
$this->fieldspec('300[1-]');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec114()
{
$fieldSpec = $this->fieldspec('aA0');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec31()
{
$this->fieldspec('245^1');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec32()
{
$this->fieldspec('245_$');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidFieldSpec33()
{
$this->fieldspec('245^');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidArgument310Decode()
{
$this->fieldspec('245{$c=$d}$a');
}
/**
* @expectedException CK\MARCspec\Exception\InvalidMARCspecException
*/
public function testInvalidArgument311Decode()
{
$this->fieldspec('245[1]/1_01');
}
/**
* assert same field tag.
*/
public function testValidFieldSpec1()
{
$fieldSpec = $this->fieldspec('LDR');
$this->assertSame('LDR', $fieldSpec->getTag());
$fieldSpec = $this->fieldspec('245');
$this->assertSame('245', $fieldSpec->getTag());
$fieldSpec = $this->fieldspec('...');
$this->assertSame('...', $fieldSpec->getTag());
$fieldSpec = $this->fieldspec('245[1]');
$this->assertSame('245', $fieldSpec->getTag());
$this->assertSame(1, $fieldSpec->getIndexStart());
$fieldSpec = $this->fieldspec('245[1-3]');
$this->assertSame(1, $fieldSpec->getIndexStart());
$this->assertSame(3, $fieldSpec->getIndexEnd());
$fieldSpec = $this->fieldspec('245[1-#]');
$this->assertSame(1, $fieldSpec->getIndexStart());
$this->assertSame('#', $fieldSpec->getIndexEnd());
$fieldSpec = $this->fieldspec('245[#-3]');
$this->assertSame('#', $fieldSpec->getIndexStart());
$this->assertSame(3, $fieldSpec->getIndexEnd());
}
/**
* test character position and range.
*/
public function testValidFieldSpec2()
{
$fieldSpec = $this->fieldspec('LDR/0-3');
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame(0, $fieldSpec->getCharStart());
$this->assertSame(3, $fieldSpec->getCharEnd());
$this->assertSame(4, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('LDR/0-#');
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame(0, $fieldSpec->getCharStart());
$this->assertSame('#', $fieldSpec->getCharEnd());
$this->assertSame(null, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('LDR/#-4');
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame('#', $fieldSpec->getCharStart());
$this->assertSame(4, $fieldSpec->getCharEnd());
$this->assertSame(5, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('LDR/#-0');
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame('#', $fieldSpec->getCharStart());
$this->assertSame(0, $fieldSpec->getCharEnd());
$this->assertSame(1, $fieldSpec->getCharLength());
}
/**
* test character range.
*/
public function testValidFieldSpec22()
{
$fieldSpec = $this->fieldspec('245/#');
$this->assertSame(1, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('245/#-#');
$this->assertSame(1, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('245/#-0');
$this->assertSame(1, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('245/#-1');
$this->assertSame(2, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('245/0-#');
$this->assertSame(0, $fieldSpec->getCharStart());
$this->assertSame('#', $fieldSpec->getCharEnd());
$this->assertSame(null, $fieldSpec->getCharLength());
}
/**
* @covers CK\MARCspec\Field::offsetSet
* @covers CK\MARCspec\Field::offsetExists
* @covers CK\MARCspec\Field::addSubSpec
*/
public function testValidFieldSpec24()
{
$fieldSpec = $this->fieldspec('...');
$fieldSpec['indexStart'] = '0';
$fieldSpec['indexEnd'] = '1';
$Subspec = new SubSpec(new MARCspec('245$b'), '!=', new MARCspec('245$c'));
$fieldSpec['subSpecs'] = $Subspec;
$fieldSpec->addSubSpec($Subspec);
$this->assertTrue($fieldSpec->offsetExists('tag'));
$this->assertTrue($fieldSpec->offsetExists('indexStart'));
$this->assertTrue($fieldSpec->offsetExists('indexEnd'));
$this->assertTrue($fieldSpec->offsetExists('subSpecs'));
}
/**
* test character position and range.
*/
public function testSetAndGetChar()
{
$fieldSpec = $this->fieldspec('LDR');
$fieldSpec->setCharStartEnd(0, 3);
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame(0, $fieldSpec->getCharStart());
$this->assertSame(3, $fieldSpec->getCharEnd());
$this->assertSame(4, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('LDR');
$fieldSpec->setCharStartEnd('#', 3);
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame('#', $fieldSpec->getCharStart());
$this->assertSame(3, $fieldSpec->getCharEnd());
$this->assertSame(4, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('LDR');
$fieldSpec->setCharStartEnd(0, 4);
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame(0, $fieldSpec->getCharStart());
$this->assertSame(4, $fieldSpec->getCharEnd());
$this->assertSame(5, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('LDR');
$fieldSpec->setCharStartLength('#', 4);
$this->assertSame('LDR', $fieldSpec->getTag());
$this->assertSame('#', $fieldSpec->getCharStart());
$this->assertSame(3, $fieldSpec->getCharEnd());
$this->assertSame(4, $fieldSpec->getCharLength());
}
/**
* test index position and range.
*/
public function testSetAndGetIndex()
{
$fieldSpec = $this->fieldspec('300');
$fieldSpec->setIndexStartEnd(0, 3);
$this->assertSame('300', $fieldSpec->getTag());
$this->assertSame(0, $fieldSpec->getIndexStart());
$this->assertSame(3, $fieldSpec->getIndexEnd());
$this->assertSame(4, $fieldSpec->getIndexLength());
$fieldSpec = $this->fieldspec('300');
$fieldSpec->setIndexStartEnd('#', 3);
$this->assertSame('300', $fieldSpec->getTag());
$this->assertSame('#', $fieldSpec->getIndexStart());
$this->assertSame(3, $fieldSpec->getIndexEnd());
$this->assertSame(4, $fieldSpec->getIndexLength());
$fieldSpec = $this->fieldspec('300');
$fieldSpec->setIndexStartEnd(0, 4);
$this->assertSame('300', $fieldSpec->getTag());
$this->assertSame(0, $fieldSpec->getIndexStart());
$this->assertSame(4, $fieldSpec->getIndexEnd());
$this->assertSame(5, $fieldSpec->getIndexLength());
$fieldSpec = $this->fieldspec('300');
$fieldSpec->setIndexStartLength('#', 4);
$this->assertSame('300', $fieldSpec->getTag());
$this->assertSame('#', $fieldSpec->getIndexStart());
$this->assertSame(3, $fieldSpec->getIndexEnd());
$this->assertSame(4, $fieldSpec->getIndexLength());
$fieldSpec = $this->fieldspec('300');
$fieldSpec->setIndexStartLength(0, 6);
$this->assertSame('300', $fieldSpec->getTag());
$this->assertSame(0, $fieldSpec->getIndexStart());
$this->assertSame(5, $fieldSpec->getIndexEnd());
$this->assertSame(6, $fieldSpec->getIndexLength());
}
/**
* test encoding.
*/
public function testEncode()
{
$fieldSpec = $this->fieldspec('245');
$this->assertSame('245', "$fieldSpec");
$fieldSpec = $this->fieldspec('007/1');
$this->assertSame('007/1', $fieldSpec->__toString());
$this->assertSame(1, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('007/1-3');
$this->assertSame('007/1-3', "$fieldSpec");
$this->assertSame(3, $fieldSpec->getCharLength());
$fieldSpec = $this->fieldspec('300[1]');
$this->assertSame('300[1]', "$fieldSpec");
$fieldSpec = $this->fieldspec('300[1-3]');
$this->assertSame('300[1-3]', $fieldSpec->__toString());
}
/**
* @covers CK\MARCspec\Field::jsonSerialize
*/
public function testJson()
{
$fieldSpec = $this->fieldspec('008[1-2]/0-5');
$_fieldSpec['tag'] = '008';
$_fieldSpec['indexStart'] = 1;
$_fieldSpec['indexEnd'] = 2;
$_fieldSpec['indexLength'] = 2;
$_fieldSpec['charStart'] = 0;
$_fieldSpec['charEnd'] = 5;
$_fieldSpec['charLength'] = 6;
$this->assertSame(json_encode($_fieldSpec), json_encode($fieldSpec));
}
/**
* @expectedException BadMethodCallException
*/
public function testOffsetUnset()
{
$fieldSpec = $this->fieldspec('245');
unset($fieldSpec['tag']);
}
}