Skip to content

Commit f49528c

Browse files
committed
Refactor URL building
Reduce complexity and simplify tests
1 parent bcb499b commit f49528c

16 files changed

+98
-220
lines changed

spec/Analytics/ReportSpec.php

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace spec\Scriptotek\Alma\Analytics;
44

55
use PhpSpec\ObjectBehavior;
6-
use Psr\Http\Message\UriInterface;
76
use Scriptotek\Alma\Analytics\Report;
87
use Scriptotek\Alma\Analytics\Row;
98
use Scriptotek\Alma\Analytics\Rows;
@@ -37,33 +36,19 @@ public function it_supports_setting_filter(Client $almaClient)
3736
$this->filter->shouldBe('la la la');
3837
}
3938

40-
public function it_can_be_counted(Client $almaClient, UriInterface $url)
39+
public function it_can_be_counted(Client $almaClient)
4140
{
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)
41+
$almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
5042
->shouldBeCalledTimes(1)
5143
->willReturn(SpecHelper::getDummyData('analytics_response.xml'));
5244

5345
$this->exists()->shouldReturn(true);
5446
$this->shouldHaveCount(25);
5547
}
5648

57-
public function it_parses_column_headers(Client $almaClient, UriInterface $url)
49+
public function it_parses_column_headers(Client $almaClient)
5850
{
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)
51+
$almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
6752
->shouldBeCalledTimes(1)
6853
->willReturn(SpecHelper::getDummyData('analytics_response.xml'));
6954

@@ -77,32 +62,18 @@ public function it_parses_column_headers(Client $almaClient, UriInterface $url)
7762
$firstRow['Event Start Date and Time']->shouldBe('2017-08-29T15:43:53');
7863
}
7964

80-
public function it_supports_resumption(Client $almaClient, UriInterface $url1, UriInterface $url2)
65+
public function it_supports_resumption(Client $almaClient)
8166
{
8267
$path = '/test/path';
8368

8469
// To speed up tests
8570
Report::$retryDelayTime = 0;
8671

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)
72+
$almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
9573
->shouldBeCalledTimes(1)
9674
->willReturn(SpecHelper::getDummyData('analytics_response_part1.xml'));
9775

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)
76+
$almaClient->getXML('/analytics/reports?limit=1000&token=9672D715A8E2EAAA6F30DD22FC52BE4CCAE35E29D921E0AC8BE8C6734C9E1571B4E48EEFCA4046EFF8CD7D1662C2D0A7677D3AD05EDC3CA7F06182E34E9D7A2F')
10677
->shouldBeCalledTimes(3)
10778
->willReturn(
10879

@@ -116,16 +87,9 @@ public function it_supports_resumption(Client $almaClient, UriInterface $url1, U
11687
$this->shouldHaveCount(150 + 150 + 88);
11788
}
11889

119-
public function it_might_not_exist(Client $almaClient, UriInterface $url)
90+
public function it_might_not_exist(Client $almaClient)
12091
{
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)
92+
$almaClient->getXML('/analytics/reports?path=%2Ftest%2Fpath&limit=1000')
12993
->shouldBeCalledTimes(1)
13094
->willThrow(new ResourceNotFound('Path not found (/test/path)'));
13195

spec/Bibs/BibSpec.php

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
7-
use Psr\Http\Message\UriInterface;
87
use Scriptotek\Alma\Bibs\Bib;
98
use Scriptotek\Alma\Bibs\Bibs;
109
use Scriptotek\Alma\Bibs\Holding;
@@ -16,18 +15,14 @@
1615

1716
class BibSpec extends ObjectBehavior
1817
{
19-
public function let(AlmaClient $client, UriInterface $url)
18+
public function let(AlmaClient $client)
2019
{
2120
$this->beConstructedWith($client, '999104760474702204');
2221
}
2322

24-
protected function expectRequest($client, $url)
23+
protected function expectRequest($client)
2524
{
26-
$client->buildUrl('/bibs/999104760474702204', [])
27-
->shouldBeCalled()
28-
->willReturn($url);
29-
30-
$client->getXML($url)
25+
$client->getXML('/bibs/999104760474702204')
3126
->shouldBeCalled()
3227
->willReturn(SpecHelper::getDummyData('bib_response_iz.xml'));
3328
}
@@ -38,23 +33,23 @@ public function it_is_lazy(AlmaClient $client)
3833
$this->shouldHaveType(Bib::class);
3934
}
4035

41-
public function it_loads_bib_data_when_needed(AlmaClient $client, UriInterface $url)
36+
public function it_loads_bib_data_when_needed(AlmaClient $client)
4237
{
43-
$this->expectRequest($client, $url);
38+
$this->expectRequest($client);
4439

4540
$this->created_date->shouldBe('2015-11-05Z');
4641
}
4742

48-
public function it_can_exist(AlmaClient $client, UriInterface $url)
43+
public function it_can_exist(AlmaClient $client)
4944
{
50-
$this->expectRequest($client, $url);
45+
$this->expectRequest($client);
5146

5247
$this->exists()->shouldBe(true);
5348
}
5449

55-
public function it_links_to_network_zone(AlmaClient $client, AlmaClient $nz, Bibs $bibs, Bib $nz_bib, UriInterface $url)
50+
public function it_links_to_network_zone(AlmaClient $client, AlmaClient $nz, Bibs $bibs, Bib $nz_bib)
5651
{
57-
$this->expectRequest($client, $url);
52+
$this->expectRequest($client);
5853

5954
$client->nz = $nz;
6055
$nz->bibs = $bibs;
@@ -71,33 +66,29 @@ public function it_provides_lazy_access_to_holdings(AlmaClient $client)
7166
$this->holdings->shouldHaveType(Holdings::class);
7267
}
7368

74-
public function it_has_a_MARC_record(AlmaClient $client, UriInterface $url)
69+
public function it_has_a_MARC_record(AlmaClient $client)
7570
{
76-
$this->expectRequest($client, $url);
71+
$this->expectRequest($client);
7772

7873
$this->record->shouldHaveType(Record::class);
7974
$this->record->getField('245')->getSubfield('a')->getData()->shouldBe('Lonely hearts of the cosmos :');
8075
}
8176

82-
public function it_can_be_edited(AlmaClient $client, UriInterface $url)
77+
public function it_can_be_edited(AlmaClient $client)
8378
{
84-
$this->expectRequest($client, $url);
79+
$this->expectRequest($client);
8580

8681
$this->record->getField('245')->getSubfield('a')->setData('New title');
8782

88-
$client->putXML($url, Argument::containingString('New title'))
83+
$client->putXML('/bibs/999104760474702204', Argument::containingString('New title'))
8984
->shouldBeCalled();
9085

9186
$this->save();
9287
}
9388

94-
public function it_catches_resource_not_found(AlmaClient $client, UriInterface $url)
89+
public function it_catches_resource_not_found(AlmaClient $client)
9590
{
96-
$client->buildUrl('/bibs/999104760474702204', [])
97-
->shouldBeCalled()
98-
->willReturn($url);
99-
100-
$client->getXML($url)
91+
$client->getXML('/bibs/999104760474702204')
10192
->shouldBeCalled()
10293
->willThrow(ResourceNotFound::class);
10394

spec/Bibs/BibsSpec.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
7-
use Psr\Http\Message\UriInterface;
87
use Scriptotek\Alma\Bibs\Bib;
98
use Scriptotek\Alma\Client as AlmaClient;
109
use Scriptotek\Marc\Record;
@@ -42,13 +41,9 @@ public function it_provides_a_lazy_array_interface_to_bib_objects(AlmaClient $cl
4241
$bib->mms_id->shouldBe($mms_id);
4342
}
4443

45-
public function it_accepts_expand_parameter(AlmaClient $client, UriInterface $url)
44+
public function it_accepts_expand_parameter(AlmaClient $client)
4645
{
47-
$client->buildUrl('/bibs/12345', ['expand' => 'p_avail'])
48-
->shouldBeCalled()
49-
->willReturn($url);
50-
51-
$client->getXML($url)
46+
$client->getXML('/bibs/12345?expand=p_avail')
5247
->shouldBeCalled()
5348
->willReturn(SpecHelper::getDummyData('bib_response_with_availability.xml'));
5449

spec/Bibs/HoldingSpec.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
7-
use Psr\Http\Message\UriInterface;
87
use Scriptotek\Alma\Bibs\Bib;
98
use Scriptotek\Alma\Bibs\Holding;
109
use Scriptotek\Alma\Bibs\Item;
@@ -25,13 +24,9 @@ public function it_is_initializable()
2524
$this->shouldHaveType(Holding::class);
2625
}
2726

28-
public function it_has_items(AlmaClient $client, UriInterface $url)
27+
public function it_has_items(AlmaClient $client)
2928
{
30-
$client->buildUrl('/bibs/abc/holdings/123/items', [])
31-
->shouldBeCalled()
32-
->willReturn($url);
33-
34-
$client->getJSON($url)
29+
$client->getJSON('/bibs/abc/holdings/123/items')
3530
->shouldBeCalled()
3631
->willReturn(SpecHelper::getDummyData('items_response.json'));
3732

spec/Bibs/HoldingsSpec.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
7-
use Psr\Http\Message\UriInterface;
87
use Scriptotek\Alma\Bibs\Bib;
98
use Scriptotek\Alma\Bibs\Holding;
109
use Scriptotek\Alma\Bibs\Holdings;
@@ -86,26 +85,18 @@ public function it_provides_a_lazy_array_interface_to_holding_objects(AlmaClient
8685
$holding->holding_id->shouldBe($holding_id);
8786
}
8887

89-
public function it_is_countable(AlmaClient $client, UriInterface $url)
88+
public function it_is_countable(AlmaClient $client)
9089
{
91-
$client->buildUrl('/bibs/abc/holdings', [])
92-
->shouldBeCalled()
93-
->willReturn($url);
94-
95-
$client->getJSON($url)
90+
$client->getJSON('/bibs/abc/holdings')
9691
->shouldBeCalled()
9792
->willReturn(json_decode($this->sample));
9893

9994
$this->shouldHaveCount(2);
10095
}
10196

102-
public function it_provides_an_iterator_interface_to_holding_objects(AlmaClient $client, UriInterface $url)
97+
public function it_provides_an_iterator_interface_to_holding_objects(AlmaClient $client)
10398
{
104-
$client->buildUrl('/bibs/abc/holdings', [])
105-
->shouldBeCalled()
106-
->willReturn($url);
107-
108-
$client->getJSON($url)
99+
$client->getJSON('/bibs/abc/holdings')
109100
->shouldBeCalled()
110101
->willReturn(json_decode($this->sample));
111102

spec/Bibs/ItemSpec.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace spec\Scriptotek\Alma\Bibs;
44

55
use PhpSpec\ObjectBehavior;
6-
use Psr\Http\Message\UriInterface;
76
use Scriptotek\Alma\Bibs\Bib;
87
use Scriptotek\Alma\Bibs\Holding;
98
use Scriptotek\Alma\Bibs\Item;
@@ -30,13 +29,15 @@ public function it_is_initializable()
3029
$this->shouldHaveType(Item::class);
3130
}
3231

33-
function it_can_be_checked_out(AlmaClient $client, User $user, Library $library, UriInterface $url)
32+
function it_can_be_checked_out(AlmaClient $client, User $user, Library $library)
3433
{
35-
$client->buildUrl('/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204/loans', ['user_id' => 'Dan Michael'])
36-
->willReturn($url);
37-
38-
$client->postJSON($url, [
39-
'library' => ['value' => 'THAT LIBRARY'], 'circ_desk' => ['value' => 'DEFAULT_CIRC_DESK']])
34+
$client->postJSON(
35+
'/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204/loans?user_id=Dan+Michael',
36+
[
37+
'library' => ['value' => 'THAT LIBRARY'],
38+
'circ_desk' => ['value' => 'DEFAULT_CIRC_DESK']
39+
]
40+
)
4041
->shouldBeCalled()
4142
->willReturn(SpecHelper::getDummyData('create_loan_response.json'));
4243

@@ -47,41 +48,29 @@ function it_can_be_checked_out(AlmaClient $client, User $user, Library $library,
4748
->shouldHaveType(Loan::class);
4849
}
4950

50-
function it_can_be_on_loan(AlmaClient $client, User $user, Library $library, UriInterface $url)
51+
function it_can_be_on_loan(AlmaClient $client, User $user, Library $library)
5152
{
52-
$client->buildUrl('/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204/loans', [])
53-
->willReturn($url);
54-
55-
$client->getJSON($url)
53+
$client->getJSON('/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204/loans')
5654
->shouldBeCalled()
5755
->willReturn(SpecHelper::getDummyData('item_loan_response.json'));
5856

5957
$this->getLoan()->shouldHaveType(Loan::class);
6058
$this->loan->shouldHaveType(Loan::class);
6159
}
6260

63-
function it_can_be_available(AlmaClient $client, User $user, Library $library, UriInterface $url)
61+
function it_can_be_available(AlmaClient $client, User $user, Library $library)
6462
{
65-
$client->buildUrl('/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204/loans', [])
66-
->willReturn($url);
67-
68-
$client->getJSON($url)
63+
$client->getJSON('/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204/loans')
6964
->shouldBeCalled()
7065
->willReturn(SpecHelper::getDummyData('item_no_loan_response.json'));
7166

7267
$this->getLoan()->shouldBe(null);
7368
$this->loan->shouldBe(null);
7469
}
7570

76-
function it_can_be_scanned_in(AlmaClient $client, Library $library, UriInterface $url)
71+
function it_can_be_scanned_in(AlmaClient $client, Library $library)
7772
{
78-
$client->buildUrl('/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204', [
79-
'op' => 'scan',
80-
'library' => 'THAT LIBRARY',
81-
'circ_desk' => 'DEFAULT_CIRC_DESK',
82-
])->willReturn($url);
83-
84-
$client->postJSON($url)
73+
$client->postJSON('/bibs/990006312214702204/holdings/22163771200002204/items/23163771190002204?op=scan&library=THAT+LIBRARY&circ_desk=DEFAULT_CIRC_DESK')
8574
->shouldBeCalled()
8675
->willReturn(SpecHelper::getDummyData('scanin_transit_response.json'));
8776

0 commit comments

Comments
 (0)