/** * @param Segment $segment * * @return RepositoryResponse * @throws RepositoryException */ public function add(Segment $segment) { $compiledUrl = $this->baseUrl . $segment->getMemberId(); $payload = ['segment' => $segment->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 (!isset($responseContent['response']['segment']['id'])) { throw RepositoryException::wrongFormat(serialize($responseContent)); } $segment->setId($responseContent['response']['segment']['id']); } return $repositoryResponse; }
/** * @test */ public function update_will_edit_an_existing_segment() { $client = $this->prophesize(Auth::class); $repository = new SegmentRepository($client->reveal()); $segment = new Segment(); $segment->setName('Test segment'); $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' => $segment->toArray()]; $client->request('PUT', Argument::any(), ['body' => json_encode($payload)])->willReturn($fakeResponse)->shouldBeCalled(); $repositoryResponse = $repository->update($segment); $this->assertTrue($repositoryResponse->isSuccessful()); }