linkmobility-php-client/Tests/RecipientsList/RecipientsListTest.php

374 lignes
9.7 KiB
PHP
Brut Lien permanent Vue normale Historique

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\RecipientsList;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\RecipientsList\RecipientsList;
use D3\LinkmobilityClient\Tests\ApiTestCase;
use D3\LinkmobilityClient\ValueObject\Recipient;
2022-07-15 23:54:52 +02:00
use libphonenumber\PhoneNumberFormat;
2022-07-11 15:06:18 +02:00
use libphonenumber\PhoneNumberType;
use libphonenumber\PhoneNumberUtil;
use PHPUnit\Framework\MockObject\MockObject;
use ReflectionException;
use stdClass;
class RecipientsListTest extends ApiTestCase
{
/** @var RecipientsList */
public $recipientsList;
2022-07-15 23:54:52 +02:00
private $phoneNumberFixture;
2022-07-11 15:06:18 +02:00
private $phoneCountryFixture = 'DE';
/**
* @return void
*/
2022-07-13 10:41:23 +02:00
public function setUp(): void
2022-07-11 15:06:18 +02:00
{
parent::setUp();
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$this->recipientsList = new RecipientsList($clientMock);
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-11 15:06:18 +02:00
}
/**
* @return void
*/
public function tearDown(): void
{
parent::tearDown();
unset($this->recipientsList);
}
/**
* @test
* @return void
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::__construct
2022-07-11 15:06:18 +02:00
*/
public function testConstruct()
{
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$this->recipientsList = new RecipientsList($clientMock);
$this->assertSame(
$this->recipientsList->getClient(),
$clientMock
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::add
2022-07-11 15:06:18 +02:00
*/
public function testAddValidNumber()
{
/** @var Recipient|MockObject $recipientMock */
$recipientMock = $this->getMockBuilder(Recipient::class)
->onlyMethods(['get', 'getCountryCode'])
->setConstructorArgs([$this->phoneNumberFixture, $this->phoneCountryFixture])
->getMock();
$recipientMock->method('get')->willReturn($this->phoneNumberFixture);
$recipientMock->method('getCountryCode')->willReturn($this->phoneCountryFixture);
$this->assertCount(
0,
$this->callMethod($this->recipientsList, 'getRecipientsList')
);
$this->assertSame(
$this->recipientsList,
$this->callMethod(
$this->recipientsList,
'add',
[$recipientMock]
)
);
$this->assertCount(
1,
$this->callMethod($this->recipientsList, 'getRecipientsList')
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::clearRecipents
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::getRecipientsList
2022-07-11 15:06:18 +02:00
*/
public function testClearRecipents()
{
$this->addRecipientFixture();
$this->assertCount(
2,
$this->callMethod($this->recipientsList, 'getRecipientsList')
);
$this->assertInstanceOf(
RecipientsList::class,
$this->callMethod(
$this->recipientsList,
'clearRecipents'
)
);
$this->assertCount(
0,
$this->callMethod($this->recipientsList, 'getRecipientsList')
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::getRecipients
2022-07-11 15:06:18 +02:00
*/
public function testGetRecipients()
{
$this->assertCount(
0,
$this->callMethod(
$this->recipientsList,
'getRecipients'
)
);
$this->addRecipientFixture();
$this->assertSame(
[
$this->phoneNumberFixture,
$this->phoneNumberFixture,
2022-07-11 15:06:18 +02:00
],
$this->callMethod(
$this->recipientsList,
'getRecipients'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::getRecipientsList
2022-07-11 15:06:18 +02:00
*/
public function testGetRecipientsList()
{
$this->assertCount(
0,
$this->callMethod(
$this->recipientsList,
'getRecipientsList'
)
);
$this->addRecipientFixture();
$recipientsList = $this->callMethod(
$this->recipientsList,
'getRecipientsList'
);
$this->assertCount(
2,
$recipientsList
);
$this->assertInstanceOf(
Recipient::class,
$recipientsList['fixture']
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::current
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::next
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::key
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::rewind
2022-07-11 15:06:18 +02:00
*/
public function testCurrentNextKeyRewind()
{
$list = $this->addRecipientFixture();
$current = $this->callMethod(
$this->recipientsList,
'current'
);
$this->assertInstanceOf(
Recipient::class,
$current
);
$this->assertSame(
$list['fixture2'],
$this->callMethod(
$this->recipientsList,
'next'
)
);
$this->assertSame(
'fixture2',
$this->callMethod(
$this->recipientsList,
'key'
)
);
$this->callMethod(
$this->recipientsList,
'rewind'
);
$this->assertSame(
'fixture',
$this->callMethod(
$this->recipientsList,
'key'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::valid
2022-07-11 15:06:18 +02:00
*/
public function testValid()
{
$this->addRecipientFixture();
$this->assertTrue(
$this->callMethod(
$this->recipientsList,
'valid'
)
);
$this->setValue(
$this->recipientsList,
'recipients',
[
'fixture' => new stdClass(),
2022-07-11 15:06:18 +02:00
]
);
$this->assertFalse(
$this->callMethod(
$this->recipientsList,
'valid'
)
);
}
/**
* @test
* @throws ReflectionException
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::setClient
* @covers \D3\LinkmobilityClient\RecipientsList\RecipientsList::getClient
2022-07-11 15:06:18 +02:00
*/
public function testSetGetClient()
{
/** @var Client|MockObject $clientMock */
$clientMock = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
2022-07-11 15:06:18 +02:00
$this->assertInstanceOf(
RecipientsList::class,
$this->callMethod(
$this->recipientsList,
'setClient',
[$clientMock]
)
);
$this->assertSame(
$clientMock,
$this->callMethod(
$this->recipientsList,
'getClient'
)
);
}
/**
* @return Recipient[]|MockObject[]
* @throws ReflectionException
*/
protected function addRecipientFixture(): array
{
/** @var Recipient|MockObject $recipientMock */
2022-07-13 10:41:23 +02:00
$recipientMock = $this->getMockBuilder(Recipient::class)
->onlyMethods([
2022-07-11 15:06:18 +02:00
'get',
'getCountryCode',
2022-07-13 10:41:23 +02:00
])
->setConstructorArgs([
2022-07-11 15:06:18 +02:00
$this->phoneNumberFixture,
$this->phoneCountryFixture,
2022-07-13 10:41:23 +02:00
])
2022-07-11 15:06:18 +02:00
->getMock();
2022-07-13 10:41:23 +02:00
$recipientMock->method('get')->willReturn($this->phoneNumberFixture);
$recipientMock->method('getCountryCode')->willReturn($this->phoneCountryFixture);
2022-07-11 15:06:18 +02:00
/** @var Recipient|MockObject $recipientMock2 */
2022-07-13 10:41:23 +02:00
$recipientMock2 = $this->getMockBuilder(Recipient::class)
->onlyMethods([
2022-07-11 15:06:18 +02:00
'get',
'getCountryCode',
2022-07-13 10:41:23 +02:00
])
->setConstructorArgs([
2022-07-11 15:06:18 +02:00
$this->phoneNumberFixture,
$this->phoneCountryFixture,
2022-07-13 10:41:23 +02:00
])
2022-07-11 15:06:18 +02:00
->getMock();
2022-07-13 10:41:23 +02:00
$recipientMock2->method('get')->willReturn($this->phoneNumberFixture);
$recipientMock2->method('getCountryCode')->willReturn($this->phoneCountryFixture);
2022-07-11 15:06:18 +02:00
$list = [
'fixture' => $recipientMock,
'fixture2' => $recipientMock2,
2022-07-11 15:06:18 +02:00
];
$this->setValue(
$this->recipientsList,
'recipients',
$list
);
return $list;
}
2022-07-13 10:41:23 +02:00
}