|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Symfony\UX\Toolkit\Tests\Recipe; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Symfony\UX\Toolkit\Dependency\PhpPackageDependency; |
| 9 | +use Symfony\UX\Toolkit\Dependency\RecipeDependency; |
| 10 | +use Symfony\UX\Toolkit\Dependency\Version; |
| 11 | +use Symfony\UX\Toolkit\Recipe\RecipeManifest; |
| 12 | +use Symfony\UX\Toolkit\Recipe\RecipeType; |
| 13 | + |
| 14 | +final class RecipeManifestTest extends TestCase |
| 15 | +{ |
| 16 | + public function testFromJsonWithInvalidJson() |
| 17 | + { |
| 18 | + $this->expectException(\JsonException::class); |
| 19 | + $this->expectExceptionMessage('Syntax error'); |
| 20 | + |
| 21 | + RecipeManifest::fromJson('test'); |
| 22 | + } |
| 23 | + |
| 24 | + public function testFromJsonWithEmpty() |
| 25 | + { |
| 26 | + $this->expectException(\InvalidArgumentException::class); |
| 27 | + $this->expectExceptionMessage('Property "type" is required.'); |
| 28 | + |
| 29 | + RecipeManifest::fromJson('{}'); |
| 30 | + } |
| 31 | + |
| 32 | + public function testFromJsonWithInvalidType() |
| 33 | + { |
| 34 | + $this->expectException(\InvalidArgumentException::class); |
| 35 | + $this->expectExceptionMessage('The recipe type "test" is not supported.'); |
| 36 | + |
| 37 | + RecipeManifest::fromJson(<<<JSON |
| 38 | + { |
| 39 | + "type": "test" |
| 40 | + } |
| 41 | + JSON); |
| 42 | + } |
| 43 | + |
| 44 | + public function testFromJsonWithMissingName() |
| 45 | + { |
| 46 | + $this->expectException(\InvalidArgumentException::class); |
| 47 | + $this->expectExceptionMessage('Property "name" is required.'); |
| 48 | + |
| 49 | + RecipeManifest::fromJson(<<<JSON |
| 50 | + { |
| 51 | + "type": "component" |
| 52 | + } |
| 53 | + JSON); |
| 54 | + } |
| 55 | + |
| 56 | + public function testFromJsonWithMissingDescription() |
| 57 | + { |
| 58 | + $this->expectException(\InvalidArgumentException::class); |
| 59 | + $this->expectExceptionMessage('Property "description" is required.'); |
| 60 | + |
| 61 | + RecipeManifest::fromJson(<<<JSON |
| 62 | + { |
| 63 | + "type": "component", |
| 64 | + "name": "MyComponent" |
| 65 | + } |
| 66 | + JSON); |
| 67 | + } |
| 68 | + |
| 69 | + public function testFromJsonWithMissingLicense() |
| 70 | + { |
| 71 | + $this->expectException(\InvalidArgumentException::class); |
| 72 | + $this->expectExceptionMessage('Property "copy-files" is required.'); |
| 73 | + |
| 74 | + RecipeManifest::fromJson(<<<JSON |
| 75 | + { |
| 76 | + "type": "component", |
| 77 | + "name": "MyComponent", |
| 78 | + "description": "An incredible component" |
| 79 | + } |
| 80 | + JSON); |
| 81 | + } |
| 82 | + |
| 83 | + public function testFromJsonWithInvalidDependencies() |
| 84 | + { |
| 85 | + $this->expectException(\InvalidArgumentException::class); |
| 86 | + $this->expectExceptionMessage('Each dependency must be an associative array.'); |
| 87 | + |
| 88 | + RecipeManifest::fromJson(<<<JSON |
| 89 | + { |
| 90 | + "type": "component", |
| 91 | + "name": "MyComponent", |
| 92 | + "description": "An incredible component", |
| 93 | + "copy-files": { |
| 94 | + "templates/": "templates/" |
| 95 | + }, |
| 96 | + "dependencies": ["foo"] |
| 97 | + } |
| 98 | + JSON); |
| 99 | + } |
| 100 | + |
| 101 | + public function testFromJsonWithInvalidDependenciesType() |
| 102 | + { |
| 103 | + $this->expectException(\InvalidArgumentException::class); |
| 104 | + $this->expectExceptionMessage('The dependency type is missing for dependency #0, add "type" key.'); |
| 105 | + |
| 106 | + RecipeManifest::fromJson(<<<JSON |
| 107 | + { |
| 108 | + "type": "component", |
| 109 | + "name": "MyComponent", |
| 110 | + "description": "An incredible component", |
| 111 | + "copy-files": { |
| 112 | + "templates/": "templates/" |
| 113 | + }, |
| 114 | + "dependencies": [ |
| 115 | + {"key": "value"} |
| 116 | + ] |
| 117 | + } |
| 118 | + JSON); |
| 119 | + } |
| 120 | + |
| 121 | + public function testFromJsonWithInvalidPhpDependency() |
| 122 | + { |
| 123 | + $this->expectException(\InvalidArgumentException::class); |
| 124 | + $this->expectExceptionMessage('The package name is missing for dependency #0, add "package" key.'); |
| 125 | + |
| 126 | + RecipeManifest::fromJson(<<<JSON |
| 127 | + { |
| 128 | + "type": "component", |
| 129 | + "name": "MyComponent", |
| 130 | + "description": "An incredible component", |
| 131 | + "copy-files": { |
| 132 | + "templates/": "templates/" |
| 133 | + }, |
| 134 | + "dependencies": [ |
| 135 | + {"type": "php"} |
| 136 | + ] |
| 137 | + } |
| 138 | + JSON); |
| 139 | + } |
| 140 | + |
| 141 | + public function testFromJsonWithInvalidRecipeDependency() |
| 142 | + { |
| 143 | + $this->expectException(\InvalidArgumentException::class); |
| 144 | + $this->expectExceptionMessage('The recipe name is missing for dependency #0, add "name" key.'); |
| 145 | + |
| 146 | + RecipeManifest::fromJson(<<<JSON |
| 147 | + { |
| 148 | + "type": "component", |
| 149 | + "name": "MyComponent", |
| 150 | + "description": "An incredible component", |
| 151 | + "copy-files": { |
| 152 | + "templates/": "templates/" |
| 153 | + }, |
| 154 | + "dependencies": [ |
| 155 | + {"type": "recipe"} |
| 156 | + ] |
| 157 | + } |
| 158 | + JSON); |
| 159 | + } |
| 160 | + |
| 161 | + public function testFromJsonWithMinimumValidData() |
| 162 | + { |
| 163 | + $manifest = RecipeManifest::fromJson(<<<JSON |
| 164 | + { |
| 165 | + "type": "component", |
| 166 | + "name": "MyComponent", |
| 167 | + "description": "An incredible component", |
| 168 | + "copy-files": { |
| 169 | + "templates/": "templates/" |
| 170 | + } |
| 171 | + } |
| 172 | + JSON); |
| 173 | + |
| 174 | + $this->assertSame(RecipeType::Component, $manifest->type); |
| 175 | + $this->assertSame('MyComponent', $manifest->name); |
| 176 | + $this->assertSame('An incredible component', $manifest->description); |
| 177 | + $this->assertSame(['templates/' => 'templates/'], $manifest->copyFiles); |
| 178 | + $this->assertEquals([], $manifest->dependencies); |
| 179 | + } |
| 180 | + |
| 181 | + public function testFromJsonWithValidData() |
| 182 | + { |
| 183 | + $manifest = RecipeManifest::fromJson(<<<JSON |
| 184 | + { |
| 185 | + "type": "component", |
| 186 | + "name": "MyComponent", |
| 187 | + "description": "An incredible component", |
| 188 | + "copy-files": { |
| 189 | + "templates/": "templates/" |
| 190 | + }, |
| 191 | + "dependencies": [ |
| 192 | + { |
| 193 | + "type": "php", |
| 194 | + "package": "symfony/ux-twig-component:^2.29" |
| 195 | + }, |
| 196 | + { |
| 197 | + "type": "recipe", |
| 198 | + "name": "OtherComponent" |
| 199 | + } |
| 200 | + ] |
| 201 | + } |
| 202 | + JSON); |
| 203 | + |
| 204 | + $this->assertSame(RecipeType::Component, $manifest->type); |
| 205 | + $this->assertSame('MyComponent', $manifest->name); |
| 206 | + $this->assertSame('An incredible component', $manifest->description); |
| 207 | + $this->assertSame(['templates/' => 'templates/'], $manifest->copyFiles); |
| 208 | + $this->assertEquals([ |
| 209 | + new PhpPackageDependency('symfony/ux-twig-component', new Version('^2.29')), |
| 210 | + new RecipeDependency('OtherComponent'), |
| 211 | + ], $manifest->dependencies); |
| 212 | + } |
| 213 | +} |
0 commit comments