Небольшой рефакторинг по тестам

- `BasicTestCase::assertAtolable() => assertIsAtolable()`
- генерация тестовых объектов `Vat`, `Payment` и `Item` вынесены в `BasicTestCase`
This commit is contained in:
2021-12-07 20:04:03 +08:00
parent 1f3d5d2f3d
commit a34a6927d1
20 changed files with 220 additions and 168 deletions

View File

@@ -12,18 +12,16 @@ namespace AtolOnline\Tests\Collections;
use AtolOnline\{
Collections\Items,
Constants\Constraints,
Entities\Item,
Helpers,
Tests\BasicTestCase};
use AtolOnline\Exceptions\{
EmptyItemNameException,
InvalidEntityInCollectionException,
NegativeItemPriceException,
NegativeItemQuantityException,
TooHighItemPriceException,
TooLongItemNameException,
TooManyException,
TooManyItemsException,};
use Exception;
TooManyItemsException};
/**
* Набор тестов для проверки работы класса коллекции предметов расчёта
@@ -42,11 +40,12 @@ class ItemsTest extends BasicTestCase
* @throws TooHighItemPriceException
* @throws TooLongItemNameException
* @throws TooManyException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyItemsExceptionByConstructor()
{
$this->expectException(TooManyItemsException::class);
new Items($this->generateObjects(Constraints::MAX_COUNT_DOC_ITEMS + 1));
new Items($this->generateItemObjects(Constraints::MAX_COUNT_DOC_ITEMS + 1));
}
/**
@@ -61,12 +60,13 @@ class ItemsTest extends BasicTestCase
* @throws TooHighItemPriceException
* @throws TooLongItemNameException
* @throws TooManyException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyItemsExceptionByPrepend()
{
$this->expectException(TooManyItemsException::class);
(new Items($this->generateObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->prepend($this->generateObjects());
(new Items($this->generateItemObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->prepend($this->generateItemObjects());
}
/**
@@ -82,12 +82,13 @@ class ItemsTest extends BasicTestCase
* @throws TooHighItemPriceException
* @throws TooLongItemNameException
* @throws TooManyException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyItemsExceptionByAdd()
{
$this->expectException(TooManyItemsException::class);
(new Items($this->generateObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->add($this->generateObjects());
(new Items($this->generateItemObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->add($this->generateItemObjects());
}
/**
@@ -103,12 +104,13 @@ class ItemsTest extends BasicTestCase
* @throws TooHighItemPriceException
* @throws TooLongItemNameException
* @throws TooManyException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyItemsExceptionByPush()
{
$this->expectException(TooManyItemsException::class);
(new Items($this->generateObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->push(...$this->generateObjects());
(new Items($this->generateItemObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->push(...$this->generateItemObjects());
}
/**
@@ -124,33 +126,12 @@ class ItemsTest extends BasicTestCase
* @throws TooHighItemPriceException
* @throws TooLongItemNameException
* @throws TooManyException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyItemsExceptionByMerge()
{
$this->expectException(TooManyItemsException::class);
(new Items($this->generateObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->merge($this->generateObjects(2));
}
/**
* Генерирует массив тестовых объектов предметов расчёта
*
* @param int $count
* @return Item[]
* @throws EmptyItemNameException
* @throws NegativeItemPriceException
* @throws NegativeItemQuantityException
* @throws TooHighItemPriceException
* @throws TooLongItemNameException
* @throws TooManyException
* @throws Exception
*/
protected function generateObjects(int $count = 1): array
{
$result = [];
for ($i = 0; $i < abs($count); ++$i) {
$result[] = new Item(Helpers::randomStr(), random_int(1, 100), random_int(1, 10));
}
return $result;
(new Items($this->generateItemObjects(Constraints::MAX_COUNT_DOC_ITEMS)))
->merge($this->generateItemObjects(2));
}
}

View File

@@ -12,14 +12,12 @@ namespace AtolOnline\Tests\Collections;
use AtolOnline\{
Collections\Payments,
Constants\Constraints,
Entities\Payment,
Enums\PaymentTypes,
Exceptions\InvalidEntityInCollectionException,
Exceptions\InvalidEnumValueException,
Exceptions\NegativePaymentSumException,
Exceptions\TooHighPaymentSumException,
Exceptions\TooManyPaymentsException,
Tests\BasicTestCase};
use Exception;
/**
* Набор тестов для проверки работы класса коллекции оплат
@@ -35,11 +33,12 @@ class PaymentsTest extends BasicTestCase
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyPaymentsExceptionByConstructor()
{
$this->expectException(TooManyPaymentsException::class);
new Payments($this->generateObjects(Constraints::MAX_COUNT_DOC_PAYMENTS + 1));
new Payments($this->generatePaymentObjects(Constraints::MAX_COUNT_DOC_PAYMENTS + 1));
}
/**
@@ -51,12 +50,13 @@ class PaymentsTest extends BasicTestCase
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyPaymentsExceptionByPrepend()
{
$this->expectException(TooManyPaymentsException::class);
(new Payments($this->generateObjects(Constraints::MAX_COUNT_DOC_PAYMENTS)))
->prepend($this->generateObjects());
(new Payments($this->generatePaymentObjects(Constraints::MAX_COUNT_DOC_PAYMENTS)))
->prepend($this->generatePaymentObjects());
}
/**
@@ -69,12 +69,13 @@ class PaymentsTest extends BasicTestCase
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyPaymentsExceptionByAdd()
{
$this->expectException(TooManyPaymentsException::class);
(new Payments($this->generateObjects(Constraints::MAX_COUNT_DOC_PAYMENTS)))
->add($this->generateObjects());
(new Payments($this->generatePaymentObjects(Constraints::MAX_COUNT_DOC_PAYMENTS)))
->add($this->generatePaymentObjects());
}
/**
@@ -87,12 +88,13 @@ class PaymentsTest extends BasicTestCase
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyPaymentsExceptionByPush()
{
$this->expectException(TooManyPaymentsException::class);
(new Payments($this->generateObjects(Constraints::MAX_COUNT_DOC_PAYMENTS + 1)))
->push(...$this->generateObjects());
(new Payments($this->generatePaymentObjects(Constraints::MAX_COUNT_DOC_PAYMENTS + 1)))
->push(...$this->generatePaymentObjects());
}
/**
@@ -105,34 +107,12 @@ class PaymentsTest extends BasicTestCase
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyPaymentsExceptionByMerge()
{
$this->expectException(TooManyPaymentsException::class);
(new Payments($this->generateObjects(Constraints::MAX_COUNT_DOC_PAYMENTS - 1)))
->merge($this->generateObjects(2));
}
/**
* Генерирует массив тестовых объектов оплаты
*
* @param int $count
* @return Payment[]
* @throws InvalidEnumValueException
* @throws NegativePaymentSumException
* @throws TooHighPaymentSumException
* @throws Exception
*/
protected function generateObjects(int $count = 1): array
{
$types = PaymentTypes::toArray();
$result = [];
for ($i = 0; $i < abs($count); ++$i) {
$result[] = new Payment(
array_values($types)[random_int(min($types), max($types))],
random_int(1, 100) * 2 / 3
);
}
return $result;
(new Payments($this->generatePaymentObjects(Constraints::MAX_COUNT_DOC_PAYMENTS - 1)))
->merge($this->generatePaymentObjects(2));
}
}

View File

@@ -13,9 +13,7 @@ use AtolOnline\{
Collections\Vats,
Constants\Constraints,
Entities\Payment,
Entities\Vat,
Enums\PaymentTypes,
Enums\VatTypes,
Exceptions\InvalidEntityInCollectionException,
Exceptions\InvalidEnumValueException,
Exceptions\NegativePaymentSumException,
@@ -39,10 +37,10 @@ class VatsTest extends BasicTestCase
*/
public function testConstructor()
{
$vats = new Vats($this->generateObjects(3));
$vats = new Vats($this->generateVatObjects(3));
$this->assertIsCollection($vats);
$this->assertEquals(3, $vats->count());
$this->assertAtolable($vats);
$this->assertIsAtolable($vats);
}
/**
@@ -52,11 +50,12 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @covers \AtolOnline\Exceptions\TooManyVatsException
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyVatsExceptionByConstructor()
{
$this->expectException(TooManyVatsException::class);
new Vats($this->generateObjects(Constraints::MAX_COUNT_DOC_VATS + 1));
new Vats($this->generateVatObjects(Constraints::MAX_COUNT_DOC_VATS + 1));
}
/**
@@ -66,11 +65,12 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::prepend
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testPrepend()
{
$vats = (new Vats($this->generateObjects(3)))
->prepend($this->generateObjects());
$vats = (new Vats($this->generateVatObjects(3)))
->prepend($this->generateVatObjects());
$this->assertEquals(4, $vats->count());
}
@@ -82,12 +82,13 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @covers \AtolOnline\Exceptions\TooManyVatsException
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyVatsExceptionByPrepend()
{
$this->expectException(TooManyVatsException::class);
(new Vats($this->generateObjects(Constraints::MAX_COUNT_DOC_VATS)))
->prepend($this->generateObjects());
(new Vats($this->generateVatObjects(Constraints::MAX_COUNT_DOC_VATS)))
->prepend($this->generateVatObjects());
}
/**
@@ -97,11 +98,12 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::add
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testAdd()
{
$vats = (new Vats($this->generateObjects(3)))
->add($this->generateObjects());
$vats = (new Vats($this->generateVatObjects(3)))
->add($this->generateVatObjects());
$this->assertEquals(4, $vats->count());
}
@@ -113,12 +115,13 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @covers \AtolOnline\Exceptions\TooManyVatsException
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyVatsExceptionByAdd()
{
$this->expectException(TooManyVatsException::class);
(new Vats($this->generateObjects(Constraints::MAX_COUNT_DOC_VATS)))
->add($this->generateObjects());
(new Vats($this->generateVatObjects(Constraints::MAX_COUNT_DOC_VATS)))
->add($this->generateVatObjects());
}
/**
@@ -128,11 +131,12 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::push
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testPush()
{
$vats = (new Vats($this->generateObjects(3)))
->push(...$this->generateObjects(3));
$vats = (new Vats($this->generateVatObjects(3)))
->push(...$this->generateVatObjects(3));
$this->assertEquals(6, $vats->count());
}
@@ -144,12 +148,13 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @covers \AtolOnline\Exceptions\TooManyVatsException
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyVatsExceptionByPush()
{
$this->expectException(TooManyVatsException::class);
(new Vats($this->generateObjects(Constraints::MAX_COUNT_DOC_VATS)))
->push(...$this->generateObjects());
(new Vats($this->generateVatObjects(Constraints::MAX_COUNT_DOC_VATS)))
->push(...$this->generateVatObjects());
}
/**
@@ -159,11 +164,12 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::merge
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testMerge()
{
$vats = (new Vats($this->generateObjects(3)))
->merge($this->generateObjects(3));
$vats = (new Vats($this->generateVatObjects(3)))
->merge($this->generateVatObjects(3));
$this->assertEquals(6, $vats->count());
}
@@ -175,19 +181,20 @@ class VatsTest extends BasicTestCase
* @covers \AtolOnline\Collections\EntityCollection::checkCount
* @covers \AtolOnline\Exceptions\TooManyVatsException
* @throws InvalidEnumValueException
* @throws InvalidEntityInCollectionException
*/
public function testTooManyVatsExceptionByMerge()
{
$this->expectException(TooManyVatsException::class);
(new Vats($this->generateObjects(Constraints::MAX_COUNT_DOC_VATS - 1)))
->merge($this->generateObjects(2));
(new Vats($this->generateVatObjects(Constraints::MAX_COUNT_DOC_VATS - 1)))
->merge($this->generateVatObjects(2));
}
/**
* Тестирует выброс исключения при наличии скаляров в коллекции
*
* @covers \AtolOnline\Collections\EntityCollection
* @covers \AtolOnline\Collections\EntityCollection::checkClass
* @covers \AtolOnline\Collections\EntityCollection::checkItemClass
* @covers \AtolOnline\Collections\EntityCollection::jsonSerialize
* @covers \AtolOnline\Exceptions\InvalidEntityInCollectionException
* @throws InvalidEnumValueException
@@ -197,7 +204,7 @@ class VatsTest extends BasicTestCase
{
$this->expectException(InvalidEntityInCollectionException::class);
$this->expectExceptionMessage("(string)'bad element'");
(new Vats($this->generateObjects(2)))
(new Vats($this->generateVatObjects(2)))
->merge('bad element')
->jsonSerialize();
}
@@ -214,29 +221,8 @@ class VatsTest extends BasicTestCase
{
$this->expectException(InvalidEntityInCollectionException::class);
$this->expectExceptionMessage(Payment::class);
(new Vats($this->generateObjects()))
(new Vats($this->generateVatObjects()))
->merge([new Payment(PaymentTypes::PREPAID, 1)])
->jsonSerialize();
}
/**
* Генерирует массив тестовых объектов ставок НДС
*
* @param int $count
* @return Vat[]
* @throws InvalidEnumValueException
* @throws Exception
*/
protected function generateObjects(int $count = 1): array
{
$types = VatTypes::toArray();
$result = [];
for ($i = 0; $i < abs($count); ++$i) {
$result[] = new Vat(
array_values($types)[random_int(0, count($types) - 1)],
random_int(1, 100) * 2 / 3
);
}
return $result;
}
}