From 3b75c8b983e42e2693dc6b8759ec7c28222d3b0f Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Wed, 24 Nov 2021 17:57:24 +0800 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D1=81=D1=81=20`Supplier`,=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BA=D1=80=D1=8B=D1=82=D1=8B=D0=B9=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Это было слишком легко :( --- src/Entities/Supplier.php | 85 +++++++++++++++++ .../Tests/Entities/SupplierTest.php | 95 +++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 src/Entities/Supplier.php create mode 100644 tests/AtolOnline/Tests/Entities/SupplierTest.php diff --git a/src/Entities/Supplier.php b/src/Entities/Supplier.php new file mode 100644 index 0000000..4d58d08 --- /dev/null +++ b/src/Entities/Supplier.php @@ -0,0 +1,85 @@ +setPhones($phones); + } + + /** + * Возвращает установленные номера телефонов + * + * @todo вытащить в трейт + * @return Collection + */ + public function getPhones(): Collection + { + return $this->phones; + } + + /** + * Устанавливает массив номеров телефонов + * + * @todo вытащить в трейт + * @param array|Collection|null $phones + * @return $this + * @throws InvalidPhoneException + */ + public function setPhones(array|Collection|null $phones): self + { + if (!is_null($phones)) { + $phones = is_array($phones) ? collect($phones) : $phones; + $phones->each(function ($phone) { + $phone = preg_replace('/[^\d]/', '', trim($phone)); + if (preg_match(Constraints::PATTERN_PHONE, $phone) != 1) { + throw new InvalidPhoneException($phone); + } + }); + } + $this->phones = empty($phones) ? collect() : $phones; + return $this; + } + + /** + * @inheritDoc + */ + public function jsonSerialize(): array + { + $json = []; + !$this->getPhones()->isEmpty() && $json['phones'] = $this->getPhones()->toArray(); + return $json; + } +} diff --git a/tests/AtolOnline/Tests/Entities/SupplierTest.php b/tests/AtolOnline/Tests/Entities/SupplierTest.php new file mode 100644 index 0000000..d0df501 --- /dev/null +++ b/tests/AtolOnline/Tests/Entities/SupplierTest.php @@ -0,0 +1,95 @@ +assertEquals('[]', (string)(new Supplier())); + } + + /** + * Тестирует конструктор с передачей значений и корректное приведение к json + * + * @covers \AtolOnline\Entities\Supplier + * @covers \AtolOnline\Entities\Supplier::jsonSerialize + * @covers \AtolOnline\Entities\Supplier::setPhones + * @covers \AtolOnline\Entities\Supplier::getPhones + * @throws InvalidPhoneException + */ + public function testConstructorWithArgs(): void + { + $this->assertAtolable(new Supplier(['+122997365456']), ['phones' => ['+122997365456']]); + } + + /** + * Провайдер массивов телефонов, которые приводятся к null + * + * @return array + */ + public function providerNullablePhonesArrays(): array + { + return [ + [[]], + [null], + [collect()], + ]; + } + + /** + * Тестирует установку пустых телефонов + * + * @dataProvider providerNullablePhonesArrays + * @covers \AtolOnline\Entities\Supplier + * @covers \AtolOnline\Entities\Supplier::setPhones + * @covers \AtolOnline\Entities\Supplier::getPhones + * @throws InvalidPhoneException + */ + public function testNullablePhones(mixed $phones): void + { + $agent = new Supplier($phones); + $this->assertIsCollection($agent->getPhones()); + $this->assertTrue($agent->getPhones()->isEmpty()); + } + + /** + * Тестирует установку невалидных телефонов + * + * @covers \AtolOnline\Entities\Supplier + * @covers \AtolOnline\Entities\Supplier::setPhones + * @covers \AtolOnline\Exceptions\InvalidPhoneException + * @throws InvalidPhoneException + */ + public function testInvalidPhoneException(): void + { + $this->expectException(InvalidPhoneException::class); + (new Supplier())->setPhones([ + '12345678901234567', // good + '+123456789012345678', // good + '12345678901234567890', // bad + '+12345678901234567890', // bad + ]); + } +}