Skip to content

Commit 5994adf

Browse files
committed
(added test case to PersonRiskDetectorServiceTest where all values are filled)
1 parent 42a0c8b commit 5994adf

File tree

1 file changed

+102
-1
lines changed

1 file changed

+102
-1
lines changed

tests/functional/PersonRiskDetectorServiceTest.php

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use org\nameapi\ontology\input\context\Priority;
88
use org\nameapi\ontology\input\entities\person\NaturalInputPerson;
99
use org\nameapi\ontology\input\entities\person\name\InputPersonName;
10+
use org\nameapi\ontology\input\entities\address\StructuredAddress;
11+
use org\nameapi\ontology\input\entities\address\StructuredStreetInfo;
12+
use org\nameapi\ontology\input\entities\address\StructuredPlaceInfo;
1013

1114

1215
/**
@@ -16,13 +19,16 @@
1619
*/
1720
class PersonRiskDetectorServiceTest extends \PHPUnit_Framework_TestCase {
1821

22+
/**
23+
* In this test only the person's name is sent to the server.
24+
*/
1925
public function testDetect() {
2026
//setup code:
2127
$context = Context::builder()
2228
->priority(Priority::REALTIME())
2329
->build();
2430
$myApiKey = 'test'; //grab one from nameapi.org
25-
$serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('rc53-api.nameapi.org'), '5.0');
31+
$serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('rc53-api.nameapi.org'), '5.3');
2632
$riskDetector = $serviceFactory->riskServices()->personRiskDetector();
2733

2834
//the call:
@@ -33,6 +39,7 @@ public function testDetect() {
3339
->gender("FEMALE")
3440
->build();
3541
$riskResult = $riskDetector->detect($inputPerson);
42+
//var_dump($riskResult);
3643

3744
//the assertions:
3845
$this->assertTrue($riskResult->getScore() >= 0.8);
@@ -47,4 +54,98 @@ public function testDetect() {
4754
$this->assertTrue($riskResult->getRisks()[0]->getRiskScore() <= 1.0);
4855
}
4956

57+
58+
/**
59+
* In this test all fields are filled:
60+
* - the person's name
61+
* - an email address
62+
* - a telephone number
63+
* - a physical address
64+
*/
65+
public function testDetect_allValues() {
66+
$context = Context::builder()
67+
->priority(Priority::REALTIME())
68+
->build();
69+
$myApiKey = 'test'; //grab one from nameapi.org
70+
$serviceFactory = new ServiceFactory($myApiKey, $context, Host::http('rc53-api.nameapi.org'), '5.3');
71+
$riskDetector = $serviceFactory->riskServices()->personRiskDetector();
72+
73+
$inputPerson = NaturalInputPerson::builder()
74+
->name(InputPersonName::westernBuilder()
75+
->givenName( "Donald" )
76+
->surname( "Duck" )
77+
->build())
78+
->addEmailAddress("[email protected]")
79+
->addTelNumber("999 999 999")
80+
->addAddressForAll(StructuredAddress::builder()
81+
->streetInfo(StructuredStreetInfo::builder()
82+
->streetName("Winzenheimer Str.")
83+
->houseNumber("5")
84+
->build())
85+
->placeInfo(StructuredPlaceInfo::builder()
86+
->postalCode("55555")
87+
->locality("Atlantis")
88+
->build())
89+
->build())
90+
->build();
91+
92+
$result = $riskDetector->detect($inputPerson);
93+
//var_dump($result);
94+
95+
$this->assertEquals(true, $result->hasRisk());
96+
$this->assertEquals(4, sizeof($result->getRisks()));
97+
$this->assertTrue($result->getScore() >= 0.95 && $result->getScore() <= 1.0);
98+
99+
$this->_expectRiskOnName($result->getRisks());
100+
$this->_expectRiskOnAddress($result->getRisks());
101+
$this->_expectRiskOnEmail($result->getRisks());
102+
$this->_expectRiskOnTel($result->getRisks());
103+
$firstRisk = $result->getRisks()[0];
104+
$this->assertTrue($firstRisk->getRiskScore() >= 0.9 && $firstRisk->getRiskScore() <= 1.0);
105+
$this->assertEquals('NAME', (string)$firstRisk->getDataItem());
106+
$this->assertEquals('FICTIONAL', (string)$firstRisk->getRiskType());
107+
}
108+
109+
110+
private function _expectRiskOnName($risks) {
111+
foreach ($risks as $risk) {
112+
if ((string)$risk->getDataItem() === 'NAME') {
113+
return;
114+
}
115+
}
116+
$this->assertTrue(false); //fail
117+
}
118+
119+
private function _expectRiskOnAddress($risks) {
120+
foreach ($risks as $risk) {
121+
if ((string)$risk->getDataItem() === 'ADDRESS') {
122+
123+
return;
124+
}
125+
}
126+
$this->assertTrue(false); //fail
127+
}
128+
129+
private function _expectRiskOnEmail($risks) {
130+
foreach ($risks as $risk) {
131+
if ((string)$risk->getDataItem() === 'EMAIL') {
132+
133+
return;
134+
}
135+
}
136+
$this->assertTrue(false); //fail
137+
}
138+
139+
private function _expectRiskOnTel($risks) {
140+
foreach ($risks as $risk) {
141+
if ((string)$risk->getDataItem() === 'TEL') {
142+
143+
return;
144+
}
145+
}
146+
$this->assertTrue(false); //fail
147+
}
148+
149+
150+
50151
}

0 commit comments

Comments
 (0)