Допокрытие `ArrayAccess`-методов

pull/15/head
Anthony Axenov 2021-12-12 11:09:12 +08:00
parent b4af189292
commit 294a3ef2f3
4 changed files with 68 additions and 20 deletions

View File

@ -25,7 +25,7 @@
| Протокол | API | ФФД | Статус | | Протокол | API | ФФД | Статус |
|----------|-----|------|-------------| |----------|-----|------|-------------|
| v4 | 5.7 | 1.05 | Рефакторинг | | v4 | 5.8 | 1.05 | Рефакторинг |
| v5 | 2.0 | 1.2 | В планах | | v5 | 2.0 | 1.2 | В планах |
## Плюшечки ## Плюшечки

View File

@ -7,11 +7,12 @@
* https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE * https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE
*/ */
declare(strict_types = 1); declare(strict_types=1);
namespace AtolOnline\Entities; namespace AtolOnline\Entities;
use ArrayAccess; use ArrayAccess;
use BadMethodCallException;
use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Arrayable;
use JsonSerializable; use JsonSerializable;
use Stringable; use Stringable;
@ -65,7 +66,7 @@ abstract class Entity implements JsonSerializable, Stringable, Arrayable, ArrayA
*/ */
public function offsetSet(mixed $offset, mixed $value) public function offsetSet(mixed $offset, mixed $value)
{ {
throw new \BadMethodCallException( throw new BadMethodCallException(
'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.' 'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.'
); );
} }
@ -75,7 +76,7 @@ abstract class Entity implements JsonSerializable, Stringable, Arrayable, ArrayA
*/ */
public function offsetUnset(mixed $offset): void public function offsetUnset(mixed $offset): void
{ {
throw new \BadMethodCallException( throw new BadMethodCallException(
'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.' 'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.'
); );
} }

View File

@ -93,6 +93,7 @@ class BasicTestCase extends TestCase
* @param Entity|EntityCollection $entity * @param Entity|EntityCollection $entity
* @param array|null $json_structure * @param array|null $json_structure
* @covers \AtolOnline\Entities\Entity::__toString * @covers \AtolOnline\Entities\Entity::__toString
* @covers \AtolOnline\Entities\Entity::toArray
* @covers \AtolOnline\Entities\Entity::jsonSerialize * @covers \AtolOnline\Entities\Entity::jsonSerialize
* @covers \AtolOnline\Collections\EntityCollection::jsonSerialize * @covers \AtolOnline\Collections\EntityCollection::jsonSerialize
* @throws Exception * @throws Exception
@ -100,6 +101,8 @@ class BasicTestCase extends TestCase
public function assertIsAtolable(Entity|EntityCollection $entity, ?array $json_structure = null): void public function assertIsAtolable(Entity|EntityCollection $entity, ?array $json_structure = null): void
{ {
$this->assertIsArray($entity->jsonSerialize()); $this->assertIsArray($entity->jsonSerialize());
$this->assertIsArray($entity->toArray());
$this->assertEquals($entity->jsonSerialize(), $entity->toArray());
$this->assertIsString((string)$entity); $this->assertIsString((string)$entity);
$this->assertJson((string)$entity); $this->assertJson((string)$entity);
if (!is_null($json_structure)) { if (!is_null($json_structure)) {

View File

@ -9,15 +9,16 @@
namespace AtolOnline\Tests\Entities; namespace AtolOnline\Tests\Entities;
use AtolOnline\{ use AtolOnline\{Entities\Client,
Entities\Client,
Exceptions\InvalidEmailException, Exceptions\InvalidEmailException,
Exceptions\InvalidInnLengthException, Exceptions\InvalidInnLengthException,
Exceptions\InvalidPhoneException, Exceptions\InvalidPhoneException,
Exceptions\TooLongClientNameException, Exceptions\TooLongClientNameException,
Exceptions\TooLongEmailException, Exceptions\TooLongEmailException,
Helpers, Helpers,
Tests\BasicTestCase}; Tests\BasicTestCase
};
use BadMethodCallException;
use Exception; use Exception;
/** /**
@ -58,17 +59,19 @@ class ClientTest extends BasicTestCase
$this->assertIsAtolable(new Client(email: 'john@example.com'), ['email' => 'john@example.com']); $this->assertIsAtolable(new Client(email: 'john@example.com'), ['email' => 'john@example.com']);
$this->assertIsAtolable(new Client(phone: '+1/22/99*73s dsdas654 5s6'), ['phone' => '+122997365456']); $this->assertIsAtolable(new Client(phone: '+1/22/99*73s dsdas654 5s6'), ['phone' => '+122997365456']);
$this->assertIsAtolable(new Client(inn: '+fasd3\qe3fs_=nac99013928czc'), ['inn' => '3399013928']); $this->assertIsAtolable(new Client(inn: '+fasd3\qe3fs_=nac99013928czc'), ['inn' => '3399013928']);
$this->assertIsAtolable(new Client( $this->assertIsAtolable(
'John Doe', new Client(
'john@example.com', 'John Doe',
'+1/22/99*73s dsdas654 5s6', // +122997365456 'john@example.com',
'+fasd3\qe3fs_=nac99013928czc' // 3399013928 '+1/22/99*73s dsdas654 5s6', // +122997365456
), [ '+fasd3\qe3fs_=nac99013928czc' // 3399013928
'name' => 'John Doe', ), [
'email' => 'john@example.com', 'name' => 'John Doe',
'phone' => '+122997365456', 'email' => 'john@example.com',
'inn' => '3399013928', 'phone' => '+122997365456',
]); 'inn' => '3399013928',
]
);
} }
/** /**
@ -131,12 +134,12 @@ class ClientTest extends BasicTestCase
/** /**
* Тестирует установку валидного телефона * Тестирует установку валидного телефона
* *
* @throws InvalidPhoneException
* @todo актуализировать при доработатанной валидации * @todo актуализировать при доработатанной валидации
* @dataProvider providerValidPhones * @dataProvider providerValidPhones
* @covers \AtolOnline\Entities\Client * @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::setPhone * @covers \AtolOnline\Entities\Client::setPhone
* @covers \AtolOnline\Entities\Client::getPhone * @covers \AtolOnline\Entities\Client::getPhone
* @throws InvalidPhoneException
*/ */
public function testValidPhone(string $input, string $output): void public function testValidPhone(string $input, string $output): void
{ {
@ -146,11 +149,11 @@ class ClientTest extends BasicTestCase
/** /**
* Тестирует установку невалидного телефона * Тестирует установку невалидного телефона
* *
* @throws InvalidPhoneException
* @todo актуализировать при доработатанной валидации * @todo актуализировать при доработатанной валидации
* @covers \AtolOnline\Entities\Client * @covers \AtolOnline\Entities\Client
* @covers \AtolOnline\Entities\Client::setPhone * @covers \AtolOnline\Entities\Client::setPhone
* @covers \AtolOnline\Exceptions\InvalidPhoneException * @covers \AtolOnline\Exceptions\InvalidPhoneException
* @throws InvalidPhoneException
*/ */
public function testInvalidPhoneException(): void public function testInvalidPhoneException(): void
{ {
@ -247,4 +250,45 @@ class ClientTest extends BasicTestCase
$this->expectException(InvalidInnLengthException::class); $this->expectException(InvalidInnLengthException::class);
(new Client())->setInn('1234567890123'); (new Client())->setInn('1234567890123');
} }
/**
* Тестирует обращение к атрибутам объекта как к элементам массива
*
* @covers \AtolOnline\Entities\Entity::offsetGet
* @covers \AtolOnline\Entities\Entity::offsetExists
* @return void
*/
public function testOffsetGetExists(): void
{
$client = new Client('John Doe');
$this->assertEquals('John Doe', $client['name']);
$this->assertTrue(isset($client['name']));
$this->assertFalse(isset($client['qwerty']));
}
/**
* Тестирует выброс исключения при попытке задать значение атрибуту объекта как элементу массива
*
* @covers \AtolOnline\Entities\Entity::offsetSet
* @return void
*/
public function testBadMethodCallExceptionBySet(): void
{
$this->expectException(BadMethodCallException::class);
$client = new Client('John Doe');
$client['name'] = 'qwerty';
}
/**
* Тестирует выброс исключения при попытке удалить значение атрибута объекта как элемент массива
*
* @covers \AtolOnline\Entities\Entity::offsetUnset
* @return void
*/
public function testBadMethodCallExceptionByUnset(): void
{
$this->expectException(BadMethodCallException::class);
$client = new Client('John Doe');
unset($client['name']);
}
} }