Skip to content

Commit bf01fce

Browse files
Merge branch 'release/v4.2.0'
OPTIM/TEST: Improved missing property handling and added tests. - OPTIM: Improved handling of missing property values. - TEST: Added tests for various missing value scenarios.
2 parents 4635f76 + 308d161 commit bf01fce

File tree

8 files changed

+235
-22
lines changed

8 files changed

+235
-22
lines changed

Evidence.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
namespace fiftyone\pipeline\core;
2525

26+
use fiftyone\pipeline\core\Messages;
27+
2628
/**
2729
* Storage of evidence on a FlowData object
2830
*/
@@ -67,7 +69,7 @@ public function set($key, $value)
6769
public function setArray($array)
6870
{
6971
if (!is_array($array)) {
70-
$this->flowData->setError("core", "Must pass key and value");
72+
$this->flowData->setError("core", Messages::PASS_KEY_VALUE);
7173
}
7274

7375
foreach ($array as $key => $value) {

FlowData.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
namespace fiftyone\pipeline\core;
2525

26+
use fiftyone\pipeline\core\Messages;
2627
/**
2728
* FlowData is created by a specific Pipeline
2829
* It collects evidence set by the user
@@ -78,7 +79,7 @@ public function process()
7879
$this->processed = true;
7980
return $this;
8081
} else {
81-
$this->setError("global", "FlowData already processed");
82+
$this->setError("global", Messages::FLOW_DATA_PROCESSED);
8283
}
8384
}
8485

@@ -89,11 +90,7 @@ public function process()
8990
*/
9091
public function getFromElement($flowElement)
9192
{
92-
if (isset($this->data[$flowElement->dataKey])) {
93-
return $this->data[$flowElement->dataKey];
94-
} else {
95-
return null;
96-
}
93+
return $this->get($flowElement->dataKey);
9794
}
9895

9996
/**
@@ -107,7 +104,10 @@ public function get($flowElementKey)
107104
if (isset($this->data[$flowElementKey])) {
108105
return $this->data[$flowElementKey];
109106
} else {
110-
return null;
107+
throw new \Exception(
108+
sprintf(Messages::NO_ELEMENT_DATA,
109+
$flowElementKey,
110+
join(",", array_keys($this->data))));
111111
}
112112
}
113113

@@ -118,11 +118,7 @@ public function get($flowElementKey)
118118
*/
119119
public function __get($flowElementKey)
120120
{
121-
if (isset($this->data[$flowElementKey])) {
122-
return $this->data[$flowElementKey];
123-
} else {
124-
return null;
125-
}
121+
return $this->get($flowElementKey);
126122
}
127123

128124
/**
@@ -201,15 +197,18 @@ public function getWhere($metaKey, $metaValue)
201197

202198
if (isset($keys)) {
203199
foreach ($keys as $key => $flowElement) {
204-
// First check if FlowElement has any data set
205200

206-
$data = $this->get($flowElement);
201+
// First check if FlowElement has any data set
207202

208-
if ($data) {
209-
try {
210-
$output[$key] = $data->get($key);
211-
} catch (\Exception $e) {
212-
continue;
203+
if (isset($this->data[$flowElement])) {
204+
$data = $this->get($flowElement);
205+
206+
if ($data) {
207+
try {
208+
$output[$key] = $data->get($key);
209+
} catch (\Exception $e) {
210+
continue;
211+
}
213212
}
214213
}
215214
}

Messages.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\core;
25+
26+
/**
27+
* Constants used for messages returned to the user. These can be error
28+
* messages, or otherwise. Messages which require formatting will contain
29+
* format characters e.g. %s.
30+
*/
31+
class Messages {
32+
/**
33+
* Error message thrown when there is no matching element in the FlowData.
34+
*/
35+
public const NO_ELEMENT_DATA = "There is no element data for '%s' against "
36+
. "this flow data. Available element data keys are: '%s'";
37+
public const PASS_KEY_VALUE = "Must pass key and value";
38+
public const FLOW_DATA_PROCESSED = "FlowData already processed";
39+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"FlowData.php",
3838
"FlowElement.php",
3939
"Logger.php",
40+
"Messages.php",
4041
"Pipeline.php",
4142
"PipelineBuilder.php",
4243
"JavascriptBuilder.php",

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<file>tests/core.php</file>
55
<file>tests/examples.php</file>
66
<file>tests/javascriptBuilderTests.php</file>
7+
<file>tests/FlowDataTests.php</file>
78
</testsuite>
89
</testsuites>
910
</phpunit>

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
![51Degrees](https://51degrees.com/DesktopModules/FiftyOne/Distributor/Logo.ashx?utm_source=github&utm_medium=repository&utm_content=readme_main&utm_campaign=php-open-source "Data rewards the curious") **PHP Pipeline API**
44

5-
[Developer Documentation](https://docs.51degrees.com?utm_source=github&utm_medium=repository&utm_content=documentation&utm_campaign=php-open-source "developer documentation")
5+
[Developer Documentation](https://51degrees.com/documentation/4.2/index.html?utm_source=github&utm_medium=repository&utm_content=documentation&utm_campaign=php-open-source"developer documentation")
66

77
## Introduction
88
This project contains the core source code for the PHP implementation of the 51Degrees Pipeline API.

tests/FlowDataTests.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}

tests/core.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,15 @@ public function testGetFromElement()
8989
// Test check stop FlowData works
9090
public function testStopFlowData()
9191
{
92+
$getValue = null;
9293
$testPipeline = new TestPipeline();
93-
$getValue = $testPipeline->flowData->get("example2");
94+
try {
95+
$getValue = $testPipeline->flowData->get("example2");
96+
$this->fail();
97+
}
98+
catch (\Exception $e) {
99+
// An exception should be thrown.
100+
}
94101
$this->assertTrue($getValue === null);
95102
}
96103

0 commit comments

Comments
 (0)