1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Structure; |
4
|
|
|
|
5
|
|
|
use KDTree\Exceptions\InvalidDimensionsCount; |
6
|
|
|
use KDTree\Exceptions\InvalidPointProvided; |
7
|
|
|
use KDTree\Exceptions\PointNotFound; |
8
|
|
|
use KDTree\Interfaces\PointsListInterface; |
9
|
|
|
use KDTree\Structure\PointsList; |
10
|
|
|
use KDTree\ValueObject\Point; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
final class PointsListTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testConstructAndIterate(): void |
16
|
|
|
{ |
17
|
|
|
$pointsList = $this->preparePointsList(); |
18
|
|
|
|
19
|
|
|
$this->assertCount(3, $pointsList); |
20
|
|
|
foreach ($pointsList as $key => $point) { |
21
|
|
|
$this->assertEquals(implode('_', $point->getAxises()), $key); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function testPointExists(): void |
26
|
|
|
{ |
27
|
|
|
$pointsList = $this->preparePointsList(); |
28
|
|
|
|
29
|
|
|
$this->assertTrue($pointsList->pointExists(new Point(1, 1))); |
30
|
|
|
$this->assertFalse($pointsList->pointExists(new Point(99, 99))); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testInvalidDimensionsCount(): void |
34
|
|
|
{ |
35
|
|
|
$this->expectException(InvalidDimensionsCount::class); |
36
|
|
|
new PointsList(0); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testDifferentDimensionsPoint(): void |
40
|
|
|
{ |
41
|
|
|
$pointsList = $this->preparePointsList(); |
42
|
|
|
$this->expectException(InvalidPointProvided::class); |
43
|
|
|
|
44
|
|
|
$pointsList->addPoint(new Point(1, 2, 3)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testPointAddSuccess(): void |
48
|
|
|
{ |
49
|
|
|
$pointsList = $this->preparePointsList(); |
50
|
|
|
$point = new Point(9, 10); |
51
|
|
|
$this->assertFalse($pointsList->pointExists($point)); |
52
|
|
|
|
53
|
|
|
$pointsList->addPoint($point); |
54
|
|
|
$this->assertTrue($pointsList->pointExists($point)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testPointRemoveSuccess(): void |
58
|
|
|
{ |
59
|
|
|
$pointsList = $this->preparePointsList(); |
60
|
|
|
$point = new Point(1, 1); |
61
|
|
|
$this->assertTrue($pointsList->pointExists($point)); |
62
|
|
|
|
63
|
|
|
$pointsList->removePoint($point); |
64
|
|
|
$this->assertFalse($pointsList->pointExists($point)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testPointNotExistsWhileRemove(): void |
68
|
|
|
{ |
69
|
|
|
$pointsList = $this->preparePointsList(); |
70
|
|
|
$point = new Point(99, 99); |
71
|
|
|
$this->expectException(PointNotFound::class); |
72
|
|
|
|
73
|
|
|
$pointsList->removePoint($point); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return PointsListInterface |
78
|
|
|
* |
79
|
|
|
* @throws InvalidDimensionsCount|InvalidPointProvided |
80
|
|
|
*/ |
81
|
|
|
private function preparePointsList(): PointsListInterface |
82
|
|
|
{ |
83
|
|
|
return (new PointsList(2)) |
84
|
|
|
->addPoint(new Point(1, 1)) |
85
|
|
|
->addPoint(new Point(2, 2)) |
86
|
|
|
->addPoint(new Point(3, 3)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|