diff --git a/.travis.yml b/.travis.yml index 981566b..4a621a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,17 +3,13 @@ language: php sudo: false php: - - 7.1 - - 7.2 - 7.3 - - 7.4 + - 7.4 + - 8.0 env: - - SYMFONY_VERSION=4.0.* - - SYMFONY_VERSION=4.1.* - - SYMFONY_VERSION=4.2.* + - SYMFONY_VERSION=4.4.* - SYMFONY_VERSION=4.3.* - - SYMFONY_VERSION=4.4.* before_install: - composer self-update @@ -25,6 +21,7 @@ install: script: - ./vendor/bin/phpcs ./ -p --encoding=utf-8 --extensions=php --ignore="vendor|Tests" --standard=./vendor/escapestudios/symfony2-coding-standard/Symfony - ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.xml + - ./vendor/bin/phpstan analyse -l 8 --no-progress -c phpstan.neon ./ after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/Controller/SinchController.php b/Controller/SinchController.php index c450f55..81bbaec 100644 --- a/Controller/SinchController.php +++ b/Controller/SinchController.php @@ -12,7 +12,6 @@ namespace Fresh\SinchBundle\Controller; -use Fresh\SinchBundle\Event\SinchEvents; use Fresh\SinchBundle\Event\SmsMessageCallbackEvent; use Fresh\SinchBundle\Form\Type\CallbackRequestType; use Fresh\SinchBundle\Model\CallbackRequest; @@ -57,8 +56,7 @@ public function callbackAction(Request $request): Response $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $event = new SmsMessageCallbackEvent($callbackRequest); - $this->eventDispatcher->dispatch(SinchEvents::CALLBACK_RECEIVED, $event); + $this->eventDispatcher->dispatch(new SmsMessageCallbackEvent($callbackRequest)); } else { return new Response('Bad Request', Response::HTTP_BAD_REQUEST); } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 3c1ef6c..a557021 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -12,13 +12,14 @@ namespace Fresh\SinchBundle\DependencyInjection; +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; /** * Configuration. * - * @author Artem Henvald + * @author Artem Henvald */ class Configuration implements ConfigurationInterface { @@ -27,16 +28,19 @@ class Configuration implements ConfigurationInterface */ public function getConfigTreeBuilder(): TreeBuilder { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('fresh_sinch'); + $treeBuilder = new TreeBuilder('fresh_sinch'); - $rootNode + /** @var ArrayNodeDefinition $root */ + $root = $treeBuilder->getRootNode(); + + $root ->children() ->scalarNode('host')->defaultValue('https://messagingapi.sinch.com')->end() ->scalarNode('key')->end() ->scalarNode('secret')->end() ->scalarNode('from')->defaultNull()->end() - ->end(); + ->end() + ; return $treeBuilder; } diff --git a/Event/SmsEvent.php b/Event/AbstractBaseSmsEvent.php similarity index 95% rename from Event/SmsEvent.php rename to Event/AbstractBaseSmsEvent.php index 8a5cb5d..60052fb 100644 --- a/Event/SmsEvent.php +++ b/Event/AbstractBaseSmsEvent.php @@ -12,14 +12,12 @@ namespace Fresh\SinchBundle\Event; -use Symfony\Component\EventDispatcher\Event; - /** - * SmsEvent. + * AbstractBaseSmsEvent. * * @author Artem Henvald */ -class SmsEvent extends Event +abstract class AbstractBaseSmsEvent { /** @var string */ private $number; diff --git a/Event/PostSmsSendEvent.php b/Event/PostSmsSendEvent.php new file mode 100644 index 0000000..11e8660 --- /dev/null +++ b/Event/PostSmsSendEvent.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Fresh\SinchBundle\Event; + +/** + * PostSmsSendEvent. + * + * @author Artem Henvald + */ +class PostSmsSendEvent extends AbstractBaseSmsEvent +{ +} diff --git a/Event/PreSmsSendEvent.php b/Event/PreSmsSendEvent.php new file mode 100644 index 0000000..72a062a --- /dev/null +++ b/Event/PreSmsSendEvent.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Fresh\SinchBundle\Event; + +/** + * PreSmsSendEvent. + * + * @author Artem Henvald + */ +class PreSmsSendEvent extends AbstractBaseSmsEvent +{ +} diff --git a/Event/SinchEvents.php b/Event/SinchEvents.php deleted file mode 100644 index ed874ab..0000000 --- a/Event/SinchEvents.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Fresh\SinchBundle\Event; - -/** - * SinchEvents. - * - * @author Artem Henvald - */ -final class SinchEvents -{ - /** - * This event is triggered before the SMS is going to be sent. - * - * @see \Fresh\SinchBundle\Event\SmsEvent Listeners receive an instance of this class - */ - public const PRE_SMS_SEND = 'sinch.sms.pre_send'; - - /** - * This event is triggered after the SMS is successfully sent. - * - * @see \Fresh\SinchBundle\Event\SmsEvent Listeners receive an instance of this class - */ - public const POST_SMS_SEND = 'sinch.sms.post_send'; - - /** - * This event is triggered callback from Sinch is received. - * - * @see \Fresh\SinchBundle\Event\SmsMessageCallbackEvent Listeners receive an instance of this class - */ - public const CALLBACK_RECEIVED = 'sinch.callback.received'; -} diff --git a/Event/SmsMessageCallbackEvent.php b/Event/SmsMessageCallbackEvent.php index e37a120..3663b9d 100644 --- a/Event/SmsMessageCallbackEvent.php +++ b/Event/SmsMessageCallbackEvent.php @@ -14,14 +14,13 @@ use Fresh\SinchBundle\Model\CallbackRequest; use Fresh\SinchBundle\Model\Identity; -use Symfony\Component\EventDispatcher\Event; /** * SmsMessageCallbackEvent. * * @author Artem Henvald */ -class SmsMessageCallbackEvent extends Event +class SmsMessageCallbackEvent { /** @var CallbackRequest */ private $callbackRequest; diff --git a/README.md b/README.md index 544f64c..bc4b0a7 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,10 @@ [![StyleCI](https://styleci.io/repos/44092074/shield?style=flat-square)](https://styleci.io/repos/44092074) [![Gitter](https://img.shields.io/badge/gitter-join%20chat-brightgreen.svg?style=flat-square)](https://gitter.im/fre5h/SinchBundle) -[![SensioLabsInsight](https://insight.sensiolabs.com/projects/2303fcfb-2e4b-45b3-8b37-6d1e7598acf4/small.png)](https://insight.sensiolabs.com/projects/2303fcfb-2e4b-45b3-8b37-6d1e7598acf4) -[![knpbundles.com](http://knpbundles.com/fre5h/SinchBundle/badge-short)](http://knpbundles.com/fre5h/SinchBundle) - ## Requirements -* PHP 7.1 *and later* -* Symfony 4.0 *and later* +* PHP 7.3 *and later* +* Symfony 4.3 *and later* ## Installation diff --git a/Resources/config/services.yml b/Resources/config/services.yml index db7c5ae..54f2746 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -16,3 +16,6 @@ services: Fresh\SinchBundle\Service\Sinch: class: Fresh\SinchBundle\Service\Sinch + + Symfony\Contracts\HttpClient\HttpClientInterface: + factory: ['Symfony\Component\HttpClient\HttpClient', 'create'] diff --git a/Resources/images/sinch-logo.png b/Resources/images/sinch-logo.png index 1a597c7..85a3db2 100644 Binary files a/Resources/images/sinch-logo.png and b/Resources/images/sinch-logo.png differ diff --git a/Service/HTTPClientInterface.php b/Service/HTTPClientInterface.php deleted file mode 100644 index 39a3ab4..0000000 --- a/Service/HTTPClientInterface.php +++ /dev/null @@ -1,39 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Fresh\SinchBundle\Service; - -/** - * HTTPClientInterface. - * - * @author Artem Henvald - */ -interface HTTPClientInterface -{ - /** - * @param string $uri - * @param array $body - * @param array $headers - * - * @return mixed - */ - public function post(string $uri, array $body = [], array $headers = []); - - /** - * @param string $uri - * @param array $queryParameters - * @param array $headers - * - * @return mixed - */ - public function get(string $uri, array $queryParameters = [], array $headers = []); -} diff --git a/Service/HTTPClientService.php b/Service/HTTPClientService.php deleted file mode 100644 index 32efa60..0000000 --- a/Service/HTTPClientService.php +++ /dev/null @@ -1,37 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Fresh\SinchBundle\Service; - -/** - * HTTPClientInterface. - * - * @author Artem Henvald - */ -class HTTPClientService implements HTTPClientInterface -{ - /** - * {@inheritdoc} - */ - public function post(string $uri, array $body = [], array $headers = []) - { - // @todo - } - - /** - * {@inheritdoc} - */ - public function get(string $uri, array $queryParameters = [], array $headers = []) - { - // @todo - } -} diff --git a/Service/Sinch.php b/Service/Sinch.php index f565e23..69f8af6 100644 --- a/Service/Sinch.php +++ b/Service/Sinch.php @@ -12,17 +12,18 @@ namespace Fresh\SinchBundle\Service; -use Fresh\SinchBundle\Event\SinchEvents; -use Fresh\SinchBundle\Event\SmsEvent; +use Fresh\SinchBundle\Event\PostSmsSendEvent; +use Fresh\SinchBundle\Event\PreSmsSendEvent; use Fresh\SinchBundle\Exception\SinchException; use Fresh\SinchBundle\Helper\SinchSmsStatus; -use GuzzleHttp\Client; -use GuzzleHttp\Exception\ClientException; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; +use Symfony\Contracts\HttpClient\HttpClientInterface; /** - * Sinch Service. + * Sinch. * * @author Artem Henvald */ @@ -32,12 +33,12 @@ class Sinch public const URL_FOR_CHECKING_SMS_STATUS = '/v1/message/status/'; - /** @var Client */ - private $guzzleHTTPClient; - /** @var EventDispatcherInterface */ private $dispatcher; + /** @var HttpClientInterface */ + private $httpClient; + /** @var string */ private $host; @@ -52,22 +53,20 @@ class Sinch /** * @param EventDispatcherInterface $dispatcher + * @param HttpClientInterface $httpClient * @param string $host * @param string $key * @param string $secret * @param string|null $from */ - public function __construct(EventDispatcherInterface $dispatcher, $host, $key, $secret, $from = null) + public function __construct(EventDispatcherInterface $dispatcher, HttpClientInterface $httpClient, $host, $key, $secret, $from = null) { $this->dispatcher = $dispatcher; + $this->httpClient = $httpClient; $this->host = $host; $this->key = $key; $this->secret = $secret; $this->from = $from; - - $this->guzzleHTTPClient = new Client([ - 'base_uri' => \rtrim($this->host, '/'), - ]); } /** @@ -84,37 +83,38 @@ public function sendSMS(string $phoneNumber, string $messageText, ?string $from // @todo validate phone number $body = [ - 'auth' => [$this->key, $this->secret], - 'headers' => ['X-Timestamp' => (new \DateTime('now'))->format('c')], // ISO 8601 date format - 'json' => ['message' => $messageText], + 'message' => $messageText, ]; if (null !== $from) { - $body['json']['from'] = $from; + $body['from'] = $from; } elseif (null !== $this->from) { $from = $this->from; - $body['json']['from'] = $from; + $body['from'] = $from; } - try { - $smsEvent = new SmsEvent($phoneNumber, $messageText, $from); + $this->dispatcher->dispatch(new PreSmsSendEvent($phoneNumber, $messageText, $from)); - $this->dispatcher->dispatch(SinchEvents::PRE_SMS_SEND, $smsEvent); - $response = $this->guzzleHTTPClient->post(self::URL_FOR_SENDING_SMS.$phoneNumber, $body); - $this->dispatcher->dispatch(SinchEvents::POST_SMS_SEND, $smsEvent); - } catch (ClientException $e) { - throw SinchExceptionResolver::createAppropriateSinchException($e); - } + $response = $this->httpClient->request( + Request::METHOD_POST, + self::URL_FOR_SENDING_SMS.$phoneNumber, + [ + 'auth_basic' => [$this->key, $this->secret], + 'headers' => ['X-Timestamp' => (new \DateTime('now'))->format('c')], + 'json' => $body, + ] + ); + + $this->dispatcher->dispatch(new PostSmsSendEvent($phoneNumber, $messageText, $from)); $messageId = null; - if (Response::HTTP_OK === $response->getStatusCode() && $response->hasHeader('Content-Type') - && 'application/json; charset=utf-8' === $response->getHeaderLine('Content-Type') + $headers = $response->getHeaders(); + if (Response::HTTP_OK === $response->getStatusCode() && isset($headers['Content-Type']) + && 'application/json; charset=utf-8' === $headers['Content-Type'] ) { - $content = $response->getBody()->getContents(); - $content = \json_decode($content, true); - - if (isset($content['messageId']) && \array_key_exists('messageId', $content)) { + $content = \json_decode($response->getContent(), true); + if (\array_key_exists('messageId', $content)) { $messageId = $content['messageId']; } } @@ -132,7 +132,7 @@ public function getStatusOfSMS(int $messageId): string $response = $this->sendRequestToCheckStatusOfSMS($messageId); $result = ''; - if (isset($response['status']) && \array_key_exists('status', $response)) { + if (\array_key_exists('status', $response)) { $result = $response['status']; } @@ -149,7 +149,7 @@ public function smsIsSentSuccessfully(int $messageId): bool $response = $this->sendRequestToCheckStatusOfSMS($messageId); $result = false; - if (isset($response['status']) && SinchSmsStatus::SUCCESSFUL === $response['status']) { + if (\array_key_exists('status', $response) && SinchSmsStatus::SUCCESSFUL === $response['status']) { $result = true; } @@ -166,7 +166,7 @@ public function smsIsPending(int $messageId): bool $response = $this->sendRequestToCheckStatusOfSMS($messageId); $result = false; - if (isset($response['status']) && SinchSmsStatus::PENDING === $response['status']) { + if (\array_key_exists('status', $response) && SinchSmsStatus::PENDING === $response['status']) { $result = true; } @@ -183,7 +183,7 @@ public function smsIsFaulted(int $messageId): bool $response = $this->sendRequestToCheckStatusOfSMS($messageId); $result = false; - if (isset($response['status']) && SinchSmsStatus::FAULTED === $response['status']) { + if (\array_key_exists('status', $response) && SinchSmsStatus::FAULTED === $response['status']) { $result = true; } @@ -200,7 +200,7 @@ public function smsInUnknownStatus(int $messageId): bool $response = $this->sendRequestToCheckStatusOfSMS($messageId); $result = false; - if (isset($response['status']) && SinchSmsStatus::UNKNOWN === $response['status']) { + if (\array_key_exists('status', $response) && SinchSmsStatus::UNKNOWN === $response['status']) { $result = true; } @@ -214,24 +214,22 @@ public function smsInUnknownStatus(int $messageId): bool */ private function sendRequestToCheckStatusOfSMS(int $messageId): ?array { - $body = [ - 'auth' => [$this->key, $this->secret], - 'headers' => ['X-Timestamp' => (new \DateTime('now'))->format('c')], - ]; - - try { - $response = $this->guzzleHTTPClient->get(self::URL_FOR_CHECKING_SMS_STATUS.$messageId, $body); - } catch (ClientException $e) { - throw SinchExceptionResolver::createAppropriateSinchException($e); - } + $response = $this->httpClient->request( + Request::METHOD_GET, + self::URL_FOR_CHECKING_SMS_STATUS.$messageId, + [ + 'auth_basic' => [$this->key, $this->secret], + 'headers' => ['X-Timestamp' => (new \DateTime('now'))->format('c')], + ] + ); $result = null; - if (Response::HTTP_OK === $response->getStatusCode() && $response->hasHeader('Content-Type') - && 'application/json; charset=utf-8' === $response->getHeaderLine('Content-Type') + $headers = $response->getHeaders(); + if (Response::HTTP_OK === $response->getStatusCode() && isset($headers['Content-Type']) + && 'application/json; charset=utf-8' === $headers['Content-Type'] ) { - $content = $response->getBody()->getContents(); - $result = \json_decode($content, true); + $result = \json_decode($response->getContent(), true); } return $result; diff --git a/Service/SinchExceptionResolver.php b/Service/SinchExceptionResolver.php index 5403af7..97b80f0 100644 --- a/Service/SinchExceptionResolver.php +++ b/Service/SinchExceptionResolver.php @@ -23,7 +23,6 @@ use Fresh\SinchBundle\Exception\SinchException; use Fresh\SinchBundle\Exception\Unauthorized\SinchIllegalAuthorizationHeaderException; use Fresh\SinchBundle\Helper\SinchErrorCode; -use GuzzleHttp\Exception\ClientException; use Symfony\Component\HttpFoundation\Response; /** @@ -51,23 +50,18 @@ public static function createAppropriateSinchException(ClientException $e): \Exc switch ($responseStatusCode) { case Response::HTTP_BAD_REQUEST: $exception = self::getSinchExceptionForBadRequest($errorCode, $errorMessage); - break; case Response::HTTP_UNAUTHORIZED: $exception = self::getSinchExceptionForUnauthorized($errorCode, $errorMessage); - break; case Response::HTTP_PAYMENT_REQUIRED: $exception = self::getSinchExceptionForPaymentRequired($errorCode, $errorMessage); - break; case Response::HTTP_FORBIDDEN: $exception = self::getSinchExceptionForForbidden($errorCode, $errorMessage); - break; case Response::HTTP_INTERNAL_SERVER_ERROR: $exception = self::getSinchExceptionForInternalServerError($errorCode, $errorMessage); - break; } @@ -91,15 +85,12 @@ private static function getSinchExceptionForBadRequest(int $errorCode, string $e switch ($errorCode) { case SinchErrorCode::PARAMETER_VALIDATION: $exception = new SinchParameterValidationException($errorMessage); - break; case SinchErrorCode::MISSING_PARAMETER: $exception = new SinchMissingParameterException($errorMessage); - break; case SinchErrorCode::INVALID_REQUEST: $exception = new SinchInvalidRequestException($errorMessage); - break; } @@ -155,16 +146,13 @@ private static function getSinchExceptionForForbidden(int $errorCode, string $er switch ($errorCode) { case SinchErrorCode::FORBIDDEN_REQUEST: $exception = new SinchForbiddenRequestException($errorMessage); - break; case SinchErrorCode::INVALID_AUTHORIZATION_SCHEME_FOR_CALLING_THE_METHOD: $exception = new SinchInvalidAuthorizationSchemeException($errorMessage); - break; case SinchErrorCode::NO_VERIFIED_PHONE_NUMBER_ON_YOUR_SINCH_ACCOUNT: case SinchErrorCode::SANDBOX_SMS_ONLY_ALLOWED_TO_BE_SENT_TO_VERIFIED_NUMBERS: $exception = new SinchNoVerifiedPhoneNumberException($errorMessage); - break; } @@ -187,6 +175,4 @@ private static function getSinchExceptionForInternalServerError(int $errorCode, return $exception; } - - // endregion } diff --git a/Tests/Controller/SinchControllerTest.php b/Tests/Controller/SinchControllerTest.php index a6747fa..fc13ca4 100644 --- a/Tests/Controller/SinchControllerTest.php +++ b/Tests/Controller/SinchControllerTest.php @@ -10,6 +10,7 @@ namespace Fresh\SinchBundle\Tests\Controller; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher; use Symfony\Component\Form\Form; @@ -27,109 +28,119 @@ class SinchControllerTest extends WebTestCase { public const DEFAULT_SINCH_CALLBACK_URL = '/sinch/callback'; - /** @var \PHPUnit_Framework_MockObject_MockObject|TraceableEventDispatcher */ + /** @var TraceableEventDispatcher|MockObject */ private $eventDispatcher; - /** @var \PHPUnit_Framework_MockObject_MockObject|FormFactory */ + /** @var FormFactory|MockObject */ private $formFactory; - /** @var \PHPUnit_Framework_MockObject_MockObject|Form */ + /** @var Form|MockObject */ private $form; /** @var SinchController */ private $controller; - protected function setUp() + protected function setUp(): void { - $this->eventDispatcher = $this->getMockBuilder(TraceableEventDispatcher::class) - ->disableOriginalConstructor() - ->setMethods(['dispatch']) - ->getMock(); - - $this->formFactory = $this->getMockBuilder(FormFactory::class) - ->disableOriginalConstructor() - ->setMethods(['create']) - ->getMock(); - - $this->form = $this->getMockBuilder(Form::class) - ->disableOriginalConstructor() - ->setMethods(['isValid', 'isSubmitted', 'handleRequest']) - ->getMock(); - - $this->formFactory->expects($this->once()) - ->method('create') - ->willReturn($this->form); + $this->eventDispatcher = $this->createMock(TraceableEventDispatcher::class); + $this->form = $this->createMock(Form::class); + $this->formFactory = $this->createMock(FormFactory::class); + $this->formFactory + ->expects(self::once()) + ->method('create') + ->willReturn($this->form) + ; $this->controller = new SinchController($this->formFactory, $this->eventDispatcher); } - protected function tearDown() + protected function tearDown(): void { - unset($this->formFactory); - unset($this->eventDispatcher); - unset($this->controller); - unset($this->form); + unset( + $this->eventDispatcher, + $this->form, + $this->formFactory, + $this->controller, + ); } - public function testValidCallback() + public function testValidCallback(): void { - $this->form->expects($this->once()) - ->method('isSubmitted') - ->willReturn(true); - - $this->form->expects($this->once()) - ->method('isValid') - ->willReturn(true); + $this->form + ->expects(self::once()) + ->method('isSubmitted') + ->willReturn(true) + ; + + $this->form + ->expects(self::once()) + ->method('isValid') + ->willReturn(true) + ; $request = Request::create(self::DEFAULT_SINCH_CALLBACK_URL, 'POST'); $response = $this->controller->callbackAction($request); - $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); + self::assertEquals(Response::HTTP_OK, $response->getStatusCode()); } - public function testNotSubmittedData() + public function testNotSubmittedData(): void { - $this->form->expects($this->once()) - ->method('isSubmitted') - ->willReturn(false); - $this->form->expects($this->never()) - ->method('isValid'); + $this->form + ->expects(self::once()) + ->method('isSubmitted') + ->willReturn(false) + ; + $this->form + ->expects($this->never()) + ->method('isValid') + ; $request = Request::create(self::DEFAULT_SINCH_CALLBACK_URL, 'POST'); $response = $this->controller->callbackAction($request); - $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); + self::assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); } - public function testNotValidData() + public function testNotValidData(): void { - $this->form->expects($this->once()) - ->method('isSubmitted') - ->willReturn(true); - - $this->form->expects($this->once()) - ->method('isValid') - ->willReturn(false); + $this->form + ->expects(self::once()) + ->method('isSubmitted') + ->willReturn(true) + ; + + $this->form + ->expects(self::once()) + ->method('isValid') + ->willReturn(false) + ; $request = Request::create(self::DEFAULT_SINCH_CALLBACK_URL, 'POST'); $response = $this->controller->callbackAction($request); - $this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); + self::assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode()); } - public function testInternalError() + public function testInternalError(): void { - $this->eventDispatcher->expects($this->once()) - ->method('dispatch') - ->willThrowException(new \Exception()); - - $this->form->expects($this->once()) - ->method('isSubmitted') - ->willReturn(true); - - $this->form->expects($this->once()) - ->method('isValid') - ->willReturn(true); + $this->eventDispatcher + ->expects(self::once()) + ->method('dispatch') + ->willThrowException(new \Exception()) + ; + + $this->form + ->expects(self::once()) + ->method('isSubmitted') + ->willReturn(true) + ; + + $this->form + ->expects(self::once()) + ->method('isValid') + ->willReturn(true) + ; $request = Request::create(self::DEFAULT_SINCH_CALLBACK_URL, 'POST'); $response = $this->controller->callbackAction($request); - $this->assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode()); + self::assertEquals(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode()); } } diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index efe5b41..060016c 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -23,9 +23,9 @@ class ConfigurationTest extends TestCase { use ConfigurationTestCaseTrait; - public function testInvalidConfiguration() + public function testInvalidConfiguration(): void { - $this->assertConfigurationIsInvalid( + self::assertConfigurationIsInvalid( [ [ 'invalid_parameter' => 123, @@ -35,9 +35,9 @@ public function testInvalidConfiguration() ); } - public function testValidDefaultConfiguration() + public function testValidDefaultConfiguration(): void { - $this->assertProcessedConfigurationEquals( + self::assertProcessedConfigurationEquals( [], [ 'host' => 'https://messagingapi.sinch.com', @@ -46,9 +46,9 @@ public function testValidDefaultConfiguration() ); } - public function testValidConfigurationWithHost() + public function testValidConfigurationWithHost(): void { - $this->assertProcessedConfigurationEquals( + self::assertProcessedConfigurationEquals( [ [ 'host' => 'https://test.com', @@ -65,9 +65,9 @@ public function testValidConfigurationWithHost() ); } - public function testValidConfigurationWithoutFrom() + public function testValidConfigurationWithoutFrom(): void { - $this->assertProcessedConfigurationEquals( + self::assertProcessedConfigurationEquals( [ [ 'key' => '1234567890', @@ -83,9 +83,9 @@ public function testValidConfigurationWithoutFrom() ); } - public function testValidConfigurationWithFrom() + public function testValidConfigurationWithFrom(): void { - $this->assertProcessedConfigurationEquals( + self::assertProcessedConfigurationEquals( [ [ 'key' => '1234567890', @@ -102,7 +102,7 @@ public function testValidConfigurationWithFrom() ); } - protected function getConfiguration() + protected function getConfiguration(): Configuration { return new Configuration(); } diff --git a/Tests/DependencyInjection/FreshSinchExtensionTest.php b/Tests/DependencyInjection/FreshSinchExtensionTest.php index ccc500a..47d612a 100644 --- a/Tests/DependencyInjection/FreshSinchExtensionTest.php +++ b/Tests/DependencyInjection/FreshSinchExtensionTest.php @@ -31,20 +31,20 @@ class FreshSinchExtensionTest extends TestCase /** @var ContainerBuilder */ private $container; - protected function setUp() + protected function setUp(): void { $this->extension = new FreshSinchExtension(); $this->container = new ContainerBuilder(); $this->container->registerExtension($this->extension); } - public function testLoadExtension() + public function testLoadExtension(): void { $yaml = <<<'CONFIG' -fresh_sinch: - key: some_dummy_key - secret: some_dummy_secret -CONFIG; + fresh_sinch: + key: some_dummy_key + secret: some_dummy_secret + CONFIG; $parser = new Parser(); $config = $parser->parse($yaml); @@ -54,19 +54,19 @@ public function testLoadExtension() $this->container->set('form.factory', new \stdClass()); $this->container->compile(); - $this->assertArrayHasKey(Sinch::class, $this->container->getRemovedIds()); - $this->assertArrayHasKey(SinchController::class, $this->container->getRemovedIds()); + self::assertArrayHasKey(Sinch::class, $this->container->getRemovedIds()); + self::assertArrayHasKey(SinchController::class, $this->container->getRemovedIds()); - $this->assertArrayNotHasKey(Sinch::class, $this->container->getDefinitions()); - $this->assertArrayNotHasKey(SinchController::class, $this->container->getDefinitions()); + self::assertArrayNotHasKey(Sinch::class, $this->container->getDefinitions()); + self::assertArrayNotHasKey(SinchController::class, $this->container->getDefinitions()); $this->expectException(ServiceNotFoundException::class); $this->container->get(Sinch::class); $this->container->get(SinchController::class); - $this->assertTrue($this->container->hasParameter('sinch.host')); - $this->assertTrue($this->container->hasParameter('sinch.key')); - $this->assertTrue($this->container->hasParameter('sinch.secret')); - $this->assertTrue($this->container->hasParameter('sinch.from')); + self::assertTrue($this->container->hasParameter('sinch.host')); + self::assertTrue($this->container->hasParameter('sinch.key')); + self::assertTrue($this->container->hasParameter('sinch.secret')); + self::assertTrue($this->container->hasParameter('sinch.from')); } } diff --git a/Tests/Event/PostSmsSendEventTest.php b/Tests/Event/PostSmsSendEventTest.php new file mode 100644 index 0000000..3e3dd63 --- /dev/null +++ b/Tests/Event/PostSmsSendEventTest.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fresh\SinchBundle\Tests\Event; + +use Fresh\SinchBundle\Event\AbstractBaseSmsEvent; +use Fresh\SinchBundle\Event\PostSmsSendEvent; +use PHPUnit\Framework\TestCase; + +/** + * PostSmsSendEventTest. + * + * @author Artem Henvald + */ +class PostSmsSendEventTest extends TestCase +{ + public function testConstructorWithoutFrom(): void + { + $number = '+46700000000'; + $message = 'Hello world'; + + $postSmsSendEvent = new PostSmsSendEvent($number, $message); + + self::assertEquals($number, $postSmsSendEvent->getNumber()); + self::assertEquals($message, $postSmsSendEvent->getMessage()); + self::assertInstanceOf(AbstractBaseSmsEvent::class, $postSmsSendEvent); + } + + public function testConstructorWithFrom(): void + { + $number = '+46700000000'; + $message = 'Hello world'; + $from = 'Santa Claus'; + + $postSmsSendEvent = new PostSmsSendEvent($number, $message, $from); + + self::assertEquals($number, $postSmsSendEvent->getNumber()); + self::assertEquals($message, $postSmsSendEvent->getMessage()); + self::assertEquals($from, $postSmsSendEvent->getFrom()); + } + + public function testSetGetNumber(): void + { + $postSmsSendEvent = new PostSmsSendEvent('', ''); + + $number = '+46700000000'; + $postSmsSendEvent->setNumber($number); + self::assertEquals($number, $postSmsSendEvent->getNumber()); + } + + public function testSetGetMessage(): void + { + $postSmsSendEvent = new PostSmsSendEvent('', ''); + + $message = 'Hello world'; + $postSmsSendEvent->setMessage($message); + self::assertEquals($message, $postSmsSendEvent->getMessage()); + } + + public function testSetGetFrom(): void + { + $postSmsSendEvent = new PostSmsSendEvent('', ''); + + $from = 'Santa Claus'; + $postSmsSendEvent->setFrom($from); + self::assertEquals($from, $postSmsSendEvent->getFrom()); + } +} diff --git a/Tests/Event/PreSmsSendEventTest.php b/Tests/Event/PreSmsSendEventTest.php new file mode 100644 index 0000000..099f90c --- /dev/null +++ b/Tests/Event/PreSmsSendEventTest.php @@ -0,0 +1,73 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fresh\SinchBundle\Tests\Event; + +use Fresh\SinchBundle\Event\PreSmsSendEvent; +use PHPUnit\Framework\TestCase; + +/** + * PreSmsSendEventTest. + * + * @author Artem Henvald + */ +class PreSmsSendEventTest extends TestCase +{ + public function testConstructorWithoutFrom(): void + { + $number = '+46700000000'; + $message = 'Hello world'; + + $preSmsSendEvent = new PreSmsSendEvent($number, $message); + + self::assertEquals($number, $preSmsSendEvent->getNumber()); + self::assertEquals($message, $preSmsSendEvent->getMessage()); + } + + public function testConstructorWithFrom(): void + { + $number = '+46700000000'; + $message = 'Hello world'; + $from = 'Santa Claus'; + + $preSmsSendEvent = new PreSmsSendEvent($number, $message, $from); + + self::assertEquals($number, $preSmsSendEvent->getNumber()); + self::assertEquals($message, $preSmsSendEvent->getMessage()); + self::assertEquals($from, $preSmsSendEvent->getFrom()); + } + + public function testSetGetNumber(): void + { + $preSmsSendEvent = new PreSmsSendEvent('', ''); + + $number = '+46700000000'; + $preSmsSendEvent->setNumber($number); + self::assertEquals($number, $preSmsSendEvent->getNumber()); + } + + public function testSetGetMessage(): void + { + $preSmsSendEvent = new PreSmsSendEvent('', ''); + + $message = 'Hello world'; + $preSmsSendEvent->setMessage($message); + self::assertEquals($message, $preSmsSendEvent->getMessage()); + } + + public function testSetGetFrom(): void + { + $preSmsSendEvent = new PreSmsSendEvent('', ''); + + $from = 'Santa Claus'; + $preSmsSendEvent->setFrom($from); + self::assertEquals($from, $preSmsSendEvent->getFrom()); + } +} diff --git a/Tests/Event/SmsEventTest.php b/Tests/Event/SmsEventTest.php deleted file mode 100644 index 2c109b4..0000000 --- a/Tests/Event/SmsEventTest.php +++ /dev/null @@ -1,73 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Fresh\SinchBundle\Tests\Event; - -use Fresh\SinchBundle\Event\SmsEvent; -use PHPUnit\Framework\TestCase; - -/** - * SmsEventTest. - * - * @author Artem Henvald - */ -class SmsEventTest extends TestCase -{ - public function testConstructorWithoutFrom() - { - $number = '+46700000000'; - $message = 'Hello world'; - - $smsEvent = new SmsEvent($number, $message); - - $this->assertEquals($number, $smsEvent->getNumber()); - $this->assertEquals($message, $smsEvent->getMessage()); - } - - public function testConstructorWithFrom() - { - $number = '+46700000000'; - $message = 'Hello world'; - $from = 'Santa Claus'; - - $smsEvent = new SmsEvent($number, $message, $from); - - $this->assertEquals($number, $smsEvent->getNumber()); - $this->assertEquals($message, $smsEvent->getMessage()); - $this->assertEquals($from, $smsEvent->getFrom()); - } - - public function testSetGetNumber() - { - $smsEvent = new SmsEvent('', ''); - - $number = '+46700000000'; - $smsEvent->setNumber($number); - $this->assertEquals($number, $smsEvent->getNumber()); - } - - public function testSetGetMessage() - { - $smsEvent = new SmsEvent('', ''); - - $message = 'Hello world'; - $smsEvent->setMessage($message); - $this->assertEquals($message, $smsEvent->getMessage()); - } - - public function testSetGetFrom() - { - $smsEvent = new SmsEvent('', ''); - - $from = 'Santa Claus'; - $smsEvent->setFrom($from); - $this->assertEquals($from, $smsEvent->getFrom()); - } -} diff --git a/Tests/Event/SmsMessageCallbackEventTest.php b/Tests/Event/SmsMessageCallbackEventTest.php index 5e8b430..6923b10 100644 --- a/Tests/Event/SmsMessageCallbackEventTest.php +++ b/Tests/Event/SmsMessageCallbackEventTest.php @@ -22,7 +22,7 @@ */ class SmsMessageCallbackEventTest extends TestCase { - public function testConstructor() + public function testConstructor(): void { $event = 'incomingSms'; $from = new Identity(); @@ -38,13 +38,14 @@ public function testConstructor() ->setMessage($message) ->setTimestamp($timestamp) ->setVersion($version); + $callbackEvent = new SmsMessageCallbackEvent($callbackRequest); - $this->assertEquals($event, $callbackEvent->getEvent()); - $this->assertEquals($from, $callbackEvent->getFrom()); - $this->assertEquals($to, $callbackEvent->getTo()); - $this->assertEquals($message, $callbackEvent->getMessage()); - $this->assertEquals($timestamp, $callbackEvent->getTimestamp()); - $this->assertEquals($version, $callbackEvent->getVersion()); + self::assertEquals($event, $callbackEvent->getEvent()); + self::assertEquals($from, $callbackEvent->getFrom()); + self::assertEquals($to, $callbackEvent->getTo()); + self::assertEquals($message, $callbackEvent->getMessage()); + self::assertEquals($timestamp, $callbackEvent->getTimestamp()); + self::assertEquals($version, $callbackEvent->getVersion()); } } diff --git a/Tests/Form/Type/TypeTestCase.php b/Tests/Form/Type/AbstractTypeTestCase.php similarity index 86% rename from Tests/Form/Type/TypeTestCase.php rename to Tests/Form/Type/AbstractTypeTestCase.php index 71c54d7..bf5cdc6 100644 --- a/Tests/Form/Type/TypeTestCase.php +++ b/Tests/Form/Type/AbstractTypeTestCase.php @@ -16,11 +16,11 @@ use Symfony\Component\Form\Test\FormIntegrationTestCase; /** - * TypeTestCase + * AbstractTypeTestCase. * * @author Artem Henvald */ -abstract class TypeTestCase extends FormIntegrationTestCase +abstract class AbstractTypeTestCase extends FormIntegrationTestCase { /** @var FormBuilder */ protected $builder; @@ -28,7 +28,7 @@ abstract class TypeTestCase extends FormIntegrationTestCase /** @var EventDispatcher */ protected $dispatcher; - protected function setUp() + protected function setUp(): void { parent::setUp(); @@ -36,7 +36,7 @@ protected function setUp() $this->builder = new FormBuilder(null, null, $this->dispatcher, $this->factory); } - public static function assertDateTimeEquals(\DateTime $expected, \DateTime $actual) + public static function assertDateTimeEquals(\DateTime $expected, \DateTime $actual): void { self::assertEquals($expected->format('c'), $actual->format('c')); } diff --git a/Tests/Form/Type/CallbackRequestTypeTest.php b/Tests/Form/Type/CallbackRequestTypeTest.php index a56e134..82b75b9 100644 --- a/Tests/Form/Type/CallbackRequestTypeTest.php +++ b/Tests/Form/Type/CallbackRequestTypeTest.php @@ -21,27 +21,27 @@ * * @author Artem Henvald */ -class CallbackRequestTypeTest extends TypeTestCase +class CallbackRequestTypeTest extends AbstractTypeTestCase { - public function testGetBlockPrefix() + public function testGetBlockPrefix(): void { - $this->assertEmpty((new CallbackRequestType)->getBlockPrefix()); + self::assertEmpty((new CallbackRequestType)->getBlockPrefix()); } - public function testFormBuilder() + public function testFormBuilder(): void { $form = $this->factory->createBuilder(CallbackRequestType::class)->getForm(); - $this->assertEquals(6, $form->count()); - $this->assertInstanceOf(FormInterface::class, $form->get('event')); - $this->assertInstanceOf(FormInterface::class, $form->get('from')); - $this->assertInstanceOf(FormInterface::class, $form->get('to')); - $this->assertInstanceOf(FormInterface::class, $form->get('message')); - $this->assertInstanceOf(FormInterface::class, $form->get('timestamp')); - $this->assertInstanceOf(FormInterface::class, $form->get('version')); + self::assertEquals(6, $form->count()); + self::assertInstanceOf(FormInterface::class, $form->get('event')); + self::assertInstanceOf(FormInterface::class, $form->get('from')); + self::assertInstanceOf(FormInterface::class, $form->get('to')); + self::assertInstanceOf(FormInterface::class, $form->get('message')); + self::assertInstanceOf(FormInterface::class, $form->get('timestamp')); + self::assertInstanceOf(FormInterface::class, $form->get('version')); } - public function testGetDefaultOptions() + public function testGetDefaultOptions(): void { $type = new CallbackRequestType(); @@ -51,11 +51,11 @@ public function testGetDefaultOptions() $options = $optionResolver->resolve(); - $this->assertFalse($options['csrf_protection']); - $this->assertEquals(CallbackRequest::class, $options['data_class']); + self::assertFalse($options['csrf_protection']); + self::assertEquals(CallbackRequest::class, $options['data_class']); } - public function testSubmitValidData() + public function testSubmitValidData(): void { $data = [ 'event' => 'incomingSms', @@ -85,23 +85,23 @@ public function testSubmitValidData() // Submit the data to the form directly $form->submit($data); - $this->assertTrue($form->isSynchronized()); + self::assertTrue($form->isSynchronized()); - /** @var \Fresh\SinchBundle\Model\CallbackRequest $formData */ + /** @var CallbackRequest $formData */ $formData = $form->getData(); - $this->assertEquals($identity, $formData); - $this->assertInternalType('string', $formData->getEvent()); - $this->assertInternalType('object', $formData->getFrom()); - $this->assertInternalType('object', $formData->getTo()); - $this->assertInternalType('string', $formData->getMessage()); - $this->assertInternalType('object', $formData->getTimestamp()); - $this->assertInternalType('integer', $formData->getVersion()); + self::assertEquals($identity, $formData); + self::assertIsString($formData->getEvent()); + self::assertIsObject($formData->getFrom()); + self::assertIsObject($formData->getTo()); + self::assertIsString($formData->getMessage()); + self::assertIsObject($formData->getTimestamp()); + self::assertIsInt($formData->getVersion()); $view = $form->createView(); $children = $view->children; foreach (\array_keys($data) as $key) { - $this->assertArrayHasKey($key, $children); + self::assertArrayHasKey($key, $children); } } } diff --git a/Tests/Form/Type/IdentityTypeTest.php b/Tests/Form/Type/IdentityTypeTest.php index 9bbb480..20018fc 100644 --- a/Tests/Form/Type/IdentityTypeTest.php +++ b/Tests/Form/Type/IdentityTypeTest.php @@ -20,23 +20,23 @@ * * @author Artem Henvald */ -class IdentityTypeTest extends TypeTestCase +class IdentityTypeTest extends AbstractTypeTestCase { - public function testGetBlockPrefix() + public function testGetBlockPrefix(): void { - $this->assertEmpty((new IdentityType)->getBlockPrefix()); + self::assertEmpty((new IdentityType())->getBlockPrefix()); } - public function testFormBuilder() + public function testFormBuilder(): void { $form = $this->factory->createBuilder(IdentityType::class)->getForm(); - $this->assertEquals(2, $form->count()); - $this->assertInstanceOf(FormInterface::class, $form->get('type')); - $this->assertInstanceOf(FormInterface::class, $form->get('endpoint')); + self::assertEquals(2, $form->count()); + self::assertInstanceOf(FormInterface::class, $form->get('type')); + self::assertInstanceOf(FormInterface::class, $form->get('endpoint')); } - public function testGetDefaultOptions() + public function testGetDefaultOptions(): void { $type = new IdentityType(); @@ -46,11 +46,11 @@ public function testGetDefaultOptions() $options = $optionResolver->resolve(); - $this->assertFalse($options['csrf_protection']); - $this->assertEquals(Identity::class, $options['data_class']); + self::assertFalse($options['csrf_protection']); + self::assertEquals(Identity::class, $options['data_class']); } - public function testSubmitValidData() + public function testSubmitValidData(): void { $data = [ 'type' => 'number', @@ -59,25 +59,27 @@ public function testSubmitValidData() $form = $this->factory->create(IdentityType::class); - $identity = (new Identity())->setType('number') - ->setEndpoint('+46700000000'); + $identity = (new Identity()) + ->setType('number') + ->setEndpoint('+46700000000') + ; // Submit the data to the form directly $form->submit($data); - $this->assertTrue($form->isSynchronized()); + self::assertTrue($form->isSynchronized()); - /** @var \Fresh\SinchBundle\Model\Identity $formData */ + /** @var Identity $formData */ $formData = $form->getData(); - $this->assertEquals($identity, $formData); - $this->assertInternalType('string', $formData->getType()); - $this->assertInternalType('string', $formData->getEndpoint()); + self::assertEquals($identity, $formData); + self::assertIsString($formData->getType()); + self::assertIsString($formData->getEndpoint()); $view = $form->createView(); $children = $view->children; foreach (\array_keys($data) as $key) { - $this->assertArrayHasKey($key, $children); + self::assertArrayHasKey($key, $children); } } } diff --git a/Tests/Helper/SinchSupportedCountriesTest.php b/Tests/Helper/SinchSupportedCountriesTest.php index e60d950..c25b2ef 100644 --- a/Tests/Helper/SinchSupportedCountriesTest.php +++ b/Tests/Helper/SinchSupportedCountriesTest.php @@ -20,13 +20,13 @@ */ class SinchSupportedCountriesTest extends TestCase { - public function testSupportedCountry() + public function testSupportedCountry(): void { - $this->assertTrue(SinchSupportedCountries::isCountrySupported('UA')); + self::assertTrue(SinchSupportedCountries::isCountrySupported('UA')); } - public function testUnsupportedCountry() + public function testUnsupportedCountry(): void { - $this->assertFalse(SinchSupportedCountries::isCountrySupported('YO')); + self::assertFalse(SinchSupportedCountries::isCountrySupported('YO')); } } diff --git a/Tests/Model/CallbackRequestTest.php b/Tests/Model/CallbackRequestTest.php index 48346aa..4dd53d6 100644 --- a/Tests/Model/CallbackRequestTest.php +++ b/Tests/Model/CallbackRequestTest.php @@ -21,57 +21,57 @@ */ class CallbackRequestTest extends TestCase { - public function testConstructor() + public function testConstructor(): void { $callbackRequest = new CallbackRequest(); - $this->assertNull($callbackRequest->getEvent()); - $this->assertNull($callbackRequest->getFrom()); - $this->assertNull($callbackRequest->getTo()); - $this->assertNull($callbackRequest->getMessage()); - $this->assertNull($callbackRequest->getTimestamp()); - $this->assertNull($callbackRequest->getVersion()); + self::assertNull($callbackRequest->getEvent()); + self::assertNull($callbackRequest->getFrom()); + self::assertNull($callbackRequest->getTo()); + self::assertNull($callbackRequest->getMessage()); + self::assertNull($callbackRequest->getTimestamp()); + self::assertNull($callbackRequest->getVersion()); } - public function testSetGetEvent() + public function testSetGetEvent(): void { $event = 'incomingSms'; $callbackRequest = (new CallbackRequest())->setEvent($event); - $this->assertEquals($event, $callbackRequest->getEvent()); + self::assertEquals($event, $callbackRequest->getEvent()); } - public function testSetGetTo() + public function testSetGetTo(): void { $to = new Identity(); $callbackRequest = (new CallbackRequest())->setTo($to); - $this->assertEquals($to, $callbackRequest->getTo()); + self::assertEquals($to, $callbackRequest->getTo()); } - public function testSetGetFrom() + public function testSetGetFrom(): void { $from = new Identity(); $callbackRequest = (new CallbackRequest())->setFrom($from); - $this->assertEquals($from, $callbackRequest->getFrom()); + self::assertEquals($from, $callbackRequest->getFrom()); } - public function testSetGetMessage() + public function testSetGetMessage(): void { $message = 'Hello world'; $callbackRequest = (new CallbackRequest())->setMessage($message); - $this->assertEquals($message, $callbackRequest->getMessage()); + self::assertEquals($message, $callbackRequest->getMessage()); } - public function testSetGetTimestamp() + public function testSetGetTimestamp(): void { $timestamp = new \DateTime('now'); $callbackRequest = (new CallbackRequest())->setTimestamp($timestamp); - $this->assertEquals($timestamp, $callbackRequest->getTimestamp()); + self::assertEquals($timestamp, $callbackRequest->getTimestamp()); } - public function testSetGetVersion() + public function testSetGetVersion(): void { $version = 1; $callbackRequest = (new CallbackRequest())->setVersion($version); - $this->assertEquals($version, $callbackRequest->getVersion()); + self::assertEquals($version, $callbackRequest->getVersion()); } } diff --git a/Tests/Model/IdentityTest.php b/Tests/Model/IdentityTest.php index 1d5bbbb..3620d86 100644 --- a/Tests/Model/IdentityTest.php +++ b/Tests/Model/IdentityTest.php @@ -20,25 +20,25 @@ */ class IdentityTest extends TestCase { - public function testConstructor() + public function testConstructor(): void { $identity = new Identity(); - $this->assertNull($identity->getType()); - $this->assertNull($identity->getEndpoint()); + self::assertNull($identity->getType()); + self::assertNull($identity->getEndpoint()); } - public function testSetGetType() + public function testSetGetType(): void { $type = 'number'; $identity = (new Identity())->setType($type); - $this->assertEquals($type, $identity->getType()); + self::assertEquals($type, $identity->getType()); } - public function testSetGetEndpoint() + public function testSetGetEndpoint(): void { $endpoint = '+46700000000'; $identity = (new Identity())->setEndpoint($endpoint); - $this->assertEquals($endpoint, $identity->getEndpoint()); + self::assertEquals($endpoint, $identity->getEndpoint()); } } diff --git a/Tests/Service/SinchExceptionResolverTest.php b/Tests/Service/SinchExceptionResolverTest.php index 07abe15..f8bbb1e 100644 --- a/Tests/Service/SinchExceptionResolverTest.php +++ b/Tests/Service/SinchExceptionResolverTest.php @@ -21,7 +21,6 @@ use Fresh\SinchBundle\Exception\Unauthorized\SinchIllegalAuthorizationHeaderException; use Fresh\SinchBundle\Helper\SinchErrorCode; use Fresh\SinchBundle\Service\SinchExceptionResolver; -use GuzzleHttp\Exception\ClientException; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -35,93 +34,102 @@ */ class SinchExceptionResolverTest extends TestCase { - public function testSinchParameterValidationException() + public function testSinchParameterValidationException(): void { $e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::PARAMETER_VALIDATION); - $this->assertInstanceOf(SinchParameterValidationException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchParameterValidationException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchInvalidRequestException() + public function testSinchInvalidRequestException(): void { $e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::INVALID_REQUEST); - $this->assertInstanceOf(SinchInvalidRequestException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchInvalidRequestException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchMissingParameterException() + public function testSinchMissingParameterException(): void { $e = $this->getClientException(Response::HTTP_BAD_REQUEST, SinchErrorCode::MISSING_PARAMETER); - $this->assertInstanceOf(SinchMissingParameterException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchMissingParameterException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchIllegalAuthorizationHeaderException() + public function testSinchIllegalAuthorizationHeaderException(): void { $e = $this->getClientException(Response::HTTP_UNAUTHORIZED, SinchErrorCode::ILLEGAL_AUTHORIZATION_HEADER); - $this->assertInstanceOf(SinchIllegalAuthorizationHeaderException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchIllegalAuthorizationHeaderException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchPaymentRequiredException() + public function testSinchPaymentRequiredException(): void { $e = $this->getClientException(Response::HTTP_PAYMENT_REQUIRED, SinchErrorCode::THERE_IS_NOT_ENOUGH_FUNDS_TO_SEND_THE_MESSAGE); - $this->assertInstanceOf(SinchPaymentRequiredException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchPaymentRequiredException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchForbiddenRequestException() + public function testSinchForbiddenRequestException(): void { $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::FORBIDDEN_REQUEST); - $this->assertInstanceOf(SinchForbiddenRequestException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchForbiddenRequestException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchInvalidAuthorizationSchemeException() + public function testSinchInvalidAuthorizationSchemeException(): void { $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::INVALID_AUTHORIZATION_SCHEME_FOR_CALLING_THE_METHOD); - $this->assertInstanceOf(SinchInvalidAuthorizationSchemeException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchInvalidAuthorizationSchemeException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchNoVerifiedPhoneNumberException() + public function testSinchNoVerifiedPhoneNumberException(): void { $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::NO_VERIFIED_PHONE_NUMBER_ON_YOUR_SINCH_ACCOUNT); - $this->assertInstanceOf(SinchNoVerifiedPhoneNumberException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchNoVerifiedPhoneNumberException::class, SinchExceptionResolver::createAppropriateSinchException($e)); $e = $this->getClientException(Response::HTTP_FORBIDDEN, SinchErrorCode::SANDBOX_SMS_ONLY_ALLOWED_TO_BE_SENT_TO_VERIFIED_NUMBERS); - $this->assertInstanceOf(SinchNoVerifiedPhoneNumberException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchNoVerifiedPhoneNumberException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testSinchInternalErrorException() + public function testSinchInternalErrorException(): void { $e = $this->getClientException(Response::HTTP_INTERNAL_SERVER_ERROR, SinchErrorCode::INTERNAL_ERROR); - $this->assertInstanceOf(SinchInternalErrorException::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(SinchInternalErrorException::class, SinchExceptionResolver::createAppropriateSinchException($e)); } - public function testStandardException() + public function testStandardException(): void { $e = $this->getClientException(Response::HTTP_GATEWAY_TIMEOUT, 0); - $this->assertInstanceOf(\Exception::class, SinchExceptionResolver::createAppropriateSinchException($e)); + self::assertInstanceOf(\Exception::class, SinchExceptionResolver::createAppropriateSinchException($e)); } /** - * Get client exception - * * @param int $statusCode Status code * @param int $errorCode Error code * * @return ClientException */ - private function getClientException($statusCode, $errorCode) + private function getClientException($statusCode, $errorCode): ClientException { - $request = $this->getMockBuilder(RequestInterface::class)->disableOriginalConstructor()->getMock(); - $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock(); - $body = $this->getMockBuilder(StreamInterface::class)->disableOriginalConstructor()->getMock(); - - $response->expects($this->once())->method('getStatusCode')->willReturn($statusCode); - $response->expects($this->once())->method('getBody')->will($this->returnValue($body)); - - $body->expects($this->once())->method('getContents')->will($this->returnValue(<<createMock(RequestInterface::class); + $response = $this->createMock(ResponseInterface::class); + $body = $this->createMock(StreamInterface::class); + + $response + ->expects(self::once()) + ->method('getStatusCode') + ->willReturn($statusCode) + ; + $response + ->expects(self::once()) + ->method('getBody') + ->willReturn($body) + ; + + $body + ->expects(self::once()) + ->method('getContents') + ->willReturn(<< - - ./Tests