1 | <?php |
||
29 | final class ShowBuilderTest extends TestCase |
||
30 | { |
||
31 | /** |
||
32 | * @var Stub&TypeGuesserInterface |
||
33 | */ |
||
34 | private $guesser; |
||
35 | |||
36 | /** |
||
37 | * @var ShowBuilder |
||
38 | */ |
||
39 | private $showBuilder; |
||
40 | |||
41 | /** |
||
42 | * @var MockObject&AdminInterface |
||
43 | */ |
||
44 | private $admin; |
||
45 | |||
46 | /** |
||
47 | * @var Stub&ModelManager |
||
48 | */ |
||
49 | private $modelManager; |
||
50 | |||
51 | protected function setUp(): void |
||
52 | { |
||
53 | $this->guesser = $this->createStub(TypeGuesserInterface::class); |
||
54 | |||
55 | $this->showBuilder = new ShowBuilder( |
||
56 | $this->guesser, |
||
57 | [ |
||
58 | 'fakeTemplate' => 'fake', |
||
59 | TemplateRegistry::TYPE_MANY_TO_ONE => '@SonataAdmin/CRUD/Association/show_many_to_one.html.twig', |
||
60 | ] |
||
61 | ); |
||
62 | |||
63 | $this->admin = $this->createMock(AdminInterface::class); |
||
64 | $this->modelManager = $this->createStub(ModelManager::class); |
||
65 | |||
66 | $this->admin->method('getClass')->willReturn('FakeClass'); |
||
67 | $this->admin->method('getModelManager')->willReturn($this->modelManager); |
||
68 | } |
||
69 | |||
70 | public function testGetBaseList(): void |
||
71 | { |
||
72 | $this->assertInstanceOf(FieldDescriptionCollection::class, $this->showBuilder->getBaseList()); |
||
73 | } |
||
74 | |||
75 | public function testAddFieldNoType(): void |
||
76 | { |
||
77 | $typeGuess = $this->createStub(TypeGuess::class); |
||
78 | |||
79 | $fieldDescription = new FieldDescription(); |
||
80 | $fieldDescription->setName('FakeName'); |
||
81 | $fieldDescription->setMappingType(ClassMetadata::ONE); |
||
82 | |||
83 | $this->admin->expects($this->once())->method('attachAdminClass'); |
||
84 | $this->admin->expects($this->once())->method('addShowFieldDescription'); |
||
85 | |||
86 | $typeGuess->method('getType')->willReturn('fakeType'); |
||
87 | |||
88 | $this->guesser->method('guessType')->willReturn($typeGuess); |
||
89 | |||
90 | $this->modelManager->method('hasMetadata')->willReturn(false); |
||
91 | |||
92 | $this->showBuilder->addField( |
||
93 | new FieldDescriptionCollection(), |
||
94 | null, |
||
95 | $fieldDescription, |
||
96 | $this->admin |
||
97 | ); |
||
98 | |||
99 | $this->assertSame('fakeType', $fieldDescription->getType()); |
||
100 | } |
||
101 | |||
102 | public function testAddFieldWithType(): void |
||
103 | { |
||
104 | $fieldDescription = new FieldDescription(); |
||
105 | $fieldDescription->setName('FakeName'); |
||
106 | |||
107 | $this->admin->expects($this->once())->method('addShowFieldDescription'); |
||
108 | |||
109 | $this->modelManager->method('hasMetadata')->willReturn(false); |
||
110 | |||
111 | $this->showBuilder->addField( |
||
112 | new FieldDescriptionCollection(), |
||
113 | 'someType', |
||
114 | $fieldDescription, |
||
115 | $this->admin |
||
116 | ); |
||
117 | |||
118 | $this->assertSame('someType', $fieldDescription->getType()); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @dataProvider fixFieldDescriptionData |
||
123 | */ |
||
124 | public function testFixFieldDescription(string $type, string $mappingType, string $template): void |
||
148 | |||
149 | public function fixFieldDescriptionData(): iterable |
||
164 | |||
165 | /** |
||
166 | * @dataProvider fixFieldDescriptionTypes |
||
167 | */ |
||
168 | public function testFixFieldDescriptionFixesType(string $expectedType, string $type): void |
||
178 | |||
179 | public function fixFieldDescriptionTypes(): iterable |
||
186 | |||
187 | public function testFixFieldDescriptionException(): void |
||
192 | } |
||
193 |