-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPageRankAlgorithmTest.php
138 lines (119 loc) · 3.75 KB
/
PageRankAlgorithmTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace PhpScience\PageRank\Service;
use PhpScience\PageRank\Builder\NodeBuilder;
use PhpScience\PageRank\Builder\NodeCollectionBuilder;
use PhpScience\PageRank\Service\PageRankAlgorithm\Normalizer;
use PhpScience\PageRank\Service\PageRankAlgorithm\RankComparator;
use PhpScience\PageRank\Service\PageRankAlgorithm\Ranking;
use PhpScience\PageRank\Strategy\MemorySourceStrategy;
use PHPUnit\Framework\TestCase;
class PageRankAlgorithmTest extends TestCase
{
/**
* @dataProvider dataProviderExpectedNormalizedRanks
*
* @param float[] $expectedRanks
*/
public function testRun(array $expectedRanks): void
{
$pageRankAlgorithm = $this->createPageRankAlgorithm();
$nodeCollection = $pageRankAlgorithm->run(2);
static::assertSame(4, $nodeCollection->getAllNodeCount());
foreach ($nodeCollection->getNodes() as $node) {
$expectedRank = $expectedRanks[$node->getId()];
$actualRank = $node->getRank();
static::assertSame($expectedRank, $actualRank);
}
}
public function dataProviderExpectedNormalizedRanks(): array
{
return [
'scenario_1' => [
'expectedRanks' => [
1 => 1.0,
2 => 2.4999999999999996,
3 => 10.0,
4 => 8.5
]
]
];
}
/**
* @dataProvider dataProviderExpectedRanks
*
* @param float[] $expectedRanks
*/
public function testRunBatch(array $expectedRanks): void
{
$pageRankAlgorithm = $this->createPageRankAlgorithm();
$pageRankAlgorithm->initiateRanking();
$nodeCollection = $pageRankAlgorithm->runBatch(2);
static::assertSame(4, $nodeCollection->getAllNodeCount());
foreach ($nodeCollection->getNodes() as $node) {
$expectedRank = $expectedRanks[$node->getId()];
$actualRank = $node->getRank();
static::assertSame($expectedRank, $actualRank);
}
}
public function dataProviderExpectedRanks(): array
{
return [
'scenario_1' => [
'expectedRanks' => [
1 => 0.125,
2 => 0.16666666666667,
3 => 0.375,
4 => 0.33333333333333
]
]
];
}
private function getDataSource(): array
{
return [
1 => [
'id' => 1,
'out' => [2, 3],
'in' => [3]
],
2 => [
'id' => 2,
'out' => [4],
'in' => [1, 3]
],
3 => [
'id' => 3,
'out' => [1, 2, 4],
'in' => [1, 4]
],
4 => [
'id' => 4,
'out' => [3],
'in' => [3, 2]
]
];
}
private function createPageRankAlgorithm(): PageRankAlgorithmInterface
{
$dataSource = $this->getDataSource();
$nodeBuilder = new NodeBuilder();
$nodeCollectionBuilder = new NodeCollectionBuilder();
$strategy = new MemorySourceStrategy(
$nodeBuilder,
$nodeCollectionBuilder,
$dataSource
);
$rankComparator = new RankComparator();
$ranking = new Ranking(
$rankComparator,
$strategy
);
$normalizer = new Normalizer();
return new PageRankAlgorithm(
$ranking,
$strategy,
$normalizer
);
}
}