diff --git a/README.md b/README.md index c7c344d..385ee04 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ | Протокол | API | ФФД | Статус | |----------|-----|------|-------------| -| v4 | 5.7 | 1.05 | Рефакторинг | +| v4 | 5.8 | 1.05 | Рефакторинг | | v5 | 2.0 | 1.2 | В планах | ## Плюшечки diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index e0eee84..82eb1fc 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -7,11 +7,12 @@ * https://github.com/anthonyaxenov/atol-online/blob/master/LICENSE */ -declare(strict_types = 1); +declare(strict_types=1); namespace AtolOnline\Entities; use ArrayAccess; +use BadMethodCallException; use Illuminate\Contracts\Support\Arrayable; use JsonSerializable; use Stringable; @@ -65,7 +66,7 @@ abstract class Entity implements JsonSerializable, Stringable, Arrayable, ArrayA */ public function offsetSet(mixed $offset, mixed $value) { - throw new \BadMethodCallException( + throw new BadMethodCallException( 'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.' ); } @@ -75,7 +76,7 @@ abstract class Entity implements JsonSerializable, Stringable, Arrayable, ArrayA */ public function offsetUnset(mixed $offset): void { - throw new \BadMethodCallException( + throw new BadMethodCallException( 'Объект ' . static::class . ' нельзя изменять как массив. Следует использовать сеттеры.' ); } diff --git a/tests/AtolOnline/Tests/BasicTestCase.php b/tests/AtolOnline/Tests/BasicTestCase.php index 8c6aacf..22f1a1b 100644 --- a/tests/AtolOnline/Tests/BasicTestCase.php +++ b/tests/AtolOnline/Tests/BasicTestCase.php @@ -93,6 +93,7 @@ class BasicTestCase extends TestCase * @param Entity|EntityCollection $entity * @param array|null $json_structure * @covers \AtolOnline\Entities\Entity::__toString + * @covers \AtolOnline\Entities\Entity::toArray * @covers \AtolOnline\Entities\Entity::jsonSerialize * @covers \AtolOnline\Collections\EntityCollection::jsonSerialize * @throws Exception @@ -100,6 +101,8 @@ class BasicTestCase extends TestCase public function assertIsAtolable(Entity|EntityCollection $entity, ?array $json_structure = null): void { $this->assertIsArray($entity->jsonSerialize()); + $this->assertIsArray($entity->toArray()); + $this->assertEquals($entity->jsonSerialize(), $entity->toArray()); $this->assertIsString((string)$entity); $this->assertJson((string)$entity); if (!is_null($json_structure)) { diff --git a/tests/AtolOnline/Tests/Entities/ClientTest.php b/tests/AtolOnline/Tests/Entities/ClientTest.php index 25c8976..b68c8c3 100644 --- a/tests/AtolOnline/Tests/Entities/ClientTest.php +++ b/tests/AtolOnline/Tests/Entities/ClientTest.php @@ -9,15 +9,16 @@ namespace AtolOnline\Tests\Entities; -use AtolOnline\{ - Entities\Client, +use AtolOnline\{Entities\Client, Exceptions\InvalidEmailException, Exceptions\InvalidInnLengthException, Exceptions\InvalidPhoneException, Exceptions\TooLongClientNameException, Exceptions\TooLongEmailException, Helpers, - Tests\BasicTestCase}; + Tests\BasicTestCase +}; +use BadMethodCallException; 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(phone: '+1/22/99*73s dsdas654 5s6'), ['phone' => '+122997365456']); $this->assertIsAtolable(new Client(inn: '+fasd3\qe3fs_=nac99013928czc'), ['inn' => '3399013928']); - $this->assertIsAtolable(new Client( - 'John Doe', - 'john@example.com', - '+1/22/99*73s dsdas654 5s6', // +122997365456 - '+fasd3\qe3fs_=nac99013928czc' // 3399013928 - ), [ - 'name' => 'John Doe', - 'email' => 'john@example.com', - 'phone' => '+122997365456', - 'inn' => '3399013928', - ]); + $this->assertIsAtolable( + new Client( + 'John Doe', + 'john@example.com', + '+1/22/99*73s dsdas654 5s6', // +122997365456 + '+fasd3\qe3fs_=nac99013928czc' // 3399013928 + ), [ + 'name' => 'John Doe', + 'email' => 'john@example.com', + 'phone' => '+122997365456', + 'inn' => '3399013928', + ] + ); } /** @@ -131,12 +134,12 @@ class ClientTest extends BasicTestCase /** * Тестирует установку валидного телефона * + * @throws InvalidPhoneException * @todo актуализировать при доработатанной валидации * @dataProvider providerValidPhones * @covers \AtolOnline\Entities\Client * @covers \AtolOnline\Entities\Client::setPhone * @covers \AtolOnline\Entities\Client::getPhone - * @throws InvalidPhoneException */ public function testValidPhone(string $input, string $output): void { @@ -146,11 +149,11 @@ class ClientTest extends BasicTestCase /** * Тестирует установку невалидного телефона * + * @throws InvalidPhoneException * @todo актуализировать при доработатанной валидации * @covers \AtolOnline\Entities\Client * @covers \AtolOnline\Entities\Client::setPhone * @covers \AtolOnline\Exceptions\InvalidPhoneException - * @throws InvalidPhoneException */ public function testInvalidPhoneException(): void { @@ -247,4 +250,45 @@ class ClientTest extends BasicTestCase $this->expectException(InvalidInnLengthException::class); (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']); + } }