|
| 1 | +<?php |
| 2 | +/* ********************************************************************* |
| 3 | + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. |
| 4 | + * Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close, |
| 5 | + * Caversham, Reading, Berkshire, United Kingdom RG4 7BY. |
| 6 | + * |
| 7 | + * This Original Work is licensed under the European Union Public Licence (EUPL) |
| 8 | + * v.1.2 and is subject to its terms as set out below. |
| 9 | + * |
| 10 | + * If a copy of the EUPL was not distributed with this file, You can obtain |
| 11 | + * one at https://opensource.org/licenses/EUPL-1.2. |
| 12 | + * |
| 13 | + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be |
| 14 | + * amended by the European Commission) shall be deemed incompatible for |
| 15 | + * the purposes of the Work and the provisions of the compatibility |
| 16 | + * clause in Article 5 of the EUPL shall not apply. |
| 17 | + * |
| 18 | + * If using the Work as, or as part of, a network application, by |
| 19 | + * including the attribution notice(s) required under Article 5 of the EUPL |
| 20 | + * in the end user terms of the application under an appropriate heading, |
| 21 | + * such notice(s) shall fulfill the requirements of that article. |
| 22 | + * ********************************************************************* */ |
| 23 | + |
| 24 | +namespace fiftyone\pipeline\engines\tests; |
| 25 | + |
| 26 | +require(__DIR__ . "/../vendor/autoload.php"); |
| 27 | + |
| 28 | +use fiftyone\pipeline\core\FlowData; |
| 29 | +use fiftyone\pipeline\core\ElementData; |
| 30 | +use fiftyone\pipeline\core\FlowElement; |
| 31 | +use fiftyone\pipeline\core\Messages; |
| 32 | + |
| 33 | +use PHPUnit\Framework\TestCase; |
| 34 | + |
| 35 | +class FlowDataTests extends TestCase { |
| 36 | + |
| 37 | + /** |
| 38 | + * Check that an element data can be returned from a FlowData using its |
| 39 | + * data key. |
| 40 | + */ |
| 41 | + public function testGetWithKey() { |
| 42 | + $element = $this->createMock(FlowElement::class); |
| 43 | + $element->dataKey = "testKey"; |
| 44 | + $data = new ElementData($element); |
| 45 | + |
| 46 | + $flowData = new FlowData(null); |
| 47 | + $flowData->setElementData($data); |
| 48 | + |
| 49 | + $returnedData = $flowData->get("testKey"); |
| 50 | + $this->assertNotNull($returnedData); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check that an element data can be returned from a FlowData using its |
| 55 | + * data key directly via a "magic getter". |
| 56 | + */ |
| 57 | + public function testMagicGetter() { |
| 58 | + $element = $this->createMock(FlowElement::class); |
| 59 | + $element->dataKey = "testKey"; |
| 60 | + $data = new ElementData($element); |
| 61 | + |
| 62 | + $flowData = new FlowData(null); |
| 63 | + $flowData->setElementData($data); |
| 64 | + |
| 65 | + $returnedData = $flowData->testKey; |
| 66 | + $this->assertNotNull($returnedData); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Check that an element data can be returned from a FlowData using the |
| 71 | + * getFromElement method. |
| 72 | + */ |
| 73 | + public function testGetFromElement() { |
| 74 | + $element = $this->createMock(FlowElement::class); |
| 75 | + $element->dataKey = "testKey"; |
| 76 | + $data = new ElementData($element); |
| 77 | + |
| 78 | + $flowData = new FlowData(null); |
| 79 | + $flowData->setElementData($data); |
| 80 | + |
| 81 | + $returnedData = $flowData->getFromElement($element); |
| 82 | + $this->assertNotNull($returnedData); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Check that an exception is thrown when fetching a key which does not |
| 87 | + * exist in the FlowData, and that the correct error message is returned. |
| 88 | + */ |
| 89 | + public function testMissingKey() { |
| 90 | + $element = $this->createMock(FlowElement::class); |
| 91 | + $element->dataKey = "testKey"; |
| 92 | + $data = new ElementData($element); |
| 93 | + |
| 94 | + $flowData = new FlowData(null); |
| 95 | + $flowData->setElementData($data); |
| 96 | + |
| 97 | + try { |
| 98 | + $returnedData = $flowData->get("otherKey"); |
| 99 | + $this->fail(); |
| 100 | + } |
| 101 | + catch (\Exception $e) { |
| 102 | + $this->assertEquals( |
| 103 | + sprintf(Messages::NO_ELEMENT_DATA, |
| 104 | + "otherKey", |
| 105 | + "testKey"), |
| 106 | + $e->getMessage()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Check that an exception is thrown when fetching a key through a magic |
| 112 | + * getter which does not exist in the FlowData, and that the correct error |
| 113 | + * message is returned. |
| 114 | + */ |
| 115 | + public function testMissingKeyMagicGetter() { |
| 116 | + $element = $this->createMock(FlowElement::class); |
| 117 | + $element->dataKey = "testKey"; |
| 118 | + $data = new ElementData($element); |
| 119 | + |
| 120 | + $flowData = new FlowData(null); |
| 121 | + $flowData->setElementData($data); |
| 122 | + |
| 123 | + try { |
| 124 | + $returnedData = $flowData->otherKey; |
| 125 | + $this->fail(); |
| 126 | + } |
| 127 | + catch (\Exception $e) { |
| 128 | + $this->assertEquals( |
| 129 | + sprintf(Messages::NO_ELEMENT_DATA, |
| 130 | + "otherKey", |
| 131 | + "testKey"), |
| 132 | + $e->getMessage()); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Check that an exception is thrown when fetching a key using the |
| 138 | + * getFromElement method which does not exist in the FlowData, and that the |
| 139 | + * correct error message is returned. |
| 140 | + */ |
| 141 | + public function testMissingKeyFromElement() { |
| 142 | + $element = $this->createMock(FlowElement::class); |
| 143 | + $element->dataKey = "testKey"; |
| 144 | + $data = new ElementData($element); |
| 145 | + |
| 146 | + $element2 = $this->createMock(FlowElement::class); |
| 147 | + $element2->dataKey = "otherKey"; |
| 148 | + |
| 149 | + $flowData = new FlowData(null); |
| 150 | + $flowData->setElementData($data); |
| 151 | + |
| 152 | + try { |
| 153 | + $returnedData = $flowData->getFromElement($element2); |
| 154 | + $this->fail(); |
| 155 | + } |
| 156 | + catch (\Exception $e) { |
| 157 | + $this->assertEquals( |
| 158 | + sprintf(Messages::NO_ELEMENT_DATA, |
| 159 | + "otherKey", |
| 160 | + "testKey"), |
| 161 | + $e->getMessage()); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments