add tests for some classes

This commit is contained in:
2022-07-11 15:06:18 +02:00
parent 184695c076
commit 67291c19da
18 changed files with 1225 additions and 27 deletions

View File

@ -114,9 +114,9 @@ class Client
}
/**
* @return LoggerInterface
* @return LoggerInterface|null
*/
public function getLogger(): ?LoggerInterface
public function getLogger()
{
return $this->hasLogger() ? $this->logger : null;
}

View File

@ -43,9 +43,22 @@ class RecipientsList implements RecipientsListInterface, Iterator
$this->setClient($client);
}
/**
* @return PhoneNumberUtil
*/
protected function getPhoneNumberUtil(): PhoneNumberUtil
{
return PhoneNumberUtil::getInstance();
}
/**
* @param Recipient $recipient
*
* @return RecipientsListInterface
*/
public function add(Recipient $recipient) : RecipientsListInterface
{
$phoneUtil = PhoneNumberUtil::getInstance();
$phoneUtil = $this->getPhoneNumberUtil();
try {
$phoneNumber = $phoneUtil->parse( $recipient->get(), $recipient->getCountryCode() );
@ -64,6 +77,7 @@ class RecipientsList implements RecipientsListInterface, Iterator
}
$this->recipients[ md5( serialize( $recipient ) ) ] = $recipient;
} catch (NumberParseException $e) {
if ($this->getClient()->hasLogger()) {
$this->getClient()->getLogger()->info($e->getMessage());
@ -77,6 +91,9 @@ class RecipientsList implements RecipientsListInterface, Iterator
return $this;
}
/**
* @return RecipientsListInterface
*/
public function clearRecipents() : RecipientsListInterface
{
$this->recipients = [];
@ -127,7 +144,7 @@ class RecipientsList implements RecipientsListInterface, Iterator
public function rewind()
{
return reset($this->recipients);
reset($this->recipients);
}
public function valid(): bool
@ -145,9 +162,13 @@ class RecipientsList implements RecipientsListInterface, Iterator
/**
* @param Client $client
*
* @return RecipientsList
*/
public function setClient( Client $client )
public function setClient( Client $client ): RecipientsList
{
$this->client = $client;
return $this;
}
}

View File

@ -19,7 +19,6 @@ namespace D3\LinkmobilityClient\SMS;
use D3\LinkmobilityClient\Client;
use D3\LinkmobilityClient\ValueObject\SmsBinaryMessage;
use D3\LinkmobilityClient\ValueObject\SmsMessageInterface;
use D3\LinkmobilityClient\ValueObject\SmsTextMessage;
use Phlib\SmsLength\SmsLength;

View File

@ -26,7 +26,7 @@ class Recipient extends StringValueObject
{
Assert::that($iso2CountryCode)->string()->length(2);
$phoneUtil = PhoneNumberUtil::getInstance();
$phoneUtil = $this->getPhoneNumberUtil();
$phoneNumber = $phoneUtil->parse($number, strtoupper($iso2CountryCode));
$number = ltrim($phoneUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');
@ -35,6 +35,14 @@ class Recipient extends StringValueObject
$this->countryCode = $iso2CountryCode;
}
/**
* @return PhoneNumberUtil
*/
protected function getPhoneNumberUtil(): PhoneNumberUtil
{
return PhoneNumberUtil::getInstance();
}
/**
* @return string
*/

View File

@ -24,7 +24,7 @@ class Sender extends StringValueObject
{
Assert::that($iso2CountryCode)->string()->length(2);
$phoneUtil = PhoneNumberUtil::getInstance();
$phoneUtil = $this->getPhoneNumberUtil();
$phoneNumber = $phoneUtil->parse( $number, strtoupper($iso2CountryCode) );
$number = $phoneUtil->format( $phoneNumber, PhoneNumberFormat::E164 );
@ -35,4 +35,12 @@ class Sender extends StringValueObject
parent::__construct( $number);
}
/**
* @return PhoneNumberUtil
*/
protected function getPhoneNumberUtil(): PhoneNumberUtil
{
return PhoneNumberUtil::getInstance();
}
}

View File

@ -15,17 +15,16 @@ class SmsBinaryMessage extends SmsMessageAbstract
{
parent::__construct( $message);
$smsLength = new SmsLength($this->value);
$smsLength->validate();
$this->getSmsLength()->validate();
}
/**
* @return array|false|string
* @return array|false
*/
public function getMessageContent()
{
return str_split(
base64_encode($this->value),
base64_encode($this->get()),
SmsLength::MAXIMUM_CHARACTERS_UCS2_SINGLE
);
}

View File

@ -8,13 +8,20 @@ use Phlib\SmsLength\SmsLength;
abstract class SmsMessageAbstract extends StringValueObject implements SmsMessageInterface
{
/**
* @return SmsLength
*/
public function getSmsLength(): SmsLength
{
return new SmsLength($this->get());
}
/**
* @return int
*/
public function chunkCount() : int
{
$smsLength = new SmsLength($this->value);
return $smsLength->getMessageCount();
return $this->getSmsLength()->getMessageCount();
}
/**
@ -22,12 +29,11 @@ abstract class SmsMessageAbstract extends StringValueObject implements SmsMessag
*/
public function length() : int
{
$smsLength = new SmsLength($this->value);
return $smsLength->getSize();
return $this->getSmsLength()->getSize();
}
public function getMessageContent() : string
public function getMessageContent()
{
return (string) $this->value;
return $this->get();
}
}

View File

@ -21,5 +21,5 @@ interface SmsMessageInterface
public function length() : int;
public function getMessageContent() : string;
public function getMessageContent();
}

View File

@ -5,7 +5,6 @@ declare(strict_types=1);
namespace D3\LinkmobilityClient\ValueObject;
use InvalidArgumentException;
use Phlib\SmsLength\SmsLength;
class SmsTextMessage extends SmsMessageAbstract
{
@ -17,7 +16,7 @@ class SmsTextMessage extends SmsMessageAbstract
{
parent::__construct( $message);
$smsLength = new SmsLength($this->value);
$smsLength = $this->getSmsLength();
$smsLength->validate();
}
}

View File

@ -20,7 +20,7 @@ abstract class StringValueObject extends ValueObject
return $this->get();
}
public function get() : string
public function get(): string
{
return $this->value;
}