linkmobility-php-client/Tests/ValueObject/RecipientTest.php

246 lines
7.9 KiB
PHP
Raw Normal View History

2022-07-11 15:06:18 +02:00
<?php
/**
2022-07-13 10:50:45 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
2022-07-11 15:06:18 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-07-13 10:50:45 +02:00
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
2022-07-11 15:06:18 +02:00
*/
2022-07-13 10:41:23 +02:00
declare(strict_types=1);
2022-07-11 15:06:18 +02:00
namespace D3\LinkmobilityClient\Tests\ValueObject;
use Assert\InvalidArgumentException;
use D3\LinkmobilityClient\Exceptions\RecipientException;
2022-07-11 15:06:18 +02:00
use D3\LinkmobilityClient\Tests\ApiTestCase;
use D3\LinkmobilityClient\ValueObject\Recipient;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
2022-07-15 23:54:52 +02:00
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberType;
2022-07-11 15:06:18 +02:00
use libphonenumber\PhoneNumberUtil;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
class RecipientTest extends ApiTestCase
{
/** @var Recipient */
public $recipient;
2022-07-15 23:54:52 +02:00
private $phoneNumberFixture;
2022-07-11 15:06:18 +02:00
private $phoneCountryFixture = 'DE';
/**
* @return void
* @throws NumberParseException
* @throws RecipientException
2022-07-11 15:06:18 +02:00
*/
2022-07-13 10:41:23 +02:00
public function setUp(): void
2022-07-11 15:06:18 +02:00
{
parent::setUp();
2022-07-15 23:54:52 +02:00
$phoneUtil = PhoneNumberUtil::getInstance();
$example = $phoneUtil->getExampleNumberForType($this->phoneCountryFixture, PhoneNumberType::MOBILE);
2022-07-18 23:59:49 +02:00
$this->phoneNumberFixture = $phoneUtil->format($example, PhoneNumberFormat::NATIONAL);
2022-07-15 23:54:52 +02:00
2022-07-11 15:06:18 +02:00
$this->recipient = new Recipient($this->phoneNumberFixture, $this->phoneCountryFixture);
}
/**
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
unset($this->recipient);
}
/**
* @test
* @return void
* @throws NumberParseException
* @throws RecipientException
2022-07-11 15:06:18 +02:00
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\Recipient::__construct
2022-07-11 15:06:18 +02:00
*/
public function testConstructValid()
{
/** @var PhoneNumberUtil|MockObject $phoneNumberUtilMock */
$phoneNumberUtilMock = $this->getMockBuilder(PhoneNumberUtil::class)
->onlyMethods(['parse', 'format', 'isValidNumber', 'getNumberType'])
2022-07-11 15:06:18 +02:00
->disableOriginalConstructor()
->getMock();
$phoneNumberUtilMock->method('parse')->willReturn(new PhoneNumber());
$phoneNumberUtilMock->method('format')->willReturn('+491527565839');
$phoneNumberUtilMock->method('isValidNumber')->willReturn(true);
$phoneNumberUtilMock->method('getNumberType')->willReturn(PhoneNumberType::MOBILE);
2022-07-11 15:06:18 +02:00
/** @var Recipient|MockObject $recipientMock */
$recipientMock = $this->getMockBuilder(Recipient::class)
->onlyMethods(['getPhoneNumberUtil'])
->disableOriginalConstructor()
->getMock();
$recipientMock->method('getPhoneNumberUtil')->willReturn($phoneNumberUtilMock);
$recipientMock->__construct($this->phoneNumberFixture, $this->phoneCountryFixture);
$this->assertSame(
'+491527565839',
2022-07-11 15:06:18 +02:00
$this->callMethod(
$recipientMock,
'get'
)
);
$this->assertSame(
$this->phoneCountryFixture,
$this->callMethod(
$recipientMock,
'getCountryCode'
)
);
}
/**
* @test
*
* @param $number
* @param $country
* @param $validNumber
* @param $numberType
2022-07-11 15:06:18 +02:00
* @param $expectedException
*
* @return void
* @throws NumberParseException
* @throws RecipientException
2022-07-11 15:06:18 +02:00
* @dataProvider constructInvalidDataProvider
* @covers \D3\LinkmobilityClient\ValueObject\Recipient::__construct
2022-07-11 15:06:18 +02:00
*/
public function testConstructInvalid($number, $country, $validNumber, $numberType, $expectedException)
2022-07-11 15:06:18 +02:00
{
/** @var PhoneNumberUtil|MockObject $phoneNumberUtilMock */
$phoneNumberUtilMock = $this->getMockBuilder(PhoneNumberUtil::class)
->onlyMethods(['parse', 'format', 'isValidNumber', 'getNumberType'])
2022-07-11 15:06:18 +02:00
->disableOriginalConstructor()
->getMock();
if ($number === 'abc') {
2022-07-13 10:41:23 +02:00
$phoneNumberUtilMock->method('parse')->willThrowException(new NumberParseException(0, 'message'));
2022-07-11 15:06:18 +02:00
} else {
2022-07-13 10:41:23 +02:00
$phoneNumberUtilMock->method('parse')->willReturn(new PhoneNumber());
2022-07-11 15:06:18 +02:00
}
$phoneNumberUtilMock->method('format')->willReturn($number);
$phoneNumberUtilMock->method('isValidNumber')->willReturn($validNumber);
$phoneNumberUtilMock->method('getNumberType')->willReturn($numberType);
2022-07-11 15:06:18 +02:00
/** @var Recipient|MockObject $recipientMock */
$recipientMock = $this->getMockBuilder(Recipient::class)
->onlyMethods(['getPhoneNumberUtil'])
->disableOriginalConstructor()
->getMock();
$recipientMock->method('getPhoneNumberUtil')->willReturn($phoneNumberUtilMock);
$this->expectException($expectedException);
$recipientMock->__construct($number, $country);
}
/**
2022-07-21 14:07:10 +02:00
* @return array[]
2022-07-11 15:06:18 +02:00
*/
public function constructInvalidDataProvider(): array
{
2022-07-16 00:42:41 +02:00
$phoneUtil = PhoneNumberUtil::getInstance();
$example = $phoneUtil->getExampleNumberForType($this->phoneCountryFixture, PhoneNumberType::MOBILE);
2022-07-18 23:59:49 +02:00
$phoneNumberFixture = $phoneUtil->format($example, PhoneNumberFormat::NATIONAL);
2022-07-16 00:42:41 +02:00
2022-07-11 15:06:18 +02:00
return [
'empty number' => ['', 'DE', true, PhoneNumberType::MOBILE, InvalidArgumentException::class],
'invalid country code' => [$phoneNumberFixture, 'DEX', true, PhoneNumberType::MOBILE, InvalidArgumentException::class],
'unparsable' => ['abc', 'DE', true, PhoneNumberType::MOBILE, NumberParseException::class],
'invalid number' => ['abcd', 'DE', false, PhoneNumberType::MOBILE, RecipientException::class],
'not mobile number' => ['abcd', 'DE', true, PhoneNumberType::FIXED_LINE, RecipientException::class],
2022-07-11 15:06:18 +02:00
];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\Recipient::getPhoneNumberUtil
2022-07-11 15:06:18 +02:00
*/
public function testGetPhoneNumberUtil()
{
$this->assertInstanceOf(
PhoneNumberUtil::class,
$this->callMethod(
$this->recipient,
'getPhoneNumberUtil'
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\Recipient::getCountryCode
*/
public function testGetCountryCode()
{
$this->assertSame(
$this->phoneCountryFixture,
$this->callMethod(
$this->recipient,
'getCountryCode'
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\ValueObject::get
* @covers \D3\LinkmobilityClient\ValueObject\StringValueObject::get
* @covers \D3\LinkmobilityClient\ValueObject\StringValueObject::__toString
* @covers \D3\LinkmobilityClient\ValueObject\Recipient::get
*/
public function testGet()
{
$this->assertSame(
'+4915123456789',
(string) $this->recipient
);
$this->assertSame(
'+4915123456789',
$this->callMethod(
$this->recipient,
'get'
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\ValueObject::getFormatted
* @covers \D3\LinkmobilityClient\ValueObject\StringValueObject::getFormatted
* @covers \D3\LinkmobilityClient\ValueObject\Recipient::getFormatted
*/
public function testGetFormatted()
{
$this->assertSame(
'4915123456789',
$this->callMethod(
$this->recipient,
'getFormatted'
)
);
}
2022-07-13 10:41:23 +02:00
}