ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
RangeTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use PHPUnit\Framework\TestCase;
23
27class RangeTest extends TestCase
28{
29 public function testFactory(): Range
30 {
31 $f = new ILIAS\Data\Factory();
32 $range = $f->range(1, 2);
33 $this->assertInstanceOf(Range::class, $range);
34 return $range;
35 }
36
37 #[\PHPUnit\Framework\Attributes\Depends('testFactory')]
38 public function testValues(Range $range): void
39 {
40 $this->assertEquals(1, $range->getStart());
41 $this->assertEquals(2, $range->getLength());
42 }
43
44 #[\PHPUnit\Framework\Attributes\Depends('testFactory')]
45 public function testEndCalculation(Range $range): void
46 {
47 $this->assertEquals(3, $range->getEnd());
48 }
49
50 #[\PHPUnit\Framework\Attributes\Depends('testFactory')]
51 public function testWithLength(Range $range): Range
52 {
54 $this->assertEquals(1, $range->getStart());
55 $this->assertEquals(3, $range->getLength());
56 $this->assertEquals(4, $range->getEnd());
57 return $range;
58 }
59
60 #[\PHPUnit\Framework\Attributes\Depends('testWithLength')]
61 public function testWithStart(Range $range): Range
62 {
64 $this->assertEquals(3, $range->getStart());
65 $this->assertEquals(3, $range->getLength());
66 $this->assertEquals(6, $range->getEnd());
67 return $range;
68 }
69
70 #[\PHPUnit\Framework\Attributes\Depends('testWithStart')]
71 public function testUnpack(Range $range): void
72 {
73 $this->assertEquals(
74 [3,3],
75 $range->unpack()
76 );
77 }
78
79 #[\PHPUnit\Framework\Attributes\Depends('testFactory')]
80 public function testNegativeStart(Range $range): void
81 {
82 $this->expectException(InvalidArgumentException::class);
83 $range = $range->withStart(-5);
84 }
85
86 #[\PHPUnit\Framework\Attributes\Depends('testFactory')]
87 public function testNegativeLength(Range $range): void
88 {
89 $this->expectException(InvalidArgumentException::class);
91 }
92
93 public function testConstructionWrongStart(): void
94 {
95 $this->expectException(InvalidArgumentException::class);
96 $f = new ILIAS\Data\Factory();
97 $range = $f->range(-1, 2);
98 }
99
100 public function testConstructionWrongLength(): void
101 {
102 $this->expectException(InvalidArgumentException::class);
103 $f = new ILIAS\Data\Factory();
104 $range = $f->range(1, -2);
105 }
106
107 #[\PHPUnit\Framework\Attributes\DataProvider('cropCases')]
108 public function testCroppedTo($start, $length, $max, $has_changed): void
109 {
110 $f = new ILIAS\Data\Factory();
111 $range = $f->range($start, $length);
112 $cropped = $range->croppedTo($max);
113
114 if (!$has_changed) {
115 $this->assertEquals($range, $cropped);
116 } else {
117 $this->assertEquals(min($max, $start), $cropped->getStart());
118 $this->assertEquals($max, $cropped->getEnd());
119 }
120 }
121
122 public static function cropCases(): array
123 {
124 return [
125 [0, 100, 1000, false],
126 [0, 1000, 100, true],
127 [50, 100, 75, true],
128 [50, 100, 200, false],
129 [100, 100, 50, true]
130 ];
131 }
132}
Builds data types.
Definition: Factory.php:36
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
withLength(int $length)
Definition: Range.php:87
withStart(int $start)
Definition: Range.php:79
croppedTo(int $max)
This will create a range that is guaranteed to not exceed $max.
Definition: Range.php:98
testConstructionWrongLength()
Definition: RangeTest.php:100
testUnpack(Range $range)
Definition: RangeTest.php:71
testConstructionWrongStart()
Definition: RangeTest.php:93
testWithLength(Range $range)
Definition: RangeTest.php:51
testFactory()
Definition: RangeTest.php:29
testNegativeLength(Range $range)
Definition: RangeTest.php:87
testWithStart(Range $range)
Definition: RangeTest.php:61
static cropCases()
Definition: RangeTest.php:122
testNegativeStart(Range $range)
Definition: RangeTest.php:80
testEndCalculation(Range $range)
Definition: RangeTest.php:45
testValues(Range $range)
Definition: RangeTest.php:38
testCroppedTo($start, $length, $max, $has_changed)
Definition: RangeTest.php:108