1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File:StatsHtmlScrapperTest.php |
6
|
|
|
* |
7
|
|
|
* @author Maciej Sławik <[email protected]> |
8
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace MSlwk\Otomoto\App\Test\Unit\Stats\Scrapper; |
12
|
|
|
|
13
|
|
|
use MSlwk\Otomoto\App\Exception\OffersNotFoundException; |
14
|
|
|
use MSlwk\Otomoto\App\Stats\Scrapper\Data\AverageDataScrapperInterface; |
15
|
|
|
use MSlwk\Otomoto\App\Stats\Scrapper\StatsHtmlScrapper; |
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class StatsHtmlScrapperTest |
21
|
|
|
* @package MSlwk\Otomoto\App\Test\Unit\Stats\Scrapper |
22
|
|
|
*/ |
23
|
|
|
class StatsHtmlScrapperTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var MockObject|AverageDataScrapperInterface |
27
|
|
|
*/ |
28
|
|
|
private $mileageScrapper; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var MockObject|AverageDataScrapperInterface |
32
|
|
|
*/ |
33
|
|
|
private $yearScrapper; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var MockObject|AverageDataScrapperInterface |
37
|
|
|
*/ |
38
|
|
|
private $priceScrapper; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MockObject|StatsHtmlScrapper |
42
|
|
|
*/ |
43
|
|
|
private $statsHtmlScrapper; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
protected function setUp() |
49
|
|
|
{ |
50
|
|
|
$this->mileageScrapper = $this->getMockBuilder(AverageDataScrapperInterface::class) |
51
|
|
|
->getMock(); |
52
|
|
|
$this->yearScrapper = $this->getMockBuilder(AverageDataScrapperInterface::class) |
53
|
|
|
->getMock(); |
54
|
|
|
$this->priceScrapper = $this->getMockBuilder(AverageDataScrapperInterface::class) |
55
|
|
|
->getMock(); |
56
|
|
|
|
57
|
|
|
$this->statsHtmlScrapper = new StatsHtmlScrapper( |
58
|
|
|
$this->mileageScrapper, |
59
|
|
|
$this->yearScrapper, |
60
|
|
|
$this->priceScrapper |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @test |
66
|
|
|
* @dataProvider htmlWithOffersDataProvider |
67
|
|
|
* @param string $html |
68
|
|
|
*/ |
69
|
|
|
public function testGetStatsWithOffersFound(string $html) |
70
|
|
|
{ |
71
|
|
|
$this->mileageScrapper->expects($this->exactly(3)) |
|
|
|
|
72
|
|
|
->method('getAverageData') |
73
|
|
|
->willReturnOnConsecutiveCalls(100.00, 200.00, 300.00); |
74
|
|
|
$this->priceScrapper->expects($this->exactly(3)) |
75
|
|
|
->method('getAverageData') |
76
|
|
|
->willReturnOnConsecutiveCalls(150.00, 300.00, 450.00); |
77
|
|
|
$this->yearScrapper->expects($this->exactly(3)) |
78
|
|
|
->method('getAverageData') |
79
|
|
|
->willReturnOnConsecutiveCalls(2016.00, 2017.00, 2018.00); |
80
|
|
|
|
81
|
|
|
$expectedMileage = 200.00; |
82
|
|
|
$expectedPrice = 300.00; |
83
|
|
|
$expectedYear = 2017.00; |
84
|
|
|
|
85
|
|
|
$result = $this->statsHtmlScrapper->scrapModelStats($html); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$this->assertEquals($expectedMileage, $result->getAverageMileage()); |
88
|
|
|
$this->assertEquals($expectedPrice, $result->getAveragePrice()); |
89
|
|
|
$this->assertEquals($expectedYear, $result->getAverageYear()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @test |
94
|
|
|
* @dataProvider htmlWithoutOffersDataProvider |
95
|
|
|
* @param string $html |
96
|
|
|
*/ |
97
|
|
|
public function testGetStatsWithoutOffersFound(string $html) |
98
|
|
|
{ |
99
|
|
|
$this->expectException(OffersNotFoundException::class); |
100
|
|
|
$this->statsHtmlScrapper->scrapModelStats($html); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function htmlWithOffersDataProvider() |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
[ |
110
|
|
|
'<article data-ad-id="6029918432" class="offer-item is-row is-active"><div class="offer-item__photo "> <a data-ad-id="6029918432" class="offer-item__photo-link" data-ninja-extradata=\'{"ad_id": "6029918432"}\' rel="nofollow" target="_blank" href="http://allegro.pl/show_item.php?item=7336548253#eea5f2f722" title="BMW M4 bmw m4 full carbon m performance aso head zamiana!" style="background-image: url(\'https://otomotopl-imagestmp.akamaized.net/images_otomotopl/890043202_1_320x240_bmw-m4-full-carbon-m-performance-aso-head-zamiana-rzeszow.jpg\')"> </a> </div><div class="offer-item__content"> <div class="offer-item__title"> <h2 class="offer-title"> <a data-ad-id="6029918432" class="offer-title__link" data-ninja-extradata=\'{"ad_id": "6029918432"}\' rel="nofollow" target="_blank" href="http://allegro.pl/show_item.php?item=7336548253#eea5f2f722" title="bmw m4 full carbon m performance aso head zamiana!"> bmw m4 full carbon m performance aso head zamiana! </a> <div class="favorite-box in-row"> <a href="#" class="favorite-button observe-link observed-6029918432" rel="nofollow" data-statkey="ad.observed.list" data-id="6029918432" data-tracking="favourite_ad_click" data-ninja-extradata=\'{"ad_id": "6029918432"}\'> <span class="favorite-button__icon icon-observe_Active" title="Usuń z obserwowanych" data-toggle="tooltip" data-placement="bottom"></span> <span class="favorite-button__icon icon-observe_Inactive" title="Obserwuj" data-toggle="tooltip" data-placement="bottom"></span> </a></div><div class="contact-box visible-xs-block"> <div class="om-button action-button transparent contact-button" href="#" data-tracking="contact_button_click" data-id="6029918432" data-ninja-extradata=\'{"ad_id": "6029918432"}\'> <span class="icon-mail"></span> <span class="visible-lg-inline button-text">Kontakt</span> </div><div class="om-contact-layer hidden"> <a class="close-contact-layer icon-zamknij" href=""></a> <form class="quick-contact-form" data-ninja-extradata=\'{"ad_id": "6029918432"}\'> <fieldset><div class="title">Wyślij wiadomość</div><div class="om-fbox focusbox"> <input type="text" name="contact[email]" value="" placeholder="Twój e-mail"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_emailError"></small> </p></div><div class="om-fbox focusbox"> <input type="text" name="contact[phone]" placeholder="Podaj swój nr telefonu"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_phoneError"></small> </p></div><div class="om-fbox focusbox"> <textarea name="contact[txt]"></textarea><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_messageError"></small> </p></div><div class="om-fbox clr"> <input class="om-button secondary sendMessageListing" type="submit" value="Wyślij"></div><input type="hidden" class="adId" name="contact[adid]" value="6029918432"></fieldset></form> </div></div></h2> </div><div class="offer-item__price"> <div class="offer-price"> <span class="offer-price__number">229 000 <span class="offer-price__currency">PLN</span> </span> <span class="offer-price__details"> Brutto </span> </div></div><ul class="offer-item__params"><li class="offer-item__params-item" data-code="year"> <span>2015 </span> </li><li class="offer-item__params-item" data-code="mileage"> <span>49 000 km</span> </li><li class="offer-item__params-item" data-code="engine_capacity"> <span>3 000 cm3</span> </li><li class="offer-item__params-item" data-code="fuel_type"> <span>Benzyna</span> </li></ul><div class="offer-item__bottom-row "> <div class="contact-box hidden-xs"> <div class="om-button action-button transparent contact-button" href="#" data-tracking="contact_button_click" data-id="6029918432" data-ninja-extradata=\'{"ad_id": "6029918432"}\'> <span class="icon-mail"></span> <span class="visible-lg-inline button-text">Kontakt</span> </div><div class="om-contact-layer hidden"> <a class="close-contact-layer icon-zamknij" href=""></a> <form class="quick-contact-form" data-ninja-extradata=\'{"ad_id": "6029918432"}\'> <fieldset><div class="title">Wyślij wiadomość</div><div class="om-fbox focusbox"> <input type="text" name="contact[email]" value="" placeholder="Twój e-mail"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_emailError"></small> </p></div><div class="om-fbox focusbox"> <input type="text" name="contact[phone]" placeholder="Podaj swój nr telefonu"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_phoneError"></small> </p></div><div class="om-fbox focusbox"> <textarea name="contact[txt]"></textarea><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_messageError"></small> </p></div><div class="om-fbox clr"> <input class="om-button secondary sendMessageListing" type="submit" value="Wyślij"></div><input type="hidden" class="adId" name="contact[adid]" value="6029918432"></fieldset></form> </div></div><span class="offer-item__location"> <span class="icon-location-2"></span> <h4>Rzeszów <em>(Podkarpackie)</em> </h4> </span> </div></div></article><article data-ad-id="6029739372" class="offer-item is-row is-active"><div class="offer-item__photo "> <a data-ad-id="6029739372" class="offer-item__photo-link" data-ninja-extradata=\'{"ad_id": "6029739372"}\' rel="nofollow" target="_blank" href="http://allegro.pl/show_item.php?item=7333559036#eea5f2f722" title="BMW M4 bmw 540, 540i automat, shadow-line, beżowe skóry," style="background-image: url(\'https://otomotopl-imagestmp.akamaized.net/images_otomotopl/889942957_1_320x240_bmw-540-540i-automat-shadow-line-bezowe-skory-warszawa.jpg\')"> </a> </div><div class="offer-item__content"> <div class="offer-item__title"> <h2 class="offer-title"> <a data-ad-id="6029739372" class="offer-title__link" data-ninja-extradata=\'{"ad_id": "6029739372"}\' rel="nofollow" target="_blank" href="http://allegro.pl/show_item.php?item=7333559036#eea5f2f722" title="bmw 540, 540i automat, shadow-line, beżowe skóry,"> bmw 540, 540i automat, shadow-line, beżowe skóry, </a> <div class="favorite-box in-row"> <a href="#" class="favorite-button observe-link observed-6029739372" rel="nofollow" data-statkey="ad.observed.list" data-id="6029739372" data-tracking="favourite_ad_click" data-ninja-extradata=\'{"ad_id": "6029739372"}\'> <span class="favorite-button__icon icon-observe_Active" title="Usuń z obserwowanych" data-toggle="tooltip" data-placement="bottom"></span> <span class="favorite-button__icon icon-observe_Inactive" title="Obserwuj" data-toggle="tooltip" data-placement="bottom"></span> </a></div><div class="contact-box visible-xs-block"> <div class="om-button action-button transparent contact-button" href="#" data-tracking="contact_button_click" data-id="6029739372" data-ninja-extradata=\'{"ad_id": "6029739372"}\'> <span class="icon-mail"></span> <span class="visible-lg-inline button-text">Kontakt</span> </div><div class="om-contact-layer hidden"> <a class="close-contact-layer icon-zamknij" href=""></a> <form class="quick-contact-form" data-ninja-extradata=\'{"ad_id": "6029739372"}\'> <fieldset><div class="title">Wyślij wiadomość</div><div class="om-fbox focusbox"> <input type="text" name="contact[email]" value="" placeholder="Twój e-mail"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_emailError"></small> </p></div><div class="om-fbox focusbox"> <input type="text" name="contact[phone]" placeholder="Podaj swój nr telefonu"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_phoneError"></small> </p></div><div class="om-fbox focusbox"> <textarea name="contact[txt]"></textarea><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_messageError"></small> </p></div><div class="om-fbox clr"> <input class="om-button secondary sendMessageListing" type="submit" value="Wyślij"></div><input type="hidden" class="adId" name="contact[adid]" value="6029739372"></fieldset></form> </div></div></h2> </div><div class="offer-item__price"> <div class="offer-price"> <span class="offer-price__number">23 500 <span class="offer-price__currency">PLN</span> </span> <span class="offer-price__details"> Brutto </span> </div></div><ul class="offer-item__params"><li class="offer-item__params-item" data-code="year"> <span>1996 </span> </li><li class="offer-item__params-item" data-code="mileage"> <span>170 290 km</span> </li><li class="offer-item__params-item" data-code="engine_capacity"> <span>4 398 cm3</span> </li><li class="offer-item__params-item" data-code="fuel_type"> <span>Benzyna</span> </li></ul><div class="offer-item__bottom-row "> <div class="contact-box hidden-xs"> <div class="om-button action-button transparent contact-button" href="#" data-tracking="contact_button_click" data-id="6029739372" data-ninja-extradata=\'{"ad_id": "6029739372"}\'> <span class="icon-mail"></span> <span class="visible-lg-inline button-text">Kontakt</span> </div><div class="om-contact-layer hidden"> <a class="close-contact-layer icon-zamknij" href=""></a> <form class="quick-contact-form" data-ninja-extradata=\'{"ad_id": "6029739372"}\'> <fieldset><div class="title">Wyślij wiadomość</div><div class="om-fbox focusbox"> <input type="text" name="contact[email]" value="" placeholder="Twój e-mail"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_emailError"></small> </p></div><div class="om-fbox focusbox"> <input type="text" name="contact[phone]" placeholder="Podaj swój nr telefonu"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_phoneError"></small> </p></div><div class="om-fbox focusbox"> <textarea name="contact[txt]"></textarea><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_messageError"></small> </p></div><div class="om-fbox clr"> <input class="om-button secondary sendMessageListing" type="submit" value="Wyślij"></div><input type="hidden" class="adId" name="contact[adid]" value="6029739372"></fieldset></form> </div></div><span class="offer-item__location"> <span class="icon-location-2"></span> <h4>Warszawa <em>(Mazowieckie)</em> </h4> </span> </div></div></article><article data-ad-id="6029256767" class="offer-item is-row is-active"><div class="offer-item__photo "> <a data-ad-id="6029256767" class="offer-item__photo-link" data-ninja-extradata=\'{"ad_id": "6029256767"}\' rel="nofollow" target="_blank" href="http://allegro.pl/show_item.php?item=7322814982#eea5f2f722" title="BMW M4 bmw m4 hud harman full led carbon" style="background-image: url(\'https://otomotopl-imagestmp.akamaized.net/images_otomotopl/888864852_1_320x240_bmw-m4-hud-harman-full-led-carbon-poznan.jpg\')"> </a> </div><div class="offer-item__content"> <div class="offer-item__title"> <h2 class="offer-title"> <a data-ad-id="6029256767" class="offer-title__link" data-ninja-extradata=\'{"ad_id": "6029256767"}\' rel="nofollow" target="_blank" href="http://allegro.pl/show_item.php?item=7322814982#eea5f2f722" title="bmw m4 hud harman full led carbon"> bmw m4 hud harman full led carbon </a> <div class="favorite-box in-row"> <a href="#" class="favorite-button observe-link observed-6029256767" rel="nofollow" data-statkey="ad.observed.list" data-id="6029256767" data-tracking="favourite_ad_click" data-ninja-extradata=\'{"ad_id": "6029256767"}\'> <span class="favorite-button__icon icon-observe_Active" title="Usuń z obserwowanych" data-toggle="tooltip" data-placement="bottom"></span> <span class="favorite-button__icon icon-observe_Inactive" title="Obserwuj" data-toggle="tooltip" data-placement="bottom"></span> </a></div><div class="contact-box visible-xs-block"> <div class="om-button action-button transparent contact-button" href="#" data-tracking="contact_button_click" data-id="6029256767" data-ninja-extradata=\'{"ad_id": "6029256767"}\'> <span class="icon-mail"></span> <span class="visible-lg-inline button-text">Kontakt</span> </div><div class="om-contact-layer hidden"> <a class="close-contact-layer icon-zamknij" href=""></a> <form class="quick-contact-form" data-ninja-extradata=\'{"ad_id": "6029256767"}\'> <fieldset><div class="title">Wyślij wiadomość</div><div class="om-fbox focusbox"> <input type="text" name="contact[email]" value="" placeholder="Twój e-mail"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_emailError"></small> </p></div><div class="om-fbox focusbox"> <input type="text" name="contact[phone]" placeholder="Podaj swój nr telefonu"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_phoneError"></small> </p></div><div class="om-fbox focusbox"> <textarea name="contact[txt]"></textarea><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_messageError"></small> </p></div><div class="om-fbox clr"> <input class="om-button secondary sendMessageListing" type="submit" value="Wyślij"></div><input type="hidden" class="adId" name="contact[adid]" value="6029256767"></fieldset></form> </div></div></h2> </div><div class="offer-item__price"> <div class="offer-price"> <span class="offer-price__number">255 500 <span class="offer-price__currency">PLN</span> </span> <span class="offer-price__details"> Brutto </span> </div></div><ul class="offer-item__params"><li class="offer-item__params-item" data-code="year"> <span>2016 </span> </li><li class="offer-item__params-item" data-code="mileage"> <span>15 800 km</span> </li><li class="offer-item__params-item" data-code="engine_capacity"> <span>2 998 cm3</span> </li><li class="offer-item__params-item" data-code="fuel_type"> <span>Benzyna</span> </li></ul><div class="offer-item__bottom-row "> <div class="contact-box hidden-xs"> <div class="om-button action-button transparent contact-button" href="#" data-tracking="contact_button_click" data-id="6029256767" data-ninja-extradata=\'{"ad_id": "6029256767"}\'> <span class="icon-mail"></span> <span class="visible-lg-inline button-text">Kontakt</span> </div><div class="om-contact-layer hidden"> <a class="close-contact-layer icon-zamknij" href=""></a> <form class="quick-contact-form" data-ninja-extradata=\'{"ad_id": "6029256767"}\'> <fieldset><div class="title">Wyślij wiadomość</div><div class="om-fbox focusbox"> <input type="text" name="contact[email]" value="" placeholder="Twój e-mail"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_emailError"></small> </p></div><div class="om-fbox focusbox"> <input type="text" name="contact[phone]" placeholder="Podaj swój nr telefonu"><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_phoneError"></small> </p></div><div class="om-fbox focusbox"> <textarea name="contact[txt]"></textarea><p class="desc errorboxContainer"> <small class="ca6 lheight24" id="se_messageError"></small> </p></div><div class="om-fbox clr"> <input class="om-button secondary sendMessageListing" type="submit" value="Wyślij"></div><input type="hidden" class="adId" name="contact[adid]" value="6029256767"></fieldset></form> </div></div><span class="offer-item__location"> <span class="icon-location-2"></span> <h4>Poznań <em>(Wielkopolskie)</em> </h4> </span> </div></div></article>' |
111
|
|
|
] |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function htmlWithoutOffersDataProvider() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
[ |
122
|
|
|
'<html></html>' |
123
|
|
|
] |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.