Skip to content

Commit

Permalink
feat: add more methods
Browse files Browse the repository at this point in the history
  • Loading branch information
brokeyourbike committed Dec 19, 2021
1 parent 610a916 commit 7e06a2e
Show file tree
Hide file tree
Showing 18 changed files with 634 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[![Total Downloads](https://poser.pugx.org/brokeyourbike/twilio-api-client/downloads)](https://packagist.org/packages/brokeyourbike/twilio-api-client)
[![License: MPL-2.0](https://img.shields.io/badge/license-MPL--2.0-purple.svg)](https://github.com/brokeyourbike/twilio-api-client-php/blob/main/LICENSE)


[![Maintainability](https://api.codeclimate.com/v1/badges/cbab9f6ee2fde9c9b0c8/maintainability)](https://codeclimate.com/github/brokeyourbike/twilio-api-client-php/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/cbab9f6ee2fde9c9b0c8/test_coverage)](https://codeclimate.com/github/brokeyourbike/twilio-api-client-php/test_coverage)
[![tests](https://github.com/brokeyourbike/twilio-api-client-php/actions/workflows/tests.yml/badge.svg)](https://github.com/brokeyourbike/twilio-api-client-php/actions/workflows/tests.yml)

Twilio API Client for PHP
Expand Down
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,12 @@
"brokeyourbike/resolve-uri": "^1.0",
"myclabs/php-enum": "^1.8",
"brokeyourbike/http-enums": "^1.0",
"brokeyourbike/has-source-model": "^2.0",
"psr/simple-cache": "^1.0",
"brokeyourbike/data-transfer-object": "^0.1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"phpunit/phpunit": "^9",
"mockery/mockery": "^1.4",
"nesbot/carbon": "^2.53"
"mockery/mockery": "^1.4"
},
"version": "1.0.0"
"version": "0.0.0"
}
27 changes: 27 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
parameters:
ignoreErrors:
-
message: "#^Constant BrokeYourBike\\\\Twilio\\\\Enums\\\\CallerTypeEnum\\:\\:BUSINESS is unused\\.$#"
count: 1
path: src/Enums/CallerTypeEnum.php

-
message: "#^Constant BrokeYourBike\\\\Twilio\\\\Enums\\\\CallerTypeEnum\\:\\:CONSUMER is unused\\.$#"
count: 1
path: src/Enums/CallerTypeEnum.php

-
message: "#^Constant BrokeYourBike\\\\Twilio\\\\Enums\\\\CarrierTypeEnum\\:\\:LANDLINE is unused\\.$#"
count: 1
path: src/Enums/CarrierTypeEnum.php

-
message: "#^Constant BrokeYourBike\\\\Twilio\\\\Enums\\\\CarrierTypeEnum\\:\\:MOBILE is unused\\.$#"
count: 1
path: src/Enums/CarrierTypeEnum.php

-
message: "#^Constant BrokeYourBike\\\\Twilio\\\\Enums\\\\CarrierTypeEnum\\:\\:VOIP is unused\\.$#"
count: 1
path: src/Enums/CarrierTypeEnum.php

3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
includes:
- phpstan-baseline.neon

parameters:
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
Expand Down
32 changes: 32 additions & 0 deletions src/Models/PhoneNumberWithCarrierResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\Twilio\Models;

use Spatie\DataTransferObject\Attributes\MapFrom;
use BrokeYourBike\Twilio\Models\Carrier;
use BrokeYourBike\DataTransferObject\JsonResponse;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class PhoneNumberWithCarrierResponse extends JsonResponse
{
public Carrier $carrier;

#[MapFrom('country_code')]
public string $countryCode;

#[MapFrom('national_format')]
public string $nationalFormat;

#[MapFrom('phone_number')]
public string $phoneNumber;

public string $url;
}
9 changes: 5 additions & 4 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
abstract class Options implements \IteratorAggregate
abstract class Options
{
protected $options = [];
protected array $options = [];

public function getIterator(): \Traversable {
return new \ArrayIterator($this->options);
public function toArray(): array
{
return $this->options;
}
}

28 changes: 23 additions & 5 deletions src/V1/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\ClientInterface;
use BrokeYourBike\Twilio\Options;
use BrokeYourBike\Twilio\Models\PhoneNumberWithCarrierResponse;
use BrokeYourBike\Twilio\Models\PhoneNumberResponse;
use BrokeYourBike\Twilio\Models\PhoneNumberOptions;
use BrokeYourBike\Twilio\Interfaces\ConfigInterface;
Expand All @@ -35,16 +36,33 @@ public function __construct(ConfigInterface $config, ClientInterface $httpClient
$this->httpClient = $httpClient;
}

public function fetchPhoneNumber(string $phoneNumber, PhoneNumberOptions $options): PhoneNumberResponse
public function lookupPhoneNumberWithCarrier(string $phoneNumber, ?string $countryCode = null): PhoneNumberWithCarrierResponse
{
$response = $this->performRequest(HttpMethodEnum::GET(), "PhoneNumbers/{$phoneNumber}", $options);
$options = (new PhoneNumberOptions())->setType('carrier');

if ($countryCode !== null) {
$options->setCountryCode($countryCode);
}

$response = $this->lookupPhoneNumberRaw($phoneNumber, $options);
return new PhoneNumberWithCarrierResponse($response);
}

public function lookupPhoneNumber(string $phoneNumber, ?PhoneNumberOptions $options = null): PhoneNumberResponse
{
$response = $this->lookupPhoneNumberRaw($phoneNumber, $options ?? new PhoneNumberOptions());
return new PhoneNumberResponse($response);
}

public function lookupPhoneNumberRaw(string $phoneNumber, ?PhoneNumberOptions $options = null): ResponseInterface
{
return $this->performRequest(HttpMethodEnum::GET(), "v1/PhoneNumbers/{$phoneNumber}", $options ?? new PhoneNumberOptions());
}

/**
* @param HttpMethodEnum $method
* @param string $uri
* @param array<mixed> $data
* @param Options $data
* @return ResponseInterface
*/
private function performRequest(HttpMethodEnum $method, string $uri, Options $data): ResponseInterface
Expand All @@ -61,9 +79,9 @@ private function performRequest(HttpMethodEnum $method, string $uri, Options $da
];

if (HttpMethodEnum::GET()->equals($method)) {
$options[\GuzzleHttp\RequestOptions::QUERY] = $data;
$options[\GuzzleHttp\RequestOptions::QUERY] = $data->toArray();
} elseif (HttpMethodEnum::POST()->equals($method)) {
$options[\GuzzleHttp\RequestOptions::JSON] = $data;
$options[\GuzzleHttp\RequestOptions::JSON] = $data->toArray();
}

$uri = (string) $this->resolveUriFor($this->config->getUrl(), $uri);
Expand Down
55 changes: 55 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\Twilio\Tests;

use PHPUnit\Framework\TestCase;
use BrokeYourBike\Twilio\V1\Client;
use BrokeYourBike\Twilio\Interfaces\ConfigInterface;
use BrokeYourBike\ResolveUri\ResolveUriTrait;
use BrokeYourBike\HttpClient\HttpClientTrait;
use BrokeYourBike\HttpClient\HttpClientInterface;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class ClientTest extends TestCase
{
/** @test */
public function it_implemets_http_client_interface(): void
{
/** @var ConfigInterface */
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();

/** @var \GuzzleHttp\ClientInterface */
$mockedHttpClient = $this->getMockBuilder(\GuzzleHttp\ClientInterface::class)->getMock();

/** @var \Psr\SimpleCache\CacheInterface */
$mockedCache = $this->getMockBuilder(\Psr\SimpleCache\CacheInterface::class)->getMock();

$api = new Client($mockedConfig, $mockedHttpClient, $mockedCache);

$this->assertInstanceOf(HttpClientInterface::class, $api);
}

/** @test */
public function it_uses_http_client_trait(): void
{
$usedTraits = class_uses(Client::class);

$this->assertArrayHasKey(HttpClientTrait::class, $usedTraits);
}

/** @test */
public function it_uses_resolve_uri_trait(): void
{
$usedTraits = class_uses(Client::class);

$this->assertArrayHasKey(ResolveUriTrait::class, $usedTraits);
}
}
37 changes: 37 additions & 0 deletions tests/Enums/CallerTypeEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\Twilio\Tests\Enums;

use PHPUnit\Framework\TestCase;
use BrokeYourBike\Twilio\Enums\CallerTypeEnum;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class CallerTypeEnumTest extends TestCase
{
/** @test */
public function it_extends_myclabs_enum(): void
{
$parent = get_parent_class(CallerTypeEnum::class);

$this->assertSame(\MyCLabs\Enum\Enum::class, $parent);
}

/** @test */
public function it_has_not_duplicate_values()
{
$allValuesRaw = CallerTypeEnum::toArray();
$this->assertNotEmpty($allValuesRaw);

$uniqueValuesraw = array_unique($allValuesRaw, SORT_STRING);

$this->assertEquals($allValuesRaw, $uniqueValuesraw);
}
}
37 changes: 37 additions & 0 deletions tests/Enums/CarrierTypeEnumTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\Twilio\Tests\Enums;

use PHPUnit\Framework\TestCase;
use BrokeYourBike\Twilio\Enums\CarrierTypeEnum;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class CarrierTypeEnumTest extends TestCase
{
/** @test */
public function it_extends_myclabs_enum(): void
{
$parent = get_parent_class(CarrierTypeEnum::class);

$this->assertSame(\MyCLabs\Enum\Enum::class, $parent);
}

/** @test */
public function it_has_not_duplicate_values()
{
$allValuesRaw = CarrierTypeEnum::toArray();
$this->assertNotEmpty($allValuesRaw);

$uniqueValuesraw = array_unique($allValuesRaw, SORT_STRING);

$this->assertEquals($allValuesRaw, $uniqueValuesraw);
}
}
73 changes: 73 additions & 0 deletions tests/LookupPhoneNumberRawTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

// Copyright (C) 2021 Ivan Stasiuk <brokeyourbike@gmail.com>.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

namespace BrokeYourBike\Twilio\Tests;

use Psr\SimpleCache\CacheInterface;
use Psr\Http\Message\ResponseInterface;
use BrokeYourBike\Twilio\V1\Client;
use BrokeYourBike\Twilio\Interfaces\ConfigInterface;

/**
* @author Ivan Stasiuk <brokeyourbike@gmail.com>
*/
class LookupPhoneNumberRawTest extends TestCase
{
private string $sid = '12345';
private string $secret = 'super-secure-token';
private string $phone = '+123456789';

/** @test */
public function it_can_prepare_request(): void
{
$mockedConfig = $this->getMockBuilder(ConfigInterface::class)->getMock();
$mockedConfig->method('getUrl')->willReturn('https://api.example/');
$mockedConfig->method('getSid')->willReturn($this->sid);
$mockedConfig->method('getSecret')->willReturn($this->secret);

$mockedResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
$mockedResponse->method('getStatusCode')->willReturn(200);
$mockedResponse->method('getBody')
->willReturn('{
"url":"https://api.example/v1/PhoneNumbers/'. $this->phone .'"
"caller_name":null,
"country_code":"US",
"phone_number":"'. $this->phone .'",
"national_format":"012 345 6789",
"add_ons":null
}');

/** @var \Mockery\MockInterface $mockedClient */
$mockedClient = \Mockery::mock(\GuzzleHttp\Client::class);
$mockedClient->shouldReceive('request')->withArgs([
'GET',
'https://api.example/v1/PhoneNumbers/+123456789',
[
\GuzzleHttp\RequestOptions::HTTP_ERRORS => false,
\GuzzleHttp\RequestOptions::HEADERS => [
'Accept' => 'application/json',
],
\GuzzleHttp\RequestOptions::AUTH => [
$this->sid,
$this->secret,
],
\GuzzleHttp\RequestOptions::QUERY => [],
],
])->once()->andReturn($mockedResponse);

/**
* @var ConfigInterface $mockedConfig
* @var \GuzzleHttp\Client $mockedClient
* */
$api = new Client($mockedConfig, $mockedClient);

$requestResult = $api->lookupPhoneNumberRaw($this->phone);

$this->assertInstanceOf(ResponseInterface::class, $requestResult);
}
}
Loading

0 comments on commit 7e06a2e

Please sign in to comment.