|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\DataPersister\ContentNodes; |
| 4 | + |
| 5 | +use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface; |
| 6 | +use App\DataPersister\ContentNode\MultiSelectDataPersister; |
| 7 | +use App\Entity\ContentNode\ColumnLayout; |
| 8 | +use App\Entity\ContentNode\MultiSelect; |
| 9 | +use App\Entity\ContentNode\MultiSelectOption; |
| 10 | +use App\Entity\ContentType; |
| 11 | +use PHPUnit\Framework\MockObject\MockObject; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | + |
| 14 | +/** |
| 15 | + * @internal |
| 16 | + */ |
| 17 | +class MultiSelectDataPersisterTest extends TestCase { |
| 18 | + private MultiSelectDataPersister $dataPersister; |
| 19 | + private MockObject|ContextAwareDataPersisterInterface $decoratedMock; |
| 20 | + private MultiSelect $contentNode; |
| 21 | + |
| 22 | + protected function setUp(): void { |
| 23 | + $this->decoratedMock = $this->createMock(ContextAwareDataPersisterInterface::class); |
| 24 | + $this->contentNode = new MultiSelect(); |
| 25 | + |
| 26 | + $this->root = $this->createMock(ColumnLayout::class); |
| 27 | + $this->contentNode->parent = new ColumnLayout(); |
| 28 | + $this->contentNode->parent->root = $this->root; |
| 29 | + |
| 30 | + $contentType = new ContentType(); |
| 31 | + $contentType->jsonConfig = [ |
| 32 | + 'items' => [ |
| 33 | + 'key1', |
| 34 | + 'key2', |
| 35 | + ], |
| 36 | + ]; |
| 37 | + $this->contentNode->contentType = $contentType; |
| 38 | + |
| 39 | + $this->dataPersister = new MultiSelectDataPersister($this->decoratedMock); |
| 40 | + } |
| 41 | + |
| 42 | + public function testDelegatesSupportCheckToDecorated() { |
| 43 | + $this->decoratedMock |
| 44 | + ->expects($this->exactly(2)) |
| 45 | + ->method('supports') |
| 46 | + ->willReturnOnConsecutiveCalls(true, false) |
| 47 | + ; |
| 48 | + |
| 49 | + $this->assertTrue($this->dataPersister->supports($this->contentNode, [])); |
| 50 | + $this->assertFalse($this->dataPersister->supports($this->contentNode, [])); |
| 51 | + } |
| 52 | + |
| 53 | + public function testDoesNotSupportNonMultiSelect() { |
| 54 | + $this->decoratedMock |
| 55 | + ->method('supports') |
| 56 | + ->willReturn(true) |
| 57 | + ; |
| 58 | + |
| 59 | + $this->assertFalse($this->dataPersister->supports([], [])); |
| 60 | + } |
| 61 | + |
| 62 | + public function testDelegatesPersistToDecorated() { |
| 63 | + // given |
| 64 | + $this->decoratedMock->expects($this->once()) |
| 65 | + ->method('persist') |
| 66 | + ; |
| 67 | + |
| 68 | + // when |
| 69 | + $this->dataPersister->persist($this->contentNode, []); |
| 70 | + |
| 71 | + // then |
| 72 | + } |
| 73 | + |
| 74 | + public function testSetsRootFromParentOnCreate() { |
| 75 | + // given |
| 76 | + $this->decoratedMock->expects($this->once())->method('persist')->willReturnArgument(0); |
| 77 | + |
| 78 | + // when |
| 79 | + /** @var MultiSelect $data */ |
| 80 | + $data = $this->dataPersister->persist($this->contentNode, ['collection_operation_name' => 'post']); |
| 81 | + |
| 82 | + // then |
| 83 | + $this->assertEquals($this->root, $data->root); |
| 84 | + } |
| 85 | + |
| 86 | + public function testCopyMultiSelectOptionsFromContentTypeOnCreate() { |
| 87 | + // given |
| 88 | + $this->decoratedMock->expects($this->once())->method('persist')->willReturnArgument(0); |
| 89 | + |
| 90 | + // when |
| 91 | + /** @var MultiSelect $data */ |
| 92 | + $data = $this->dataPersister->persist($this->contentNode, ['collection_operation_name' => 'post']); |
| 93 | + |
| 94 | + // then |
| 95 | + $this->assertEquals($data->options[0]->translateKey, 'key1'); |
| 96 | + $this->assertEquals($data->options[0]->checked, false); |
| 97 | + $this->assertEquals($data->options[0]->getPos(), 0); |
| 98 | + |
| 99 | + $this->assertEquals($data->options[1]->translateKey, 'key2'); |
| 100 | + $this->assertEquals($data->options[1]->checked, false); |
| 101 | + $this->assertEquals($data->options[1]->getPos(), 1); |
| 102 | + } |
| 103 | + |
| 104 | + public function testCopyMultiSelectOptionsFromPrototypeOnCreate() { |
| 105 | + // given |
| 106 | + $this->addPrototype(); |
| 107 | + $this->decoratedMock->expects($this->once())->method('persist')->willReturnArgument(0); |
| 108 | + |
| 109 | + // when |
| 110 | + /** @var MultiSelect $data */ |
| 111 | + $data = $this->dataPersister->persist($this->contentNode, ['collection_operation_name' => 'post']); |
| 112 | + |
| 113 | + // then |
| 114 | + $this->assertEquals($data->options[0]->translateKey, $this->contentNode->prototype->options[0]->translateKey); |
| 115 | + $this->assertEquals($data->options[0]->checked, $this->contentNode->prototype->options[0]->checked); |
| 116 | + $this->assertEquals($data->options[0]->getPos(), $this->contentNode->prototype->options[0]->getPos()); |
| 117 | + } |
| 118 | + |
| 119 | + public function testDoesNotSetRootFromParentOnUpdate() { |
| 120 | + // given |
| 121 | + $this->decoratedMock->expects($this->once())->method('persist')->willReturnArgument(0); |
| 122 | + |
| 123 | + // when |
| 124 | + /** @var MultiSelect $data */ |
| 125 | + $data = $this->dataPersister->persist($this->contentNode, ['item_operation_name' => 'patch']); |
| 126 | + |
| 127 | + // then |
| 128 | + $this->assertNotEquals($this->root, $data->root); |
| 129 | + } |
| 130 | + |
| 131 | + public function testDoesNotCopyMultiSelectOptionsOnUpdate() { |
| 132 | + // given |
| 133 | + $this->decoratedMock->expects($this->once())->method('persist')->willReturnArgument(0); |
| 134 | + |
| 135 | + // when |
| 136 | + /** @var MultiSelect $data */ |
| 137 | + $data = $this->dataPersister->persist($this->contentNode, ['item_operation_name' => 'patch']); |
| 138 | + |
| 139 | + // then |
| 140 | + $this->assertEquals(count($data->options), 0); |
| 141 | + } |
| 142 | + |
| 143 | + private function addPrototype() { |
| 144 | + $prototype = new MultiSelect(); |
| 145 | + |
| 146 | + $option = new MultiSelectOption(); |
| 147 | + $option->translateKey = 'translateKey'; |
| 148 | + $option->checked = true; |
| 149 | + $option->setPos(51); |
| 150 | + |
| 151 | + $prototype->addOption($option); |
| 152 | + |
| 153 | + $this->contentNode->prototype = $prototype; |
| 154 | + } |
| 155 | +} |
0 commit comments