Passed
Push — master ( 1f2e81...28a1aa )
by Andrii
01:21
created

NearestSearchTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 20
c 1
b 0
f 0
dl 0
loc 50
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstruction() 0 5 1
A nearestProvider() 0 12 1
A testFindNearest() 0 7 1
A prepareKdTree() 0 8 1
1
<?php
2
3
namespace Search;
4
5
use KDTree\Interfaces\KDTreeInterface;
6
use KDTree\Search\NearestSearch;
7
use KDTree\Structure\KDTree;
8
use KDTree\ValueObject\Point;
9
use PHPUnit\Framework\TestCase;
10
11
class NearestSearchTest extends TestCase
12
{
13
    public function testConstruction(): void
14
    {
15
        $nearestSearch = new NearestSearch($this->prepareKdTree());
16
17
        $this->assertEquals(PHP_INT_MAX, $nearestSearch->getNearestDistance());
18
    }
19
20
    /**
21
     * @dataProvider nearestProvider
22
     */
23
    public function testFindNearest(array $point, array $nearest, float $distance): void
24
    {
25
        $nearestSearch = new NearestSearch($this->prepareKdTree());
26
27
        $point = $nearestSearch->nearest(new Point(...$point));
28
        $this->assertEquals($nearest, $point->getAxises());
29
        $this->assertEquals($distance, $nearestSearch->getNearestDistance());
30
    }
31
32
    /**
33
     * @return \Generator
34
     */
35
    public function nearestProvider(): \Generator
36
    {
37
        yield 'another point' => [
38
            'point' => [1, 1],
39
            'nearest' => [1, 3],
40
            'distance' => 2.0
41
        ];
42
43
        yield 'existing point' => [
44
            'point' => [9, 1],
45
            'nearest' => [9, 1],
46
            'distance' => 0.0
47
        ];
48
    }
49
50
    /**
51
     * @return KDTreeInterface
52
     */
53
    private function prepareKdTree(): KDTreeInterface
54
    {
55
        $kdTree = new KDTree(2);
56
        $kdTree->put(new Point(1, 3))
57
            ->put(new Point(4, 5))
58
            ->put(new Point(9, 1));
59
60
        return $kdTree;
61
    }
62
}
63