|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2023 SURFnet B.V. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +namespace OpenConext\EngineBlock\Metadata; |
| 20 | + |
| 21 | +use JsonSerializable; |
| 22 | +use OpenConext\EngineBlock\Exception\MduiRuntimeException; |
| 23 | +use PHPUnit\Framework\TestCase; |
| 24 | + |
| 25 | +class LogoTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @param string $json |
| 29 | + * @dataProvider provideCorrectJson |
| 30 | + */ |
| 31 | + public function test_create_logo_from_json_happy_flow(string $json): void |
| 32 | + { |
| 33 | + $jsonData = json_decode($json, true); |
| 34 | + $logo = Logo::fromJson($jsonData); |
| 35 | + self::assertInstanceOf(Logo::class, $logo); |
| 36 | + self::assertInstanceOf(MultilingualElement::class, $logo); |
| 37 | + self::assertInstanceOf(JsonSerializable::class, $logo); |
| 38 | + self::assertEquals($jsonData['url'], $logo->url); |
| 39 | + |
| 40 | + if (array_key_exists('width', $jsonData)) { |
| 41 | + self::assertEquals($jsonData['width'], $logo->width); |
| 42 | + } |
| 43 | + if (array_key_exists('height', $jsonData)) { |
| 44 | + self::assertEquals($jsonData['height'], $logo->height); |
| 45 | + } |
| 46 | + self::assertIsArray($logo->jsonSerialize()); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param string $json |
| 51 | + * @dataProvider provideUrlMissingJson |
| 52 | + */ |
| 53 | + public function test_create_logo_from_json_requires_url(string $json): void |
| 54 | + { |
| 55 | + self::expectException(MduiRuntimeException::class); |
| 56 | + self::expectExceptionMessage( |
| 57 | + 'Incomplete MDUI Logo data. The URL is missing while serializing the data from JSON' |
| 58 | + ); |
| 59 | + Logo::fromJson(json_decode($json, true)); |
| 60 | + } |
| 61 | + |
| 62 | + public function test_logo_can_not_be_translated() |
| 63 | + { |
| 64 | + self::expectException(MduiRuntimeException::class); |
| 65 | + self::expectExceptionMessage('We do not implement the Mdui Logo in a multilingual fashion'); |
| 66 | + $logo = Logo::fromJson(['url' => 'https://foobar.example.com']); |
| 67 | + $logo->translate('en'); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_primary_language_is_en() |
| 71 | + { |
| 72 | + $logo = Logo::fromJson(['url' => 'https://foobar.example.com']); |
| 73 | + $lang = $logo->getConfiguredLanguages(); |
| 74 | + self::assertEquals(['en'], $lang); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + public function provideCorrectJson() |
| 79 | + { |
| 80 | + return [ |
| 81 | + 'all data present' => ['{"url": "https://logo.example.com/logo.gif", "height": 12, "width": 50}'], |
| 82 | + 'only url set' => ['{"url": "https://logo.example.com/logo.gif"}'], |
| 83 | + 'only logo and width' => ['{"url": "https://logo.example.com/logo.gif", "width": 50}'], |
| 84 | + 'only logo and height' => ['{"url": "https://logo.example.com/logo.gif", "height": 50}'], |
| 85 | + 'additional data is ignored' => ['{"url": "https://logo.example.com/logo.gif", "height": 50, "widtf": 50}'], |
| 86 | + 'url is not validated' => ['{"url": "you are elle", "height": 50, "width": 50}'], |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + public function provideUrlMissingJson() |
| 91 | + { |
| 92 | + return [ |
| 93 | + 'url not present' => ['{"height": 12, "width": 50}'], |
| 94 | + 'url misspelled' => ['{"ulr": "you are elle", "height": 12, "width": 50}'], |
| 95 | + ]; |
| 96 | + } |
| 97 | +} |
0 commit comments