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

231 lines
7.5 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;
use D3\LinkmobilityClient\Tests\ApiTestCase;
use D3\LinkmobilityClient\ValueObject\Sender;
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 SenderTest extends ApiTestCase
{
2022-07-21 00:33:29 +02:00
/** @var Sender|MockObject */
2022-07-11 15:06:18 +02:00
public $sender;
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-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-13 10:41:23 +02:00
$this->sender = new Sender($this->phoneNumberFixture, $this->phoneCountryFixture);
2022-07-11 15:06:18 +02:00
}
/**
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
unset($this->sender);
}
/**
* @test
* @return void
* @throws NumberParseException
* @throws RecipientException
* @throws ReflectionException
* @dataProvider constructValidDataProvider
* @covers \D3\LinkmobilityClient\ValueObject\Sender::__construct
2022-07-11 15:06:18 +02:00
*/
public function testConstructValid($number, $country, $hasNumber)
2022-07-11 15:06:18 +02:00
{
/** @var PhoneNumberUtil|MockObject $phoneNumberUtilMock */
$phoneNumberUtilMock = $this->getMockBuilder(PhoneNumberUtil::class)
->onlyMethods(['parse', 'format', 'isValidNumber'])
->disableOriginalConstructor()
->getMock();
$phoneNumberUtilMock->method('parse')->willReturn(new PhoneNumber());
$phoneNumberUtilMock->method('format')->willReturn('4915792300219');
$phoneNumberUtilMock->method('isValidNumber')->willReturn(true);
/** @var Sender|MockObject $senderMock */
$senderMock = $this->getMockBuilder(Sender::class)
->onlyMethods(['getPhoneNumberUtil'])
->disableOriginalConstructor()
->getMock();
$senderMock->method('getPhoneNumberUtil')->willReturn($phoneNumberUtilMock);
$senderMock->__construct($number, $country);
2022-07-11 15:06:18 +02:00
$this->assertSame(
$hasNumber,
2022-07-11 15:06:18 +02:00
$this->callMethod(
$senderMock,
'get'
) === '4915792300219'
2022-07-11 15:06:18 +02:00
);
}
/**
* @return array[]
*/
public function constructValidDataProvider(): array
{
$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);
return [
'null number' => [null, $this->phoneCountryFixture, false],
'null country' => [$this->phoneNumberFixture, null, false],
'all values' => [$this->phoneNumberFixture, $this->phoneCountryFixture, true],
];
}
2022-07-11 15:06:18 +02:00
/**
* @test
* @param $number
* @param $country
* @param $validNumber
* @param $expectedException
* @throws ReflectionException
2022-07-11 15:06:18 +02:00
* @dataProvider constructInvalidDataProvider
* @covers \D3\LinkmobilityClient\ValueObject\Sender::__construct
2022-07-11 15:06:18 +02:00
*/
public function testConstructInvalid($number, $country, $validNumber, $expectedException)
{
/** @var PhoneNumberUtil|MockObject $phoneNumberUtilMock */
$phoneNumberUtilMock = $this->getMockBuilder(PhoneNumberUtil::class)
->onlyMethods(['parse', 'format', 'isValidNumber'])
->disableOriginalConstructor()
->getMock();
if ($number === 'abc') {
$phoneNumberUtilMock->expects($this->exactly((int) ($country !== 'DEX')))->method('parse')
->willThrowException(new NumberParseException(0, 'message'));
2022-07-11 15:06:18 +02:00
} else {
$phoneNumberUtilMock->expects($this->exactly((int) ($country !== 'DEX')))->method('parse')
->willReturn(new PhoneNumber());
2022-07-11 15:06:18 +02:00
}
$phoneNumberUtilMock->method('format')->willReturn($number);
$phoneNumberUtilMock->method('isValidNumber')->willReturn((bool) $validNumber);
2022-07-11 15:06:18 +02:00
/** @var Sender|MockObject $senderMock */
$senderMock = $this->getMockBuilder(Sender::class)
->onlyMethods(['getPhoneNumberUtil'])
->disableOriginalConstructor()
->getMock();
$senderMock->method('getPhoneNumberUtil')->willReturn($phoneNumberUtilMock);
$this->expectException($expectedException);
$this->callMethod(
$senderMock,
'__construct',
[$number, $country]
);
2022-07-11 15:06:18 +02:00
}
/**
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, InvalidArgumentException::class],
2022-07-16 00:42:41 +02:00
'invalid country code' => [$phoneNumberFixture, 'DEX', true, InvalidArgumentException::class],
2022-07-11 15:06:18 +02:00
'unparsable' => ['abc', 'DE', true, NumberParseException::class],
'invalid number' => ['abcd', 'DE', false, RecipientException::class],
2022-07-11 15:06:18 +02:00
];
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\Sender::getPhoneNumberUtil
2022-07-11 15:06:18 +02:00
*/
public function testGetPhoneNumberUtil()
{
$this->assertInstanceOf(
PhoneNumberUtil::class,
$this->callMethod(
$this->sender,
'getPhoneNumberUtil'
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\ValueObject::get
* @covers \D3\LinkmobilityClient\ValueObject\StringValueObject::get
* @covers \D3\LinkmobilityClient\ValueObject\Sender::get
*/
public function testGet()
{
$this->assertSame(
'+4915123456789',
$this->callMethod(
$this->sender,
'get'
)
);
}
/**
* @test
* @return void
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\ValueObject\ValueObject::getFormatted
* @covers \D3\LinkmobilityClient\ValueObject\StringValueObject::getFormatted
* @covers \D3\LinkmobilityClient\ValueObject\Sender::getFormatted
*/
public function testGetFormatted()
{
$this->assertSame(
'4915123456789',
$this->callMethod(
$this->sender,
'getFormatted'
)
);
}
2022-07-13 10:41:23 +02:00
}