|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Setono\SyliusTierPricingPlugin\Tests\Form\Type; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Prophecy\PhpUnit\ProphecyTrait; |
| 9 | +use Setono\SyliusTierPricingPlugin\Form\Type\PriceTierCollectionType; |
| 10 | +use Setono\SyliusTierPricingPlugin\Form\Type\PriceTierType; |
| 11 | +use Setono\SyliusTierPricingPlugin\Model\PriceTier; |
| 12 | +use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType; |
| 13 | +use Sylius\Bundle\ProductBundle\Form\Type\ProductVariantChoiceType; |
| 14 | +use Sylius\Component\Core\Model\Channel; |
| 15 | +use Sylius\Component\Core\Model\ChannelInterface; |
| 16 | +use Sylius\Resource\Doctrine\Persistence\RepositoryInterface; |
| 17 | +use Symfony\Component\Form\FormView; |
| 18 | +use Symfony\Component\Form\Test\TypeTestCase; |
| 19 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 20 | +use Symfony\UX\LiveComponent\Form\Type\LiveCollectionType; |
| 21 | + |
| 22 | +final class PriceTierCollectionTypeTest extends TypeTestCase |
| 23 | +{ |
| 24 | + use ProphecyTrait; |
| 25 | + |
| 26 | + private Channel $channel; |
| 27 | + |
| 28 | + protected function setUp(): void |
| 29 | + { |
| 30 | + $this->channel = $this->channel('WEB'); |
| 31 | + |
| 32 | + parent::setUp(); |
| 33 | + } |
| 34 | + |
| 35 | + // ----------------------------------------------------------------------- |
| 36 | + // configureOptions() — option defaults |
| 37 | + // ----------------------------------------------------------------------- |
| 38 | + |
| 39 | + #[Test] |
| 40 | + public function it_extends_live_collection_type_so_add_and_delete_fire_server_side_via_live_components(): void |
| 41 | + { |
| 42 | + self::assertSame(LiveCollectionType::class, (new PriceTierCollectionType())->getParent()); |
| 43 | + } |
| 44 | + |
| 45 | + #[Test] |
| 46 | + public function it_uses_price_tier_type_as_the_entry_type_with_a_blank_per_row_label(): void |
| 47 | + { |
| 48 | + $options = $this->resolveOptions(); |
| 49 | + |
| 50 | + self::assertSame(PriceTierType::class, $options['entry_type']); |
| 51 | + self::assertSame(['label' => false], $options['entry_options']); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + public function it_allows_adding_and_deleting_rows(): void |
| 56 | + { |
| 57 | + $options = $this->resolveOptions(); |
| 58 | + |
| 59 | + self::assertTrue($options['allow_add']); |
| 60 | + self::assertTrue($options['allow_delete']); |
| 61 | + } |
| 62 | + |
| 63 | + #[Test] |
| 64 | + public function it_disables_by_reference_so_the_owning_side_of_the_relation_is_updated_on_save(): void |
| 65 | + { |
| 66 | + self::assertFalse($this->resolveOptions()['by_reference']); |
| 67 | + } |
| 68 | + |
| 69 | + #[Test] |
| 70 | + public function it_pins_block_name_to_entry_matching_the_sylius_image_collection_convention(): void |
| 71 | + { |
| 72 | + self::assertSame('entry', $this->resolveOptions()['block_name']); |
| 73 | + } |
| 74 | + |
| 75 | + #[Test] |
| 76 | + public function it_labels_the_add_button_with_the_plugin_translation_key(): void |
| 77 | + { |
| 78 | + self::assertSame( |
| 79 | + ['label' => 'setono_sylius_tier_pricing.ui.add_price_tier'], |
| 80 | + $this->resolveOptions()['button_add_options'], |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #[Test] |
| 85 | + public function it_exposes_a_predictable_block_prefix_for_template_overrides(): void |
| 86 | + { |
| 87 | + self::assertSame( |
| 88 | + 'setono_sylius_tier_pricing_price_tier_collection', |
| 89 | + (new PriceTierCollectionType())->getBlockPrefix(), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // ----------------------------------------------------------------------- |
| 94 | + // Live decoration — the contract that makes Add/Delete fire over the wire. |
| 95 | + // Mirrors symfony/ux-live-component's own LiveCollectionTypeTest. |
| 96 | + // ----------------------------------------------------------------------- |
| 97 | + |
| 98 | + #[Test] |
| 99 | + public function the_add_button_view_carries_the_live_collection_button_add_block_prefix(): void |
| 100 | + { |
| 101 | + $blockPrefixes = $this->buttonAddView()->vars['block_prefixes']; |
| 102 | + assert(is_array($blockPrefixes)); |
| 103 | + |
| 104 | + self::assertContains('live_collection_button_add', $blockPrefixes); |
| 105 | + } |
| 106 | + |
| 107 | + #[Test] |
| 108 | + public function the_add_button_view_emits_the_live_action_attributes_for_addCollectionItem(): void |
| 109 | + { |
| 110 | + $attr = $this->buttonAddView()->vars['attr']; |
| 111 | + assert(is_array($attr)); |
| 112 | + |
| 113 | + self::assertSame('live#action', $attr['data-action']); |
| 114 | + self::assertSame('addCollectionItem', $attr['data-live-action-param']); |
| 115 | + self::assertSame('price_tiers', $attr['data-live-name-param']); |
| 116 | + } |
| 117 | + |
| 118 | + #[Test] |
| 119 | + public function each_entry_view_carries_a_delete_button_with_the_live_collection_button_delete_block_prefix(): void |
| 120 | + { |
| 121 | + $view = $this->factory |
| 122 | + ->createNamed('price_tiers', PriceTierCollectionType::class, [new PriceTier()]) |
| 123 | + ->createView(); |
| 124 | + self::assertCount(1, $view); |
| 125 | + |
| 126 | + $buttonDelete = $view[0]->vars['button_delete']; |
| 127 | + assert($buttonDelete instanceof FormView); |
| 128 | + $blockPrefixes = $buttonDelete->vars['block_prefixes']; |
| 129 | + assert(is_array($blockPrefixes)); |
| 130 | + |
| 131 | + self::assertContains('live_collection_button_delete', $blockPrefixes); |
| 132 | + } |
| 133 | + |
| 134 | + #[Test] |
| 135 | + public function the_add_button_label_resolves_to_the_plugin_translation_key(): void |
| 136 | + { |
| 137 | + self::assertSame( |
| 138 | + 'setono_sylius_tier_pricing.ui.add_price_tier', |
| 139 | + $this->buttonAddView()->vars['label'], |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + // ----------------------------------------------------------------------- |
| 144 | + // Data binding — submit a list of entries, verify each becomes a PriceTier. |
| 145 | + // ----------------------------------------------------------------------- |
| 146 | + |
| 147 | + #[Test] |
| 148 | + public function submitting_an_array_of_payloads_produces_one_price_tier_per_entry(): void |
| 149 | + { |
| 150 | + $form = $this->factory->createNamed('price_tiers', PriceTierCollectionType::class); |
| 151 | + $form->submit([ |
| 152 | + ['quantity' => '5', 'discount' => '10', 'channel' => 'WEB'], |
| 153 | + ['quantity' => '10', 'discount' => '20', 'channel' => ''], |
| 154 | + ]); |
| 155 | + |
| 156 | + self::assertTrue($form->isSynchronized()); |
| 157 | + |
| 158 | + /** @var list<PriceTier> $tiers */ |
| 159 | + $tiers = $form->getData(); |
| 160 | + self::assertCount(2, $tiers); |
| 161 | + self::assertContainsOnlyInstancesOf(PriceTier::class, $tiers); |
| 162 | + self::assertSame(5, $tiers[0]->getQuantity()); |
| 163 | + self::assertSame($this->channel, $tiers[0]->getChannel()); |
| 164 | + self::assertSame(10, $tiers[1]->getQuantity()); |
| 165 | + self::assertNull($tiers[1]->getChannel()); |
| 166 | + } |
| 167 | + |
| 168 | + protected function getTypes(): array |
| 169 | + { |
| 170 | + return [ |
| 171 | + new PriceTierType(PriceTier::class, ['setono_sylius_tier_pricing']), |
| 172 | + new ChannelChoiceType($this->channelRepository()), |
| 173 | + new ProductVariantChoiceType(), |
| 174 | + ]; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @return array<string, mixed> |
| 179 | + */ |
| 180 | + private function resolveOptions(): array |
| 181 | + { |
| 182 | + $resolver = new OptionsResolver(); |
| 183 | + (new PriceTierCollectionType())->configureOptions($resolver); |
| 184 | + |
| 185 | + /** @var array<string, mixed> $resolved */ |
| 186 | + $resolved = $resolver->resolve(); |
| 187 | + |
| 188 | + return $resolved; |
| 189 | + } |
| 190 | + |
| 191 | + private function buttonAddView(): FormView |
| 192 | + { |
| 193 | + $view = $this->factory |
| 194 | + ->createNamed('price_tiers', PriceTierCollectionType::class, []) |
| 195 | + ->createView(); |
| 196 | + $buttonAdd = $view->vars['button_add']; |
| 197 | + assert($buttonAdd instanceof FormView); |
| 198 | + |
| 199 | + return $buttonAdd; |
| 200 | + } |
| 201 | + |
| 202 | + /** @return RepositoryInterface<ChannelInterface> */ |
| 203 | + private function channelRepository(): RepositoryInterface |
| 204 | + { |
| 205 | + $repository = $this->prophesize(RepositoryInterface::class); |
| 206 | + $repository->findAll()->willReturn([$this->channel]); |
| 207 | + |
| 208 | + return $repository->reveal(); |
| 209 | + } |
| 210 | + |
| 211 | + private function channel(string $code): Channel |
| 212 | + { |
| 213 | + $channel = new Channel(); |
| 214 | + $channel->setCode($code); |
| 215 | + $channel->setName($code); |
| 216 | + |
| 217 | + return $channel; |
| 218 | + } |
| 219 | +} |
0 commit comments