ILIAS  trunk Revision v11.0_alpha-2658-ge2404539063
RangeTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
23 
27 class 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  {
53  $range = $range->withLength(3);
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  {
63  $range = $range->withStart(3);
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);
90  $range = $range->withLength(-1);
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 }
testNegativeStart(Range $range)
Definition: RangeTest.php:80
testConstructionWrongStart()
Definition: RangeTest.php:93
testNegativeLength(Range $range)
Definition: RangeTest.php:87
static cropCases()
Definition: RangeTest.php:122
testFactory()
Definition: RangeTest.php:29
testEndCalculation(Range $range)
Definition: RangeTest.php:45
testConstructionWrongLength()
Definition: RangeTest.php:100
testWithStart(Range $range)
Definition: RangeTest.php:61
croppedTo(int $max)
This will create a range that is guaranteed to not exceed $max.
Definition: Range.php:98
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
withLength(int $length)
Definition: Range.php:87
testWithLength(Range $range)
Definition: RangeTest.php:51
testCroppedTo($start, $length, $max, $has_changed)
Definition: RangeTest.php:108
withStart(int $start)
Definition: Range.php:79
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28
testValues(Range $range)
Definition: RangeTest.php:38
testUnpack(Range $range)
Definition: RangeTest.php:71