Files
kupshop/bundles/KupShop/KupShopBundle/Tests/ArrayUtilTest.php
2025-08-02 16:30:27 +02:00

121 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace KupShop\KupShopBundle\Tests;
use KupShop\KupShopBundle\Util\ArrayUtil;
use PHPUnit\Framework\TestCase;
class ArrayUtilTest extends TestCase
{
public function provideArrays(): \Generator
{
yield [null, null, null];
yield [['foo'], 'foo', 'foo'];
yield [[null], null, null];
yield [[null, 0, 1, 2], 0, 2];
yield [[14, 3, 333, 4], 3, 333];
yield [[1], 1, 1];
yield [[-10, null], -10, -10];
yield [[-100, null, 100], -100, 100];
yield [[-10, -1, null], -10, -1];
}
/**
* @dataProvider provideArrays
*/
public function testMinMaxFunc($array, $min, $max): void
{
$this->assertSame(ArrayUtil::min($array), $min);
$this->assertSame(ArrayUtil::max($array), $max);
}
public function getIterableData(): array
{
return [
'name' => 'item',
'children' => [
0 => [
'name' => 'price',
'children' => [
0 => [
'type' => 'xs:decimal',
'name' => 'without_vat',
'documentation' => 'Cena produktu bez DPH. Je-li produkt ve slevě, tak po započtení slevy.',
],
1 => [
'type' => 'xs:decimal',
'name' => 'with_vat',
'documentation' => 'Koncová cena produktu s DPH (prodejní cena na shopu). Je-li produkt ve slevě, pak cena po započtení slevy.',
],
],
'type' => 'priceType',
],
1 => [
'name' => 'set',
'children' => [
0 => [
'type' => 'xs:string',
'name' => 'content',
'minOccurs' => '0',
],
],
'type' => 'setType',
'minOccurs' => '0',
],
2 => [
'name' => 'product',
'children' => [
0 => [
'type' => 'xs:string',
'name' => 'title',
'documentation' => 'Název produktu z eshopu (co je uvedeno v H1).',
],
],
'type' => 'productType',
],
3 => [
'name' => 'brand',
'children' => [
0 => [
'type' => 'xs:string',
'name' => 'title',
'documentation' => 'Název značky, jak je na webu včetně spec zanků a kapitalizace.',
],
2 => [
'type' => 'xs:string',
'name' => 'url',
'documentation' => 'url značky',
],
],
'type' => 'brandType',
],
4 => [
'name' => 'serie',
'children' => [
0 => [
'type' => 'xs:string',
'name' => 'title',
'documentation' => 'Název modelové řady, jak je na webu včetně spec zanků a kapitalizace.',
],
],
'type' => 'serieType',
],
],
];
}
public function testIterableChunkSingleElementInChunk(): void
{
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->getIterableData()));
$chunks = iterator_to_array(ArrayUtil::iterableChunk($iterator, 3));
$this->assertTrue(count(array_pop($chunks)) <= 3);
foreach ($chunks as $chunk) {
$this->assertCount(3, $chunk);
}
}
}