|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Vyuldashev\LaravelOpenApi\Tests\Builders; |
| 6 | + |
| 7 | +use Vyuldashev\LaravelOpenApi\Builders\InfoBuilder; |
| 8 | +use Vyuldashev\LaravelOpenApi\Tests\TestCase; |
| 9 | + |
| 10 | +class InfoBuilderTest extends TestCase |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @dataProvider providerBuildContact |
| 14 | + * @param array $config |
| 15 | + * @param array $expected |
| 16 | + * @return void |
| 17 | + */ |
| 18 | + public function testBuildContact(array $config, array $expected): void |
| 19 | + { |
| 20 | + $SUT = new InfoBuilder(); |
| 21 | + $info = $SUT->build($config); |
| 22 | + $this->assertSameAssociativeArray($expected, $info->toArray()); |
| 23 | + } |
| 24 | + |
| 25 | + public function providerBuildContact(): array |
| 26 | + { |
| 27 | + $common = [ |
| 28 | + 'title' => 'sample_title', |
| 29 | + 'description' => 'sample_description', |
| 30 | + 'version' => 'sample_version', |
| 31 | + ]; |
| 32 | + |
| 33 | + return [ |
| 34 | + 'If all the elements are present, the correct json can be output.' => [ |
| 35 | + array_merge($common, [ |
| 36 | + 'contact' => [ |
| 37 | + 'name' => 'sample_contact_name', |
| 38 | + 'email' => 'sample_contact_email', |
| 39 | + 'url' => 'sample_contact_url', |
| 40 | + ], |
| 41 | + ]), |
| 42 | + array_merge($common, [ |
| 43 | + 'contact' => [ |
| 44 | + 'name' => 'sample_contact_name', |
| 45 | + 'email' => 'sample_contact_email', |
| 46 | + 'url' => 'sample_contact_url', |
| 47 | + ], |
| 48 | + ]), |
| 49 | + ], |
| 50 | + 'If Contact.name does not exist, the correct json can be output.' => [ |
| 51 | + array_merge($common, [ |
| 52 | + 'contact' => [ |
| 53 | + 'email' => 'sample_contact_email', |
| 54 | + 'url' => 'sample_contact_url', |
| 55 | + ], |
| 56 | + ]), |
| 57 | + array_merge($common, [ |
| 58 | + 'contact' => [ |
| 59 | + 'email' => 'sample_contact_email', |
| 60 | + 'url' => 'sample_contact_url', |
| 61 | + ], |
| 62 | + ]), |
| 63 | + ], |
| 64 | + 'If Contact.email does not exist, the correct json can be output.' => [ |
| 65 | + array_merge($common, [ |
| 66 | + 'contact' => [ |
| 67 | + 'name' => 'sample_contact_name', |
| 68 | + 'url' => 'sample_contact_url', |
| 69 | + ], |
| 70 | + ]), |
| 71 | + array_merge($common, [ |
| 72 | + 'contact' => [ |
| 73 | + 'name' => 'sample_contact_name', |
| 74 | + 'url' => 'sample_contact_url', |
| 75 | + ], |
| 76 | + ]), |
| 77 | + ], |
| 78 | + 'If Contact.url does not exist, the correct json can be output.' => [ |
| 79 | + array_merge($common, [ |
| 80 | + 'contact' => [ |
| 81 | + 'name' => 'sample_contact_name', |
| 82 | + 'email' => 'sample_contact_email', |
| 83 | + ], |
| 84 | + ]), |
| 85 | + array_merge($common, [ |
| 86 | + 'contact' => [ |
| 87 | + 'name' => 'sample_contact_name', |
| 88 | + 'email' => 'sample_contact_email', |
| 89 | + ], |
| 90 | + ]), |
| 91 | + ], |
| 92 | + 'If Contact does not exist, the correct json can be output.' => [ |
| 93 | + array_merge($common), |
| 94 | + array_merge($common), |
| 95 | + ], |
| 96 | + 'If Contact.* does not exist, the correct json can be output.' => [ |
| 97 | + array_merge($common, [ |
| 98 | + 'contact' => [], |
| 99 | + ]), |
| 100 | + array_merge($common), |
| 101 | + ], |
| 102 | + ]; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Assert equality as an associative array. |
| 107 | + * |
| 108 | + * @param array $expected |
| 109 | + * @param array $actual |
| 110 | + * @return void |
| 111 | + */ |
| 112 | + protected function assertSameAssociativeArray(array $expected, array $actual): void |
| 113 | + { |
| 114 | + foreach ($expected as $key => $value) { |
| 115 | + if (is_array($value)) { |
| 116 | + $this->assertSameAssociativeArray($value, $actual[$key]); |
| 117 | + unset($actual[$key]); |
| 118 | + continue; |
| 119 | + } |
| 120 | + self::assertSame($value, $actual[$key]); |
| 121 | + unset($actual[$key]); |
| 122 | + } |
| 123 | + self::assertCount(0, $actual, sprintf('[%s] does not matched keys.', join(', ', array_keys($actual)))); |
| 124 | + } |
| 125 | +} |
0 commit comments