RecommenderTest::testRecommendManhattanSam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4286
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace stojg\recommend\tests;
4
5
use stojg\recommend\Data;
6
use stojg\recommend\strategy\Cosine;
7
use stojg\recommend\strategy\Manhattan;
8
use stojg\recommend\strategy\Paerson;
9
10
class RecommenderTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @var array
14
     */
15
    protected $set;
16
17
    public function setUp()
18
    {
19
        $data = $this->getFixture('users.json');
20
        $this->set = json_decode($data, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($data, true) of type * is incompatible with the declared type array of property $set.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
21
    }
22
23
    public function testRecommendManhattanHailey()
24
    {
25
        $data = new Data($this->set);
26
        $recommendations = $data->recommend('Hailey', new Manhattan());
27
        $this->assertEquals(3, count($recommendations));
28
        $this->assertEquals('Phoenix', $recommendations[0]['key']);
29
        $this->assertEquals(4.0, $recommendations[0]['value']);
30
        $this->assertEquals('Blues Traveler', $recommendations[1]['key']);
31
        $this->assertEquals(3.0, $recommendations[1]['value']);
32
        $this->assertEquals('Slightly Stoopid', $recommendations[2]['key']);
33
        $this->assertEquals(2.5, $recommendations[2]['value']);
34
    }
35
36
    public function testRecommendManhattanChan()
37
    {
38
        $data = new Data($this->set);
39
        $recommendations = $data->recommend('Chan', new Manhattan());
40
        $this->assertEquals(2, count($recommendations));
41
        $this->assertEquals('The Strokes', $recommendations[0]['key']);
42
        $this->assertEquals(5.0, $recommendations[0]['value']);
43
    }
44
45
    public function testRecommendManhattanSam()
46
    {
47
        $data = new Data($this->set);
48
        $recommendations = $data->recommend('Sam', new Manhattan());
49
        $this->assertEquals(1, count($recommendations));
50
        $this->assertEquals('Deadmau5', $recommendations[0]['key']);
51
        $this->assertEquals(1.0, $recommendations[0]['value']);
52
    }
53
54
    public function testRecommendManhattanAngelica()
55
    {
56
        $data = new Data($this->set);
57
        $recommendations = $data->recommend('Angelica', new Manhattan());
58
        $this->assertEquals(0, count($recommendations));
59
    }
60
61
    public function testRecommendPaersonHailey()
62
    {
63
        $data = new Data($this->set);
64
        $recommendations = $data->recommend('Hailey', new Paerson());
65
        $this->assertEquals(3, count($recommendations));
66
        $this->assertEquals('Blues Traveler', $recommendations[0]['key']);
67
        $this->assertEquals(5.0, $recommendations[0]['value']);
68
        $this->assertEquals('Phoenix', $recommendations[1]['key']);
69
        $this->assertEquals(5.0, $recommendations[1]['value']);
70
        $this->assertEquals('Slightly Stoopid', $recommendations[2]['key']);
71
        $this->assertEquals(4.0, $recommendations[2]['value']);
72
    }
73
74
    public function testRecommendPaersonNoMatch()
75
    {
76
        $set = json_decode(file_get_contents(__DIR__.'/fixtures/users_nomatch.json'), true);
77
        $data = new Data($set);
78
        $recommendations = $data->recommend('Andrea', new Paerson());
79
        $this->assertEquals(0, count($recommendations));
80
    }
81
82
    public function testRecommendCosineHailey()
83
    {
84
        $data = new Data($this->set);
85
        $recommendations = $data->recommend('Hailey', new Cosine());
86
        $this->assertEquals(3, count($recommendations));
87
        $this->assertEquals('Blues Traveler', $recommendations[0]['key']);
88
        $this->assertEquals(5.0, $recommendations[0]['value']);
89
        $this->assertEquals('Phoenix', $recommendations[1]['key']);
90
        $this->assertEquals(5.0, $recommendations[1]['value']);
91
    }
92
93
    public function testFromReadme()
94
    {
95
        $artistRatings = [
96
            'Abe' => [
97
                'Blues Traveler'   => 3,
98
                'Broken Bells'     => 2,
99
                'Norah Jones'      => 4,
100
                'Phoenix'          => 5,
101
                'Slightly Stoopid' => 1,
102
                'The Strokes'      => 2,
103
                'Vampire Weekend'  => 2,
104
            ],
105
            'Blair' => [
106
                'Blues Traveler'   => 2,
107
                'Broken Bells'     => 3,
108
                'Deadmau5'         => 4,
109
                'Phoenix'          => 2,
110
                'Slightly Stoopid' => 3,
111
                'Vampire Weekend'  => 3,
112
            ],
113
            'Clair' => [
114
                'Blues Traveler'   => 5,
115
                'Broken Bells'     => 1,
116
                'Deadmau5'         => 1,
117
                'Norah Jones'      => 3,
118
                'Phoenix'          => 5,
119
                'Slightly Stoopid' => 1,
120
            ],
121
        ];
122
123
        $data = new \stojg\recommend\Data($artistRatings);
124
        $recommendations = $data->recommend('Blair', new \stojg\recommend\strategy\Manhattan());
125
        $this->assertEquals('Norah Jones', $recommendations[0]['key']);
126
        $this->assertEquals(4, $recommendations[0]['value']);
127
    }
128
129
    public function testWithArticles()
130
    {
131
        $data = new \stojg\recommend\ArticleData();
132
        $data->push('eagle', $this->getFixture('article_eagle.txt'));
133
        $data->push('kiwi', $this->getFixture('article_kiwi.txt'));
134
        $data->push('turtle', $this->getFixture('article_turtle.txt'));
135
        $data->push('quantum', $this->getFixture('article_quantum.txt'));
136
137
        $eagleNearest = $data->findNearest('eagle');
138
        $this->assertEquals('kiwi', $eagleNearest, 'readers of eagle should read kiwi');
139
140
        $kiwiNearest = $data->findNearest('kiwi');
141
        $this->assertEquals('eagle', $kiwiNearest, 'readers of kiwi should read eagle');
142
143
        $turtleNearest = $data->findNearest('turtle');
144
        $this->assertEquals('eagle', $turtleNearest, 'turtle of eagle should read eagle');
145
146
        $quantumNearest = $data->findNearest('quantum');
147
        $this->assertEquals('eagle', $quantumNearest, 'readers of quantum should read eagle');
148
    }
149
150
    protected function getFixture($fixtureFileName)
151
    {
152
        return file_get_contents(__DIR__.'/fixtures/'.$fixtureFileName);
153
    }
154
}
155