Skip to content

Commit

Permalink
Fixed null values of ratings
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrakoupil committed Jan 17, 2016
1 parent b68864b commit 512508c
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ vendor/
/tests/coverage.*
/tests/*/output
.DS_Store
/apigen*
/apigen*
.idea
10 changes: 5 additions & 5 deletions src/EshopReview.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,37 +38,37 @@ class EshopReview {
/**
* Hodnocení celkové - na stupnici 0.5 až 5 hvězdiček
*
* @var number
* @var number|null
*/
public $ratingTotal;

/**
* Hodnocení délky dodací lhůty - na stupnici 0.5 až 5 hvězdiček
*
* @var number
* @var number|null
*/
public $ratingDelivery;

/**
* Hodnocení kvality dopravy zboží - na stupnici 0.5 až 5 hvězdiček
*
* @var number
* @var number|null
*/
public $ratingTransportQuality;

/**
* Hodnocení použitelnosti a přehlednosti e-shopu
* na stupnici 0.5 až 5 hvězdiček
*
* @var number
* @var number|null
*/
public $ratingWebUsability;

/**
* Hodnocení komunikace ze strany e-shopu
* na stupnici 0.5 až 5 hvězdiček
*
* @var number
* @var number|null
*/
public $ratingCommunication;

Expand Down
36 changes: 31 additions & 5 deletions src/EshopReviewsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,38 @@ public function processElement(\SimpleXMLElement $element, $index) {
$review->date->setTimestamp((int)$element->unix_timestamp);
$review->orderId = (string)$element->order_id;
$review->pros = (string)$element->pros;
$review->ratingCommunication = (float)$element->communication;
$review->ratingDelivery = (float)$element->delivery_time;

if (count($element->communication)) {
$review->ratingCommunication = (float)$element->communication;
} else {
$review->ratingCommunication = null;
}

if (count($element->delivery_time)) {
$review->ratingDelivery = (float)$element->delivery_time;
} else {
$review->ratingDelivery = null;
}

if (count($element->total_rating)) {
$review->ratingTotal = (float)$element->total_rating;
} else {
$review->ratingTotal = null;
}

if (count($element->transport_quality)) {
$review->ratingTransportQuality = (float)$element->transport_quality;
} else {
$review->ratingTransportQuality = null;
}

if (count($element->web_usability)) {
$review->ratingWebUsability = (float)$element->web_usability;
} else {
$review->ratingWebUsability = null;
}

$review->ratingId = (int)$element->rating_id;
$review->ratingTotal = (float)$element->total_rating;
$review->ratingTransportQuality = (float)$element->transport_quality;
$review->ratingWebUsability = (float)$element->web_usability;
$review->reaction = (string)$element->reaction;
$review->summary = (string)$element->summary;

Expand Down
2 changes: 1 addition & 1 deletion src/ProductReview.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ProductReview {
/**
* Hodnocení produktu na stupnici od 0.5 do 5.
*
* @var number
* @var number|null
*/
public $rating;

Expand Down
8 changes: 8 additions & 0 deletions src/ProductReviewSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class ProductReviewSummary {
*/
public $reviewCount = 0;

/**
* Počet hodnocení na tento produkt.
* Počet hodnocení a počet recenzí nemusí nutně být totéž.
*
* @var int
*/
public $ratingCount = 0;

/**
* Průměrné hodnocení na stupnici 0.5 až 5 hvězdiček
*
Expand Down
25 changes: 17 additions & 8 deletions src/ProductReviewsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ public function processElement(\SimpleXMLElement $element, $index) {
$review->productEan = (string)$element->ean;
$review->productNumber = (string)$element->productno;
$review->pros = (string)$reviewElement->pros;
$review->rating = (float)$reviewElement->rating;
$review->ratingId = (int)$reviewElement->rating_id;
$review->summary = (string)$reviewElement->summary;

if (count($reviewElement->rating) > 0) {
$review->rating = (float)$reviewElement->rating;
} else {
$review->rating = null;
}

$prodId = $this->resolveId($review);
$review->productId = $prodId;

Expand Down Expand Up @@ -208,14 +213,18 @@ protected function addReviewToSummary($productId, ProductReview $review) {
$summary = $this->summary[$productId];

$summary->reviewCount++;
$summary->totalStars += $review->rating;
$summary->averageRating = round($summary->totalStars / $summary->reviewCount, 1);

if (!$summary->bestRating or $summary->bestRating < $review->rating) {
$summary->bestRating = $review->rating;
}
if (!$summary->worstRating or $summary->worstRating > $review->rating) {
$summary->worstRating = $review->rating;
if ($review->rating !== null) {
$summary->ratingCount++;
$summary->totalStars += $review->rating;
$summary->averageRating = round($summary->totalStars / $summary->ratingCount, 1);

if (!$summary->bestRating or $summary->bestRating < $review->rating) {
$summary->bestRating = $review->rating;
}
if (!$summary->worstRating or $summary->worstRating > $review->rating) {
$summary->worstRating = $review->rating;
}
}

if (!$summary->newestReviewDate or $summary->newestReviewDate < $review->date) {
Expand Down
6 changes: 0 additions & 6 deletions tests/example-data/eshop-reviews.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ dobré ceny]]></pros>
<name><![CDATA[bročka]]></name>
<unix_timestamp>1389727969</unix_timestamp>
<total_rating>5</total_rating>
<delivery_time>5</delivery_time>
<transport_quality>5</transport_quality>
<web_usability>5</web_usability>
<communication>5</communication>
Expand All @@ -57,11 +56,6 @@ dobré ceny]]></pros>
<rating_id>59065101</rating_id>
<name><![CDATA[BreHa]]></name>
<unix_timestamp>1387961408</unix_timestamp>
<total_rating>5</total_rating>
<delivery_time>5</delivery_time>
<transport_quality>5</transport_quality>
<web_usability>5</web_usability>
<communication>5</communication>
<pros><![CDATA[super ceny za kvalitní zboží]]></pros>
<cons><![CDATA[žlutý kůň Úpěl Ďábelské ódy]]></cons>
<order_id>2382</order_id>
Expand Down
60 changes: 60 additions & 0 deletions tests/example-data/product-reviews-null.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<product_name><![CDATA[Stojan notový Pecka MSP-008 RD]]></product_name>
<url><![CDATA[http://www.someeshop.cz/stojan-notovy-pecka.html]]></url>
<cena>290.00</cena>
<price>290.00</price>
<order_id>13960</order_id>
<reviews>
<review>
<rating_id>5957872</rating_id>
<unix_timestamp>1452316372</unix_timestamp>
<name><![CDATA[kalamajka]]></name>
<rating>3</rating>
<pros><![CDATA[celkem dobrý]]></pros>
<cons><![CDATA[zatím nic]]></cons>
<summary><![CDATA[vše ok]]></summary>
</review>
</reviews>
</product>
<product>
<product_name><![CDATA[Stojan notový Pecka MSP-008 RD]]></product_name>
<url><![CDATA[http://www.someeshop.cz/stojan-notovy-pecka.html]]></url>
<cena>290.00</cena>
<price>290.00</price>
<order_id>13960</order_id>
<reviews>
<review>
<rating_id>5957872</rating_id>
<unix_timestamp>1452356372</unix_timestamp>
<name><![CDATA[kalamajka]]></name>
<rating>5</rating>
<pros><![CDATA[zatím to vypadá dobře
za ty peníze je to dobrý kšeft
snad se nerozpadne]]></pros>
<cons><![CDATA[zatím nic]]></cons>
<summary><![CDATA[vše ok]]></summary>
</review>
</reviews>
</product>

<product>
<product_name><![CDATA[Stojan notový Pecka MSP-008 RD]]></product_name>
<url><![CDATA[http://www.someeshop.cz/stojan-notovy-pecka.html]]></url>
<cena>290.00</cena>
<price>290.00</price>
<order_id>13960</order_id>
<reviews>
<review>
<rating_id>5957872</rating_id>
<unix_timestamp>1452356372</unix_timestamp>
<name><![CDATA[kalamajka]]></name>
<pros><![CDATA[zatím nechci hodnotit, moc jsem s tím nepracoval.]]></pros>
<cons><![CDATA[zatím nic]]></cons>
<summary><![CDATA[vše ok]]></summary>
</review>
</reviews>
</product>

</products>
27 changes: 26 additions & 1 deletion tests/src/EshopReviewsClient.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,34 @@ class EshopReviewsClientTest extends \Tester\TestCase {

}

function testNullValues() {
$client = new EshopReviewsClient();

$client->useFile(__DIR__ . "/../example-data/eshop-reviews.xml");

$reviews = array();

$client->setCallback(function(EshopReview $review) use (&$reviews) {
$reviews[] = $review;
});

$client->run();

Assert::equal(3.5, $reviews[2]->ratingWebUsability);
Assert::equal(5.0, $reviews[3]->ratingWebUsability);
Assert::null($reviews[3]->ratingDelivery);

Assert::null($reviews[4]->ratingWebUsability);
Assert::null($reviews[4]->ratingTotal);
Assert::null($reviews[4]->ratingDelivery);
Assert::null($reviews[4]->ratingCommunication);
Assert::null($reviews[4]->ratingTransportQuality);

}


}

$test = new EshopReviewsClientTest();

$test->run();
$test->run();
21 changes: 20 additions & 1 deletion tests/src/ProductReviewsClient.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,28 @@ class ProductReviewsClientTest extends \Tester\TestCase {
Assert::same($lastReview, $client->getSummaryOfProduct('http://www.someeshop.cz/klasicka-kytara-44-pecka-nat.html')->reviews[0]);
}

function testWithNullRatings() {
$client = new ProductReviewsClient();
$client->useFile(__DIR__ . "/../example-data/product-reviews-null.xml");
$client->setSaveSummary(true, true);
$client->setIdResolver(function(ProductReview $r) {
return "aaa";
});
$client->run();

$reviews = $client->getSummaryOfProduct("aaa");

Assert::equal(3, $reviews->reviewCount);
Assert::equal(2, $reviews->ratingCount);
Assert::equal(4.0, $reviews->averageRating);
Assert::equal(3.0, $reviews->reviews[0]->rating);
Assert::equal(5.0, $reviews->reviews[1]->rating);
Assert::null($reviews->reviews[2]->rating);
}


}

$test = new ProductReviewsClientTest();

$test->run();
$test->run();

0 comments on commit 512508c

Please sign in to comment.