/** * @test */ public function update_will_edit_an_existing_segment() { $client = $this->prophesize(Auth::class); $repository = new SegmentBillingRepository($client->reveal()); $segment = new SegmentBilling(); $segment->setId(123456); $responseBody = json_encode(['response' => ['status' => 'OK']]); $fakeResponse = $this->prophesize(Response::class); $stream = $this->prophesize(Stream::class); $stream->getContents()->willReturn($responseBody); $stream->rewind()->willReturn(null)->shouldBeCalled(); $fakeResponse->getBody()->willReturn($stream->reveal()); $payload = ['segment-billing-category' => $segment->toArray()]; $client->request('PUT', Argument::any(), ['body' => json_encode($payload)])->willReturn($fakeResponse)->shouldBeCalled(); $repositoryResponse = $repository->update($segment); $this->assertTrue($repositoryResponse->isSuccessful()); }
/** * @param SegmentBilling $segmentBilling * * @return RepositoryResponse * @throws RepositoryException */ public function add(SegmentBilling $segmentBilling) { $compiledUrl = $this->baseUrl . '?member_id=' . $segmentBilling->getMemberId(); $payload = ['segment-billing-category' => $segmentBilling->toArray()]; $response = $this->client->request('POST', $compiledUrl, ['body' => json_encode($payload)]); $repositoryResponse = RepositoryResponse::fromResponse($response); if ($repositoryResponse->isSuccessful()) { $stream = $response->getBody(); $responseContent = json_decode($stream->getContents(), true); $stream->rewind(); if (count($responseContent['response']['segment-billing-category']) == 0) { throw RepositoryException::missingSegmentBillingContent(); } if (!isset($responseContent['response']['segment-billing-category'][0]['id'])) { throw RepositoryException::wrongFormat(serialize($responseContent)); } $segmentBilling->setId($responseContent['response']['segment-billing-category'][0]['id']); } return $repositoryResponse; }