ReportSpec   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 7
lcom 2
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 4 1
A it_is_initializable() 0 4 1
A it_supports_setting_headers() 0 6 1
A it_supports_setting_filter() 0 6 1
A it_parses_column_headers() 0 13 1
A it_supports_resumption() 0 24 1
A it_might_not_exist() 0 8 1
1
<?php
2
3
namespace spec\Scriptotek\Alma\Analytics;
4
5
use PhpSpec\ObjectBehavior;
6
use Scriptotek\Alma\Analytics\Report;
7
use Scriptotek\Alma\Analytics\Row;
8
use Scriptotek\Alma\Client;
9
use Scriptotek\Alma\Exception\ResourceNotFound;
10
use spec\Scriptotek\Alma\SpecHelper;
11
12
class ReportSpec extends ObjectBehavior
13
{
14
    public function let(Client $almaClient)
15
    {
16
        $this->beConstructedWith($almaClient, '/test/path');
17
    }
18
19
    public function it_is_initializable()
20
    {
21
        $this->shouldHaveType(Report::class);
22
    }
23
24
    public function it_supports_setting_headers(Client $almaClient)
25
    {
26
        $this->beConstructedWith($almaClient, '/test/path', ['a', 'b']);
27
28
        $this->headers->shouldBe(['a', 'b']);
0 ignored issues
show
Documentation introduced by
The property headers does not exist on object<spec\Scriptotek\Alma\Analytics\ReportSpec>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
29
    }
30
31
    public function it_supports_setting_filter(Client $almaClient)
32
    {
33
        $this->beConstructedWith($almaClient, '/test/path', ['a', 'b'], 'la la la');
34
35
        $this->filter->shouldBe('la la la');
0 ignored issues
show
Documentation introduced by
The property filter does not exist on object<spec\Scriptotek\Alma\Analytics\ReportSpec>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
    }
37
38
    public function it_parses_column_headers(Client $almaClient)
39
    {
40
        $almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
41
            ->shouldBeCalledTimes(1)
42
            ->willReturn(SpecHelper::getDummyData('analytics_response.xml'));
43
44
        // $this->getHeaders()->shouldHaveCount(11);
45
        $this->getHeaders()->shouldContain('Event Start Date and Time');
46
47
        $firstRow = $this->current();
48
        $firstRow->shouldHaveType(Row::class);
49
        $firstRow['Event Start Date and Time']->shouldBe('2017-08-29T15:43:53');
50
    }
51
52
    public function it_supports_resumption(Client $almaClient)
53
    {
54
        $path = '/test/path';
0 ignored issues
show
Unused Code introduced by
$path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
56
        // To speed up tests
57
        Report::$retryDelayTime = 0;
58
59
        $almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
60
            ->shouldBeCalledTimes(1)
61
            ->willReturn(SpecHelper::getDummyData('analytics_response_part1.xml'));
62
63
        $almaClient->getXML('/analytics/reports?limit=1000&token=9672D715A8E2EAAA6F30DD22FC52BE4CCAE35E29D921E0AC8BE8C6734C9E1571B4E48EEFCA4046EFF8CD7D1662C2D0A7677D3AD05EDC3CA7F06182E34E9D7A2F')
64
            ->shouldBeCalledTimes(3)
65
            ->willReturn(
66
67
                // If Analytics is having a bad day, we might get a "still loading" response
68
                // See: https://bitbucket.org/uwlib/uwlib-alma-analytic-tools/wiki/Understanding_Analytic_GET_Requests#!analytic-still-loading
69
                SpecHelper::getDummyData('analytics_still_loading_response.xml'),
70
                SpecHelper::getDummyData('analytics_response_part2.xml'),
71
                SpecHelper::getDummyData('analytics_response_part3.xml')
72
            );
73
74
        $this->shouldHaveCount(150 + 150 + 88);
75
    }
76
77
    public function it_might_not_exist(Client $almaClient)
78
    {
79
        $almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
80
            ->shouldBeCalledTimes(1)
81
            ->willThrow(new ResourceNotFound('Path not found (/test/path)'));
82
83
        $this->exists()->shouldReturn(false);
84
    }
85
}
86