Skip to content

Commit 9a91bb1

Browse files
authored
Merge pull request #123 from nicklaw5/v5-release
2 parents 391a196 + 07449c3 commit 9a91bb1

19 files changed

+441
-92
lines changed

spec/NewTwitchApi/NewTwitchApiSpec.php

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use GuzzleHttp\Client;
66
use NewTwitchApi\RequestGenerator;
77
use NewTwitchApi\Auth\OauthApi;
8+
use NewTwitchApi\Resources\AdsApi;
89
use NewTwitchApi\Resources\AnalyticsApi;
910
use NewTwitchApi\Resources\BitsApi;
1011
use NewTwitchApi\Resources\ChannelPointsApi;
@@ -37,6 +38,11 @@ function it_should_provide_oauth_api()
3738
$this->getOauthApi()->shouldBeAnInstanceOf(OauthApi::class);
3839
}
3940

41+
function it_should_provide_ads_api()
42+
{
43+
$this->getAdsApi()->shouldBeAnInstanceOf(AdsApi::class);
44+
}
45+
4046
function it_should_provide_analytics_api()
4147
{
4248
$this->getAnalyticsApi()->shouldBeAnInstanceOf(AnalyticsApi::class);
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace spec\NewTwitchApi\Resources;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
8+
use NewTwitchApi\RequestGenerator;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class AdsApiSpec extends ObjectBehavior
12+
{
13+
function let(Client $guzzleClient, RequestGenerator $requestGenerator, Request $request, Response $response)
14+
{
15+
$this->beConstructedWith($guzzleClient, $requestGenerator);
16+
$guzzleClient->send($request)->willReturn($response);
17+
}
18+
19+
function it_should_start_commercial(RequestGenerator $requestGenerator, Request $request, Response $response)
20+
{
21+
$requestGenerator->generate('POST', 'channels/commercial', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'length', 'value' => 30]])->willReturn($request);
22+
$this->startCommercial('TEST_TOKEN', '123', 30)->shouldBe($response);
23+
}
24+
}

spec/NewTwitchApi/Resources/ChannelPointsApiSpec.php

+42
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,51 @@ function it_should_get_custom_reward_redemption_by_ids(RequestGenerator $request
7070
$this->getCustomRewardRedemption('TEST_TOKEN', '123', '321', ['111', '222'])->shouldBe($response);
7171
}
7272

73+
function it_should_create_custom_reward(RequestGenerator $requestGenerator, Request $request, Response $response)
74+
{
75+
$requestGenerator->generate('POST', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123'], ['key' => 'cost', 'value' => 100]])->willReturn($request);
76+
$this->createCustomReward('TEST_TOKEN', '123', 'test 123', 100)->shouldBe($response);
77+
}
78+
79+
function it_should_create_custom_reward_with_one_opt(RequestGenerator $requestGenerator, Request $request, Response $response)
80+
{
81+
$requestGenerator->generate('POST', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123'], ['key' => 'cost', 'value' => 100], ['key' => 'prompt', 'value' => 'What is your name?']])->willReturn($request);
82+
$this->createCustomReward('TEST_TOKEN', '123', 'test 123', 100, ['prompt' => 'What is your name?'])->shouldBe($response);
83+
}
84+
85+
function it_should_create_custom_reward_with_multiple_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
86+
{
87+
$requestGenerator->generate('POST', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123'], ['key' => 'cost', 'value' => 100], ['key' => 'prompt', 'value' => 'What is your name?'], ['key' => 'is_enabled', 'value' => 1]])->willReturn($request);
88+
$this->createCustomReward('TEST_TOKEN', '123', 'test 123', 100, ['prompt' => 'What is your name?', 'is_enabled' => 1])->shouldBe($response);
89+
}
90+
91+
function it_should_update_custom_reward(RequestGenerator $requestGenerator, Request $request, Response $response)
92+
{
93+
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [])->willReturn($request);
94+
$this->updateCustomReward('TEST_TOKEN', '123', '321')->shouldBe($response);
95+
}
96+
97+
function it_should_update_custom_reward_with_one_opt(RequestGenerator $requestGenerator, Request $request, Response $response)
98+
{
99+
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [['key' => 'prompt', 'value' => 'What is your name?']])->willReturn($request);
100+
$this->updateCustomReward('TEST_TOKEN', '123', '321', ['prompt' => 'What is your name?'])->shouldBe($response);
101+
}
102+
103+
function it_should_update_custom_reward_with_multiple_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
104+
{
105+
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [['key' => 'prompt', 'value' => 'What is your name?'], ['key' => 'is_enabled', 'value' => 1]])->willReturn($request);
106+
$this->updateCustomReward('TEST_TOKEN', '123', '321', ['prompt' => 'What is your name?', 'is_enabled' => 1])->shouldBe($response);
107+
}
108+
73109
function it_should_delete_custom_reward(RequestGenerator $requestGenerator, Request $request, Response $response)
74110
{
75111
$requestGenerator->generate('DELETE', 'channel_points/custom_rewards', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '321']], [])->willReturn($request);
76112
$this->deleteCustomReward('TEST_TOKEN', '123', '321')->shouldBe($response);
77113
}
114+
115+
function it_should_update_redemption_status(RequestGenerator $requestGenerator, Request $request, Response $response)
116+
{
117+
$requestGenerator->generate('PATCH', 'channel_points/custom_rewards/redemptions', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'reward_id', 'value' => '456'], ['key' => 'id', 'value' => '789']], [['key' => 'status', 'value' => 'FULFILLED']])->willReturn($request);
118+
$this->updateRedemptionStatus('TEST_TOKEN', '123', '456', '789', 'FULFILLED')->shouldBe($response);
119+
}
78120
}

spec/NewTwitchApi/Resources/ChannelsApiSpec.php

+30
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,34 @@ function it_should_get_channel_editors(RequestGenerator $requestGenerator, Reque
2727
$requestGenerator->generate('GET', 'channels/editors', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
2828
$this->getChannelEditors('TEST_TOKEN', '123')->shouldBe($response);
2929
}
30+
31+
function it_should_modify_channel_with_game_id(RequestGenerator $requestGenerator, Request $request, Response $response)
32+
{
33+
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'game_id', 'value' => '0']])->willReturn($request);
34+
$this->modifyChannelInfo('TEST_TOKEN', '123', ['game_id' => '0'])->shouldBe($response);
35+
}
36+
37+
function it_should_modify_channel_with_language(RequestGenerator $requestGenerator, Request $request, Response $response)
38+
{
39+
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'broadcaster_language', 'value' => 'en']])->willReturn($request);
40+
$this->modifyChannelInfo('TEST_TOKEN', '123', ['broadcaster_language' => 'en'])->shouldBe($response);
41+
}
42+
43+
function it_should_modify_channel_with_title(RequestGenerator $requestGenerator, Request $request, Response $response)
44+
{
45+
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'title', 'value' => 'test 123']])->willReturn($request);
46+
$this->modifyChannelInfo('TEST_TOKEN', '123', ['title' => 'test 123'])->shouldBe($response);
47+
}
48+
49+
function it_should_modify_channel_with_delay(RequestGenerator $requestGenerator, Request $request, Response $response)
50+
{
51+
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'delay', 'value' => 5]])->willReturn($request);
52+
$this->modifyChannelInfo('TEST_TOKEN', '123', ['delay' => 5])->shouldBe($response);
53+
}
54+
55+
function it_should_modify_channel_with_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
56+
{
57+
$requestGenerator->generate('PATCH', 'channels', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'game_id', 'value' => '0'], ['key' => 'broadcaster_language', 'value' => 'en'], ['key' => 'title', 'value' => 'test 123'], ['key' => 'delay', 'value' => 5]])->willReturn($request);
58+
$this->modifyChannelInfo('TEST_TOKEN', '123', ['game_id' => '0', 'broadcaster_language' => 'en', 'title' => 'test 123', 'delay' => 5])->shouldBe($response);
59+
}
3060
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace spec\NewTwitchApi\Resources;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Psr7\Request;
7+
use GuzzleHttp\Psr7\Response;
8+
use NewTwitchApi\RequestGenerator;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class ModerationApiSpec extends ObjectBehavior
12+
{
13+
function let(Client $guzzleClient, RequestGenerator $requestGenerator, Request $request, Response $response)
14+
{
15+
$this->beConstructedWith($guzzleClient, $requestGenerator);
16+
$guzzleClient->send($request)->willReturn($response);
17+
}
18+
19+
function it_should_check_automod_status(RequestGenerator $requestGenerator, Request $request, Response $response)
20+
{
21+
$requestGenerator->generate('POST', 'moderation/enforcements/status', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'msg_id', 'value' => '456'], ['key' => 'msg_text', 'value' => 'test 123'], ['key' => 'user_id', 'value' => '789']])->willReturn($request);
22+
$this->checkAutoModStatus('TEST_TOKEN', '123', '456', 'test 123', '789')->shouldBe($response);
23+
}
24+
25+
function it_should_release_held_message(RequestGenerator $requestGenerator, Request $request, Response $response)
26+
{
27+
$requestGenerator->generate('POST', 'moderation/automod/message', 'TEST_TOKEN', [], [['key' => 'user_id', 'value' => '123'], ['key' => 'msg_id', 'value' => '456'], ['key' => 'action', 'value' => 'ALLOW']])->willReturn($request);
28+
$this->manageHeldAutoModMessage('TEST_TOKEN', '123', '456', 'ALLOW')->shouldBe($response);
29+
}
30+
}

spec/NewTwitchApi/Resources/PollsApiSpec.php

+18
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,22 @@ function it_should_get_polls_with_opts(RequestGenerator $requestGenerator, Reque
3939
$requestGenerator->generate('GET', 'polls', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'after', 'value' => 'abc'], ['key' => 'first', 'value' => 100]], [])->willReturn($request);
4040
$this->getPolls('TEST_TOKEN', '123', [], 'abc', 100)->shouldBe($response);
4141
}
42+
43+
function it_should_create_a_poll(RequestGenerator $requestGenerator, Request $request, Response $response)
44+
{
45+
$requestGenerator->generate('POST', 'polls', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'title', 'value' => 'What is my name?'], ['key' => 'choices', 'value' => [['title' => 'John'], ['title' => 'Doe']]], ['key' => 'duration', 'value' => 15]])->willReturn($request);
46+
$this->createPoll('TEST_TOKEN', '123', 'What is my name?', [['title' => 'John'], ['title' => 'Doe']], 15)->shouldBe($response);
47+
}
48+
49+
function it_should_create_a_poll_with_opts(RequestGenerator $requestGenerator, Request $request, Response $response)
50+
{
51+
$requestGenerator->generate('POST', 'polls', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'title', 'value' => 'What is my name?'], ['key' => 'choices', 'value' => [['title' => 'John'], ['title' => 'Doe']]], ['key' => 'duration', 'value' => 15], ['key' => 'bits_voting_enabled', 'value' => 1]])->willReturn($request);
52+
$this->createPoll('TEST_TOKEN', '123', 'What is my name?', [['title' => 'John'], ['title' => 'Doe']], 15, ['bits_voting_enabled' => 1])->shouldBe($response);
53+
}
54+
55+
function it_should_end_a_poll(RequestGenerator $requestGenerator, Request $request, Response $response)
56+
{
57+
$requestGenerator->generate('PATCH', 'polls', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '456'], ['key' => 'status', 'value' => 'TERMINATED']])->willReturn($request);
58+
$this->endPoll('TEST_TOKEN', '123', '456', 'TERMINATED')->shouldBe($response);
59+
}
4260
}

spec/NewTwitchApi/Resources/PredictionsApiSpec.php

+18
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,22 @@ function it_should_get_predictions_with_opts(RequestGenerator $requestGenerator,
3939
$requestGenerator->generate('GET', 'predictions', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'after', 'value' => 'abc'], ['key' => 'first', 'value' => 100]], [])->willReturn($request);
4040
$this->getPredictions('TEST_TOKEN', '123', [], 'abc', 100)->shouldBe($response);
4141
}
42+
43+
function it_should_create_a_prediction(RequestGenerator $requestGenerator, Request $request, Response $response)
44+
{
45+
$requestGenerator->generate('POST', 'predictions', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'title', 'value' => 'Will the coin land on heads or tails?'], ['key' => 'outcomes', 'value' => [['title' => 'Heads'], ['title' => 'Tails']]], ['key' => 'prediction_window', 'value' => 15]])->willReturn($request);
46+
$this->createPrediction('TEST_TOKEN', '123', 'Will the coin land on heads or tails?', [['title' => 'Heads'], ['title' => 'Tails']], 15)->shouldBe($response);
47+
}
48+
49+
function it_should_end_a_prediction(RequestGenerator $requestGenerator, Request $request, Response $response)
50+
{
51+
$requestGenerator->generate('PATCH', 'predictions', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '456'], ['key' => 'status', 'value' => 'CANCELLED']])->willReturn($request);
52+
$this->endPrediction('TEST_TOKEN', '123', '456', 'CANCELLED')->shouldBe($response);
53+
}
54+
55+
function it_should_resolve_a_prediction(RequestGenerator $requestGenerator, Request $request, Response $response)
56+
{
57+
$requestGenerator->generate('PATCH', 'predictions', 'TEST_TOKEN', [], [['key' => 'broadcaster_id', 'value' => '123'], ['key' => 'id', 'value' => '456'], ['key' => 'status', 'value' => 'RESOLVED'], ['key' => 'winning_outcome_id', 'value' => '1']])->willReturn($request);
58+
$this->endPrediction('TEST_TOKEN', '123', '456', 'RESOLVED', '1')->shouldBe($response);
59+
}
4260
}

spec/NewTwitchApi/Resources/StreamsApiSpec.php

+12
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,16 @@ function it_should_get_followed_streams_with_opts(RequestGenerator $requestGener
142142
$requestGenerator->generate('GET', 'streams/followed', 'TEST_TOKEN', [['key' => 'user_id', 'value' => '123'], ['key' => 'first', 'value' => 100], ['key' => 'after', 'value' => 'abc']], [])->willReturn($request);
143143
$this->getFollowedStreams('TEST_TOKEN', '123', 100, 'abc')->shouldBe($response);
144144
}
145+
146+
function it_should_create_stream_marker(RequestGenerator $requestGenerator, Request $request, Response $response)
147+
{
148+
$requestGenerator->generate('POST', 'streams/markers', 'TEST_TOKEN', [], [['key' => 'user_id', 'value' => '123']])->willReturn($request);
149+
$this->createStreamMarker('TEST_TOKEN', '123')->shouldBe($response);
150+
}
151+
152+
function it_should_create_stream_marker_with_description(RequestGenerator $requestGenerator, Request $request, Response $response)
153+
{
154+
$requestGenerator->generate('POST', 'streams/markers', 'TEST_TOKEN', [], [['key' => 'user_id', 'value' => '123'], ['key' => 'description', 'value' => 'This is a marker']])->willReturn($request);
155+
$this->createStreamMarker('TEST_TOKEN', '123', 'This is a marker')->shouldBe($response);
156+
}
145157
}

spec/NewTwitchApi/Resources/TagsApiSpec.php

+18
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,22 @@ function it_should_get_stream_tags(RequestGenerator $requestGenerator, Request $
4646
$requestGenerator->generate('GET', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
4747
$this->getStreamTags('TEST_TOKEN', '123')->shouldBe($response);
4848
}
49+
50+
function it_should_replace_stream_tags(RequestGenerator $requestGenerator, Request $request, Response $response)
51+
{
52+
$requestGenerator->generate('PUT', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [])->willReturn($request);
53+
$this->replaceStreamTags('TEST_TOKEN', '123')->shouldBe($response);
54+
}
55+
56+
function it_should_replace_stream_tags_with_one_tag(RequestGenerator $requestGenerator, Request $request, Response $response)
57+
{
58+
$requestGenerator->generate('PUT', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'tag_ids', 'value' => ['456']]])->willReturn($request);
59+
$this->replaceStreamTags('TEST_TOKEN', '123', ['456'])->shouldBe($response);
60+
}
61+
62+
function it_should_replace_stream_tags_with_multiple_tags(RequestGenerator $requestGenerator, Request $request, Response $response)
63+
{
64+
$requestGenerator->generate('PUT', 'streams/tags', 'TEST_TOKEN', [['key' => 'broadcaster_id', 'value' => '123']], [['key' => 'tag_ids', 'value' => ['456', '789']]])->willReturn($request);
65+
$this->replaceStreamTags('TEST_TOKEN', '123', ['456', '789'])->shouldBe($response);
66+
}
4967
}

src/NewTwitchApi/NewTwitchApi.php

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use GuzzleHttp\Client;
88
use NewTwitchApi\Auth\OauthApi;
9+
use NewTwitchApi\Resources\AdsApi;
910
use NewTwitchApi\Resources\AnalyticsApi;
1011
use NewTwitchApi\Resources\BitsApi;
1112
use NewTwitchApi\Resources\ChannelPointsApi;
@@ -31,6 +32,7 @@
3132
class NewTwitchApi
3233
{
3334
private $oauthApi;
35+
private $adsApi;
3436
private $analyticsApi;
3537
private $bitsApi;
3638
private $channelPointsApi;
@@ -57,6 +59,7 @@ public function __construct(Client $helixGuzzleClient, string $clientId, string
5759
{
5860
$requestGenerator = new RequestGenerator();
5961
$this->oauthApi = new OauthApi($clientId, $clientSecret, $authGuzzleClient);
62+
$this->adsApi = new AdsApi($helixGuzzleClient, $requestGenerator);
6063
$this->analyticsApi = new AnalyticsApi($helixGuzzleClient, $requestGenerator);
6164
$this->bitsApi = new BitsApi($helixGuzzleClient, $requestGenerator);
6265
$this->channelPointsApi = new ChannelPointsApi($helixGuzzleClient, $requestGenerator);
@@ -85,6 +88,11 @@ public function getOauthApi(): OauthApi
8588
return $this->oauthApi;
8689
}
8790

91+
public function getAdsApi(): AdsApi
92+
{
93+
return $this->adsApi;
94+
}
95+
8896
public function getAnalyticsApi(): AnalyticsApi
8997
{
9098
return $this->analyticsApi;

src/NewTwitchApi/Resources/AdsApi.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace NewTwitchApi\Resources;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use Psr\Http\Message\ResponseInterface;
9+
10+
class AdsApi extends AbstractResource
11+
{
12+
/**
13+
* @throws GuzzleException
14+
* @link https://dev.twitch.tv/docs/api/reference#start-commercial
15+
*/
16+
public function startCommercial(string $bearer, string $broadcasterId, int $length): ResponseInterface
17+
{
18+
$bodyParamsMap = [];
19+
20+
$bodyParamsMap[] = ['key' => 'broadcaster_id', 'value' => $broadcasterId];
21+
$bodyParamsMap[] = ['key' => 'length', 'value' => $length];
22+
23+
return $this->postApi('channels/commercial', $bearer, [], $bodyParamsMap);
24+
}
25+
}

0 commit comments

Comments
 (0)