Completed
Push — master ( 47778c...ba169d )
by Dan Michael O.
10:36
created

ReportSpec::it_is_initializable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace spec\Scriptotek\Alma\Analytics;
4
5
use PhpSpec\ObjectBehavior;
6
use Psr\Http\Message\UriInterface;
7
use Scriptotek\Alma\Analytics\Report;
8
use Scriptotek\Alma\Analytics\Row;
9
use Scriptotek\Alma\Analytics\Rows;
10
use Scriptotek\Alma\Client;
11
use Scriptotek\Alma\Exception\ResourceNotFound;
12
use spec\Scriptotek\Alma\SpecHelper;
13
14
class ReportSpec extends ObjectBehavior
15
{
16
    public function let(Client $almaClient)
17
    {
18
        $this->beConstructedWith($almaClient, '/test/path');
19
    }
20
21
    public function it_is_initializable()
22
    {
23
        $this->shouldHaveType(Report::class);
24
    }
25
26
    public function it_supports_setting_headers(Client $almaClient)
27
    {
28
        $this->beConstructedWith($almaClient, '/test/path', ['a', 'b']);
29
30
        $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...
31
    }
32
33
    public function it_supports_setting_filter(Client $almaClient)
34
    {
35
        $this->beConstructedWith($almaClient, '/test/path', ['a', 'b'], 'la la la');
36
37
        $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...
38
    }
39
40 View Code Duplication
    public function it_can_be_counted(Client $almaClient, UriInterface $url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $almaClient->buildUrl('/analytics/reports', [
43
            'path' => '/test/path',
44
            'limit' => 1000,
45
            'token' => null,
46
            'filter' => null,
47
        ])->shouldBeCalled()->willReturn($url);
48
49
        $almaClient->getXML($url)
50
            ->shouldBeCalledTimes(1)
51
            ->willReturn(SpecHelper::getDummyData('analytics_response.xml'));
52
53
        $this->exists()->shouldReturn(true);
54
        $this->shouldHaveCount(25);
55
    }
56
57
    public function it_parses_column_headers(Client $almaClient, UriInterface $url)
58
    {
59
        $almaClient->buildUrl('/analytics/reports', [
60
            'path' => '/test/path',
61
            'limit' => 1000,
62
            'token' => null,
63
            'filter' => null,
64
        ])->shouldBeCalled()->willReturn($url);
65
66
        $almaClient->getXML($url)
67
            ->shouldBeCalledTimes(1)
68
            ->willReturn(SpecHelper::getDummyData('analytics_response.xml'));
69
70
        $this->getHeaders()->shouldHaveCount(11);
71
        $this->getHeaders()->shouldContain('Event Start Date and Time');
72
73
        $this->rewind();
74
        $this->valid();
75
        $firstRow = $this->current();
76
        $firstRow->shouldHaveType(Row::class);
77
        $firstRow['Event Start Date and Time']->shouldBe('2017-08-29T15:43:53');
78
    }
79
80
    public function it_supports_resumption(Client $almaClient, UriInterface $url1, UriInterface $url2)
81
    {
82
        $path = '/test/path';
83
84
        // To speed up tests
85
        Report::$retryDelayTime = 0;
86
87
        $almaClient->buildUrl('/analytics/reports', [
88
            'path' => $path,
89
            'limit' => 1000,
90
            'token' => null,
91
            'filter' => null,
92
        ])->shouldBeCalled()->willReturn($url1);
93
94
        $almaClient->getXML($url1)
95
            ->shouldBeCalledTimes(1)
96
            ->willReturn(SpecHelper::getDummyData('analytics_response_part1.xml'));
97
98
        $almaClient->buildUrl('/analytics/reports', [
99
            'path' => null,
100
            'limit' => 1000,
101
            'token' => '9672D715A8E2EAAA6F30DD22FC52BE4CCAE35E29D921E0AC8BE8C6734C9E1571B4E48EEFCA4046EFF8CD7D1662C2D0A7677D3AD05EDC3CA7F06182E34E9D7A2F',
102
            'filter' => null,
103
        ])->shouldBeCalled()->willReturn($url2);
104
105
        $almaClient->getXML($url2)
106
            ->shouldBeCalledTimes(3)
107
            ->willReturn(
108
109
                // If Analytics is having a bad day, we might get a "still loading" response
110
                // See: https://bitbucket.org/uwlib/uwlib-alma-analytic-tools/wiki/Understanding_Analytic_GET_Requests#!analytic-still-loading
111
                SpecHelper::getDummyData('analytics_still_loading_response.xml'),
112
                SpecHelper::getDummyData('analytics_response_part2.xml'),
113
                SpecHelper::getDummyData('analytics_response_part3.xml')
114
            );
115
116
        $this->shouldHaveCount(150 + 150 + 88);
117
    }
118
119 View Code Duplication
    public function it_might_not_exist(Client $almaClient, UriInterface $url)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $almaClient->buildUrl('/analytics/reports', [
122
            'path' => '/test/path',
123
            'limit' => 1000,
124
            'token' => null,
125
            'filter' => null,
126
        ])->shouldBeCalled()->willReturn($url);
127
128
        $almaClient->getXML($url)
129
            ->shouldBeCalledTimes(1)
130
            ->willThrow(new ResourceNotFound('Path not found (/test/path)'));
131
132
        $this->exists()->shouldReturn(false);
133
    }
134
}
135