/**
  * @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     $memberId
  * @param int $start
  * @param int $maxResults
  *
  * @return SegmentBilling[]|null
  * @throws RepositoryException
  */
 public function findAll($memberId, $start = 0, $maxResults = 100)
 {
     $cacheKey = self::CACHE_NAMESPACE . sha1($memberId . $start . $maxResults);
     if ($this->isCacheEnabled()) {
         if ($this->cache->contains($cacheKey)) {
             return $this->cache->fetch($cacheKey);
         }
     }
     $compiledUrl = $this->baseUrl . '?member_id=' . $memberId . '&start_element=' . $start . '&num_elements=' . $maxResults;
     $response = $this->client->request('GET', $compiledUrl);
     $repositoryResponse = RepositoryResponse::fromResponse($response);
     if (!$repositoryResponse->isSuccessful()) {
         throw RepositoryException::failed($repositoryResponse);
     }
     $stream = $response->getBody();
     $responseContent = json_decode($stream->getContents(), true);
     $stream->rewind();
     $result = [];
     if (!$responseContent['response']['segment-billing-categories']) {
         $responseContent['response']['segment-billing-categories'] = [];
     }
     foreach ($responseContent['response']['segment-billing-categories'] as $segmentBillingArray) {
         $result[] = SegmentBilling::fromArray($segmentBillingArray);
     }
     if ($this->isCacheEnabled()) {
         $this->cache->save($cacheKey, $result, self::CACHE_EXPIRATION);
     }
     return $result;
 }
 /**
  * @param SegmentBilling $segment
  *
  * @return self
  */
 public static function missingSegmentBillingId($segment)
 {
     return new self('Missing segment billing id for ' . serialize($segment->getId()));
 }
 /**
  * @test
  */
 public function delete_will_remove_a_segment()
 {
     $repositorySegment = $this->getSegmentRepository();
     $segment = new Segment();
     $segment->setName('Test segment' . uniqid());
     $segment->setCategory('Test category' . uniqid());
     $segment->setMemberId(getenv('MEMBER_ID'));
     $segment->setActive(true);
     $repositoryResponseSegment = $repositorySegment->add($segment);
     $this->assertTrue($repositoryResponseSegment->isSuccessful(), $repositoryResponseSegment->getError()->getError());
     $this->assertNotNull($segment->getId());
     $repository = $this->getSegmentBillingRepository();
     $segmentBilling = new SegmentBilling();
     $segmentBilling->setMemberId(getenv('MEMBER_ID'));
     $segmentBilling->setActive(true);
     $segmentBilling->setDataCategoryId(getenv('DATA_CATEGORY_ID'));
     $segmentBilling->setDataProviderId(getenv('DATA_PROVIDER_ID'));
     $segmentBilling->setSegmentId($segment->getId());
     $segmentBilling->setIsPublic(true);
     $repositoryResponse = $repository->add($segmentBilling);
     $this->assertTrue($repositoryResponse->isSuccessful(), $repositoryResponse->getError()->getError());
     $repositoryResponse = $repository->remove($segmentBilling->getMemberId(), $segmentBilling->getId());
     $this->assertTrue($repositoryResponse->isSuccessful(), $repositoryResponse->getError()->getError());
 }