|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TinyBlocks\Collection\Operations\Retrieve; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use TinyBlocks\Collection\Collection; |
| 9 | +use TinyBlocks\Collection\Models\CryptoCurrency; |
| 10 | + |
| 11 | +final class CollectionSliceOperationTest extends TestCase |
| 12 | +{ |
| 13 | + public function testSliceReturnsSubsetOfElements(): void |
| 14 | + { |
| 15 | + /** @Given a collection of CryptoCurrency objects */ |
| 16 | + $elements = [ |
| 17 | + new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'), |
| 18 | + new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'), |
| 19 | + new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB'), |
| 20 | + new CryptoCurrency(name: 'Cardano', price: 2.0, symbol: 'ADA') |
| 21 | + ]; |
| 22 | + $collection = Collection::createFrom(elements: $elements); |
| 23 | + |
| 24 | + /** @When slicing the collection */ |
| 25 | + $actual = $collection->slice(index: 1, length: 2); |
| 26 | + |
| 27 | + /** @Then the result should contain the sliced elements */ |
| 28 | + self::assertSame([ |
| 29 | + 1 => $elements[1]->toArray(), |
| 30 | + 2 => $elements[2]->toArray() |
| 31 | + ], $actual->toArray()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testSliceReturnsEmptyWhenIndexExceedsCollectionSize(): void |
| 35 | + { |
| 36 | + /** @Given a collection of CryptoCurrency objects */ |
| 37 | + $elements = [ |
| 38 | + new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'), |
| 39 | + new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'), |
| 40 | + new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB') |
| 41 | + ]; |
| 42 | + $collection = Collection::createFrom(elements: $elements); |
| 43 | + |
| 44 | + /** @When slicing the collection with an index that exceeds the collection size */ |
| 45 | + $actual = $collection->slice(index: 5, length: 2); |
| 46 | + |
| 47 | + /** @Then the result should be an empty array */ |
| 48 | + self::assertEmpty($actual->toArray()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testSliceWithZeroLengthReturnsEmpty(): void |
| 52 | + { |
| 53 | + /** @Given a collection of CryptoCurrency objects */ |
| 54 | + $elements = [ |
| 55 | + new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'), |
| 56 | + new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'), |
| 57 | + new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB') |
| 58 | + ]; |
| 59 | + $collection = Collection::createFrom(elements: $elements); |
| 60 | + |
| 61 | + /** @When slicing with length 0 */ |
| 62 | + $actual = $collection->slice(index: 1, length: 0); |
| 63 | + |
| 64 | + /** @Then the result should be an empty array */ |
| 65 | + self::assertEmpty($actual->toArray()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testSliceWithLengthOne(): void |
| 69 | + { |
| 70 | + /** @Given a collection of CryptoCurrency objects */ |
| 71 | + $elements = [ |
| 72 | + new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'), |
| 73 | + new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'), |
| 74 | + new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB') |
| 75 | + ]; |
| 76 | + $collection = Collection::createFrom(elements: $elements); |
| 77 | + |
| 78 | + /** @When slicing with length 1 */ |
| 79 | + $actual = $collection->slice(index: 1, length: 1); |
| 80 | + |
| 81 | + /** @Then the result should contain only one element */ |
| 82 | + self::assertSame([1 => $elements[1]->toArray()], $actual->toArray()); |
| 83 | + } |
| 84 | + |
| 85 | + public function testSliceWithNegativeTwoLength(): void |
| 86 | + { |
| 87 | + /** @Given a collection of CryptoCurrency objects */ |
| 88 | + $elements = [ |
| 89 | + new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'), |
| 90 | + new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'), |
| 91 | + new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB'), |
| 92 | + new CryptoCurrency(name: 'Cardano', price: 2.0, symbol: 'ADA') |
| 93 | + ]; |
| 94 | + $collection = Collection::createFrom(elements: $elements); |
| 95 | + |
| 96 | + /** @When slicing with length -2 */ |
| 97 | + $actual = $collection->slice(index: 1, length: -2); |
| 98 | + |
| 99 | + /** @Then the result should contain only the first element after the index */ |
| 100 | + self::assertSame([1 => $elements[1]->toArray()], $actual->toArray()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testSliceWithoutPassingLength(): void |
| 104 | + { |
| 105 | + /** @Given a collection of CryptoCurrency objects */ |
| 106 | + $elements = [ |
| 107 | + new CryptoCurrency(name: 'Bitcoin', price: 60000.0, symbol: 'BTC'), |
| 108 | + new CryptoCurrency(name: 'Ethereum', price: 40000.0, symbol: 'ETH'), |
| 109 | + new CryptoCurrency(name: 'Binance Coin', price: 1500.0, symbol: 'BNB'), |
| 110 | + new CryptoCurrency(name: 'Cardano', price: 2.0, symbol: 'ADA') |
| 111 | + ]; |
| 112 | + $collection = Collection::createFrom(elements: $elements); |
| 113 | + |
| 114 | + /** @When slicing without a passing length (defaults to -1) */ |
| 115 | + $actual = $collection->slice(index: 1); |
| 116 | + |
| 117 | + /** @Then the result should contain all elements starting from the index */ |
| 118 | + self::assertSame([ |
| 119 | + 1 => $elements[1]->toArray(), |
| 120 | + 2 => $elements[2]->toArray(), |
| 121 | + 3 => $elements[3]->toArray() |
| 122 | + ], $actual->toArray()); |
| 123 | + } |
| 124 | +} |
0 commit comments