Skip to content

Commit

Permalink
Fixed bug in Pollen endpoint, where spores was not being correctly pa…
Browse files Browse the repository at this point in the history
…rsed.
  • Loading branch information
rugaard committed Jul 25, 2019
1 parent 62074d1 commit ba91529
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ before_script:
- if [ $(phpenv version-name) = "7.2" ]; then ./cc-test-reporter before-build; fi

script:
- vendor/bin/phpunit --coverage-text --coverage-clover clover.xml
- vendor/bin/phpunit --do-not-cache-result --coverage-text --coverage-clover clover.xml

after_success:
- if [ $(phpenv version-name) = "7.2" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT; fi
52 changes: 38 additions & 14 deletions src/DTO/Pollen.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ public function parse(array $data) : void
// Convert to array.
$data = (array) $data;

// Convert value to integer.
$data['value'] = (int) $data['value'];

// Get level of pollen.
$data['level'] = $this->getPollenLevel($data['name'], $data['value']);
if (in_array($data['name'], ['Alternaria', 'Cladosporium'])) {
$data['level'] = $this->determinePollenLevelByText($data['value']);
$data['value'] = null;
} else {
$data['value'] = is_numeric($data['value']) ? (int) $data['value'] : 0;
$data['level'] = $this->getPollenLevel($data['name'], $data['value']);
}

return Collection::make($data);
}));
Expand Down Expand Up @@ -126,11 +128,11 @@ public function getReadings() : Collection
/**
* Get pollen level.
*
* @param string $name
* @param int $value
* @param string $name
* @param int|string $value
* @return string|null
*/
protected function getPollenLevel(string $name, int $value) :? string
protected function getPollenLevel(string $name, $value) :? string
{
switch ($name) {
case 'Birk':
Expand All @@ -145,10 +147,6 @@ protected function getPollenLevel(string $name, int $value) :? string
return $this->determinePollenLevel($value, 50, 10);
case 'Hassel':
return $this->determinePollenLevel($value, 15, 5);
case 'Alternaria':
return $this->determinePollenLevel($value, 100, 20);
case 'Cladosporium':
return $this->determinePollenLevel($value, 6000, 2000);
}
return null;
}
Expand All @@ -159,10 +157,14 @@ protected function getPollenLevel(string $name, int $value) :? string
* @param int $value
* @param int $highValue
* @param int $moderateValue
* @return string
* @return string|null
*/
protected function determinePollenLevel(int $value, int $highValue, int $moderateValue) : string
protected function determinePollenLevel(int $value, int $highValue, int $moderateValue) :? string
{
if ($value <= 0) {
return null;
}

if ($value > $highValue) {
return 'High';
}
Expand All @@ -173,4 +175,26 @@ protected function determinePollenLevel(int $value, int $highValue, int $moderat

return 'Low';
}

/**
* Determine pollen level by text.
*
* @param string $value
* @return string|null
*/
protected function determinePollenLevelByText(string $value) :? string
{
switch ($value) {
case 'Højt':
return 'High';
break;
case 'Moderat':
return 'Moderate';
break;
case 'Lavt':
return 'Low';
default:
return null;
}
}
}
46 changes: 30 additions & 16 deletions tests/DTO/PollenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function testReadings() : void
* Test pollen levels.
*
* @return void
* @throws \ReflectionException
*/
public function testPollenLevels() : void
{
Expand All @@ -86,42 +85,50 @@ public function testPollenLevels() : void
'Birk' => Collection::make([
['value' => 101, 'expectedLevel' => 'High'],
['value' => 31, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 1, 'expectedLevel' => 'Low'],
['value' => 0, 'expectedLevel' => null]
]),
'Bynke' => Collection::make([
['value' => 51, 'expectedLevel' => 'High'],
['value' => 11, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 1, 'expectedLevel' => 'Low'],
['value' => 0, 'expectedLevel' => null]
]),
'El' => Collection::make([
['value' => 51, 'expectedLevel' => 'High'],
['value' => 11, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 1, 'expectedLevel' => 'Low'],
['value' => 0, 'expectedLevel' => null]
]),
'Elm' => Collection::make([
['value' => 51, 'expectedLevel' => 'High'],
['value' => 11, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 1, 'expectedLevel' => 'Low'],
['value' => 0, 'expectedLevel' => null]
]),
'Græs' => Collection::make([
['value' => 51, 'expectedLevel' => 'High'],
['value' => 11, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 1, 'expectedLevel' => 'Low'],
['value' => 0, 'expectedLevel' => null]
]),
'Hassel' => Collection::make([
['value' => 16, 'expectedLevel' => 'High'],
['value' => 6, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 1, 'expectedLevel' => 'Low'],
['value' => 0, 'expectedLevel' => null]
]),
'Alternaria' => Collection::make([
['value' => 101, 'expectedLevel' => 'High'],
['value' => 21, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 'Højt', 'expectedLevel' => 'High'],
['value' => 'Moderat', 'expectedLevel' => 'Moderate'],
['value' => 'Lavt', 'expectedLevel' => 'Low'],
['value' => '-', 'expectedLevel' => null]
]),
'Cladosporium' => Collection::make([
['value' => 6001, 'expectedLevel' => 'High'],
['value' => 2001, 'expectedLevel' => 'Moderate'],
['value' => 0, 'expectedLevel' => 'Low']
['value' => 'Højt', 'expectedLevel' => 'High'],
['value' => 'Moderat', 'expectedLevel' => 'Moderate'],
['value' => 'Lavt', 'expectedLevel' => 'Low'],
['value' => '-', 'expectedLevel' => null],
]),
]);

Expand All @@ -132,9 +139,16 @@ public function testPollenLevels() : void
// for each available level.
$mockedData->each(function ($data, $pollenName) use ($dto) {
$data->each(function ($test) use ($dto, $pollenName) {
$result = $this->invokeMethod($dto, 'getPollenLevel', [$pollenName, $test['value']]);
$this->assertIsString($result);
$this->assertEquals($test['expectedLevel'], $result);
$result = in_array($pollenName, ['Alternaria', 'Cladosporium'])
? $this->invokeMethod($dto, 'determinePollenLevelByText', [$test['value']])
: $this->invokeMethod($dto, 'getPollenLevel', [$pollenName, $test['value']]);

if ($test['expectedLevel'] !== null) {
$this->assertIsString($result);
$this->assertEquals($test['expectedLevel'], $result);
} else {
$this->assertNull($result);
}
});
});

Expand Down
16 changes: 7 additions & 9 deletions tests/Endpoints/PollenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,19 @@ public function testReadings() : void
$this->assertEquals('Birk', $reading->get('name'));
$this->assertIsInt($reading->get('value'));
$this->assertEquals(0, $reading->get('value'));
$this->assertEquals('Low', $reading->get('level'));
$this->assertNull($reading->get('level'));
break;
case 1:
$this->assertEquals('Bynke', $reading->get('name'));
$this->assertIsInt($reading->get('value'));
$this->assertEquals(0, $reading->get('value'));
$this->assertEquals('Low', $reading->get('level'));
$this->assertNull($reading->get('level'));
break;
case 2:
$this->assertEquals('El', $reading->get('name'));
$this->assertIsInt($reading->get('value'));
$this->assertEquals(0, $reading->get('value'));
$this->assertEquals('Low', $reading->get('level'));
$this->assertNull($reading->get('level'));
break;
case 3:
$this->assertEquals('Elm', $reading->get('name'));
Expand All @@ -194,18 +194,16 @@ public function testReadings() : void
$this->assertEquals('Hassel', $reading->get('name'));
$this->assertIsInt($reading->get('value'));
$this->assertEquals(0, $reading->get('value'));
$this->assertEquals('Low', $reading->get('level'));
$this->assertNull($reading->get('level'));
break;
case 6:
$this->assertEquals('Alternaria', $reading->get('name'));
$this->assertIsInt($reading->get('value'));
$this->assertEquals(0, $reading->get('value'));
$this->assertEquals('Low', $reading->get('level'));
$this->assertNull($reading->get('value'));
$this->assertEquals('Moderate', $reading->get('level'));
break;
case 7:
$this->assertEquals('Cladosporium', $reading->get('name'));
$this->assertIsInt($reading->get('value'));
$this->assertEquals(0, $reading->get('value'));
$this->assertNull($reading->get('value'));
$this->assertEquals('Low', $reading->get('level'));
break;
}
Expand Down

0 comments on commit ba91529

Please sign in to comment.