1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueObject; |
4
|
|
|
|
5
|
|
|
use KDTree\Exceptions\InvalidDimensionsCount; |
6
|
|
|
use KDTree\Exceptions\InvalidPointProvided; |
7
|
|
|
use KDTree\Exceptions\UnknownDimension; |
8
|
|
|
use KDTree\Interfaces\PointInterface; |
9
|
|
|
use KDTree\ValueObject\Point; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
|
12
|
|
|
final class PointTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
public function testCorrectConstruction(): void |
15
|
|
|
{ |
16
|
|
|
$points = [2.74, 3.75]; |
17
|
|
|
$point = $this->preparePoint(...$points); |
18
|
|
|
$name = 'Test point'; |
19
|
|
|
$point->setName($name); |
20
|
|
|
$this->assertEquals($points, $point->getAxises()); |
21
|
|
|
$this->assertEquals(count($points), $point->getDimensions()); |
22
|
|
|
$this->assertEquals($name, $point->getName()); |
23
|
|
|
$this->assertEquals($points[0], $point->getDAxis(0)); |
24
|
|
|
$this->assertEquals($points[1], $point->getDAxis(1)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testIncorrectPointsCount(): void |
28
|
|
|
{ |
29
|
|
|
$this->expectException(InvalidDimensionsCount::class); |
30
|
|
|
$this->preparePoint(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testEquals(): void |
34
|
|
|
{ |
35
|
|
|
$firstPointCoords = [23, 24]; |
36
|
|
|
$secondPointCoords = [25, 26]; |
37
|
|
|
|
38
|
|
|
$firstPoint = $this->preparePoint(...$firstPointCoords); |
39
|
|
|
$secondPoint = $this->preparePoint(...$secondPointCoords); |
40
|
|
|
$thirdPoint = $this->preparePoint(...$firstPointCoords); |
41
|
|
|
|
42
|
|
|
$this->assertFalse($firstPoint->equals($secondPoint)); |
43
|
|
|
$this->assertTrue($firstPoint->equals($thirdPoint)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testUnknownDimension(): void |
47
|
|
|
{ |
48
|
|
|
$point = $this->preparePoint(23, 24); |
49
|
|
|
$this->expectException(UnknownDimension::class); |
50
|
|
|
$point->getDAxis(3); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testDistance(): void |
54
|
|
|
{ |
55
|
|
|
$firstPoint = $this->preparePoint(0, 0); |
56
|
|
|
$secondPoint = $this->preparePoint(5, 5); |
57
|
|
|
|
58
|
|
|
$this->assertEquals(7.0710678118654755, $firstPoint->distance($secondPoint)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testDifferentDimensionsCount(): void |
62
|
|
|
{ |
63
|
|
|
$firstPoint = $this->preparePoint(0, 0); |
64
|
|
|
$secondPoint = $this->preparePoint(5, 5, 5); |
65
|
|
|
|
66
|
|
|
$this->expectException(InvalidPointProvided::class); |
67
|
|
|
$firstPoint->distance($secondPoint); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param float ...$points |
72
|
|
|
* |
73
|
|
|
* @return PointInterface |
74
|
|
|
* @throws InvalidDimensionsCount |
75
|
|
|
*/ |
76
|
|
|
private function preparePoint(float ...$points): PointInterface |
77
|
|
|
{ |
78
|
|
|
return new Point(...$points); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|