|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DataKit\DataViews\Tests\DataView; |
| 4 | + |
| 5 | +use DataKit\DataViews\DataView\Action; |
| 6 | +use DataKit\DataViews\DataView\Actions; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | + |
| 9 | +/** |
| 10 | + * Unit tests for {@see Actions} |
| 11 | + * |
| 12 | + * @since $ver$ |
| 13 | + */ |
| 14 | +final class ActionsTest extends TestCase { |
| 15 | + /** |
| 16 | + * Test case for {@see Actions::of()} and {@see Actions::to_array()}. |
| 17 | + * |
| 18 | + * @since $ver$ |
| 19 | + */ |
| 20 | + public function testOf(): void { |
| 21 | + $actions = Actions::of( |
| 22 | + $action_1 = Action::url( 'action1', 'Action 1', 'https://some-url.test' )->primary( 'one' ), |
| 23 | + $action_2 = Action::url( 'action2', 'Action 2', 'https://some-url.test' ), |
| 24 | + ); |
| 25 | + |
| 26 | + self::assertCount( 2, $actions ); |
| 27 | + self::assertSame( |
| 28 | + [ 'action1' => $action_1->to_array(), 'action2' => $action_2->to_array() ], |
| 29 | + $actions->to_array() |
| 30 | + ); |
| 31 | + |
| 32 | + $this->expectException( \InvalidArgumentException::class ); |
| 33 | + Actions::of(); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Test case for {@see Actions::getIterator()}. |
| 38 | + * |
| 39 | + * @since $ver$ |
| 40 | + */ |
| 41 | + public function testGetIterator(): void { |
| 42 | + $actions = Actions::of( |
| 43 | + $action_1 = Action::url( 'action1', 'Action 1', 'https://some-url.test' )->primary( 'one' ), |
| 44 | + $action_2 = Action::url( 'action2', 'Action 2', 'https://some-url.test' ), |
| 45 | + ); |
| 46 | + |
| 47 | + $result = iterator_to_array( $actions ); |
| 48 | + |
| 49 | + self::assertSame( [ 'action1' => $action_1, 'action2' => $action_2 ], $result ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test case for {@see Actions::append()} and {@see Actions::prepend()}. |
| 54 | + * |
| 55 | + * @since $ver$ |
| 56 | + */ |
| 57 | + public function testAppendPrepend(): void { |
| 58 | + $actions = Actions::of( |
| 59 | + Action::url( 'action1', 'Action 1', 'https://some-url.test' )->primary( 'one' ), |
| 60 | + Action::url( 'action2', 'Action 2', 'https://some-url.test' ), |
| 61 | + ); |
| 62 | + |
| 63 | + $append = $actions->append( |
| 64 | + Action::url( 'action3', 'Action 3', 'https://some-url.test' ), |
| 65 | + Action::url( 'action4', 'Action 4', 'https://some-url.test' ), |
| 66 | + ); |
| 67 | + |
| 68 | + $prepend = $actions->prepend( |
| 69 | + Action::url( 'action3', 'Action 3', 'https://some-url.test' ), |
| 70 | + Action::url( 'action4', 'Action 4', 'https://some-url.test' ), |
| 71 | + ); |
| 72 | + |
| 73 | + self::assertNotSame( $actions, $append ); |
| 74 | + self::assertNotSame( $actions, $prepend ); |
| 75 | + self::assertNotSame( $append, $prepend ); |
| 76 | + |
| 77 | + self::assertCount( 2, $actions ); |
| 78 | + self::assertCount( 4, $append ); |
| 79 | + self::assertCount( 4, $prepend ); |
| 80 | + |
| 81 | + self::assertSame( [ 'action3', 'action4', 'action1', 'action2' ], array_keys( $prepend->to_array() ) ); |
| 82 | + self::assertSame( [ 'action1', 'action2', 'action3', 'action4' ], array_keys( $append->to_array() ) ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Test case for {@see Actions::append()} and {@see Actions::prepend()} without any params. |
| 87 | + * |
| 88 | + * @since $ver$ |
| 89 | + * |
| 90 | + * @param bool $is_prepend Whether the method to be called is `prepend()`. |
| 91 | + * |
| 92 | + * @testWith [false, true] |
| 93 | + */ |
| 94 | + public function testAppendPrependException( bool $is_prepend ): void { |
| 95 | + $actions = Actions::of( |
| 96 | + Action::url( 'action1', 'Action 1', 'https://some-url.test' ), |
| 97 | + ); |
| 98 | + |
| 99 | + $this->expectException( \InvalidArgumentException::class ); |
| 100 | + |
| 101 | + $is_prepend |
| 102 | + ? $actions->prepend() |
| 103 | + : $actions->append(); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test case for {@see Actions::merge()}. |
| 108 | + * |
| 109 | + * @since $ver$ |
| 110 | + */ |
| 111 | + public function testMerge(): void { |
| 112 | + $actions = Actions::of( |
| 113 | + Action::url( 'action1', 'Action 1', 'https://some-url.test' ), |
| 114 | + Action::url( 'action2', 'Action 2', 'https://some-url.test' ), |
| 115 | + ); |
| 116 | + |
| 117 | + $additional = Actions::of( |
| 118 | + Action::url( 'action3', 'Action 3', 'https://some-url.test', ), |
| 119 | + Action::url( 'action1', 'Overwritten action 1', 'https://overwritten.test' ), |
| 120 | + Action::url( 'action4', 'Action 4', 'https://some-url.test' ), |
| 121 | + ); |
| 122 | + |
| 123 | + $merged = $actions->merge( $additional ); |
| 124 | + $merged_reversed = $additional->merge( $actions ); |
| 125 | + |
| 126 | + self::assertNotSame( $merged, $actions ); |
| 127 | + self::assertNotSame( $merged, $additional ); |
| 128 | + self::assertCount( 2, $actions ); |
| 129 | + self::assertCount( 3, $additional ); |
| 130 | + self::assertCount( 4, $merged ); // Because action 1 is overwritten. |
| 131 | + self::assertCount( 4, $merged_reversed ); |
| 132 | + |
| 133 | + self::assertSame( [ 'action1', 'action2', 'action3', 'action4' ], array_keys( $merged->to_array() ) ); |
| 134 | + self::assertSame( [ 'action3', 'action1', 'action4', 'action2' ], array_keys( $merged_reversed->to_array() ) ); |
| 135 | + |
| 136 | + self::assertSame( 'Overwritten action 1', $merged->to_array()['action1']['label'] ); |
| 137 | + self::assertSame( 'Action 1', $merged_reversed->to_array()['action1']['label'] ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Test case for {@see Actions::without())}. |
| 142 | + * |
| 143 | + * @since $ver$ |
| 144 | + */ |
| 145 | + public function testWithout(): void { |
| 146 | + $actions = Actions::of( |
| 147 | + Action::url( 'action1', 'Action 1', 'https://overwritten.test' ), |
| 148 | + Action::url( 'action2', 'Action 2', 'https://some-url.test', ), |
| 149 | + Action::url( 'action3', 'Action 3', 'https://some-url.test', ), |
| 150 | + Action::url( 'action4', 'Action 4', 'https://some-url.test' ), |
| 151 | + ); |
| 152 | + |
| 153 | + $without = $actions->without( 'action2', 'action3' ); |
| 154 | + |
| 155 | + self::assertNotSame( $without, $actions ); |
| 156 | + self::assertCount( 2, $without ); |
| 157 | + self::assertSame( [ 'action1', 'action4' ], array_keys( $without->to_array() ) ); |
| 158 | + |
| 159 | + $this->expectException( \InvalidArgumentException::class ); |
| 160 | + $actions->without(); |
| 161 | + } |
| 162 | +} |
0 commit comments