Skip to content

Commit

Permalink
Fix search using empty search term (#20)
Browse files Browse the repository at this point in the history
* Update value of search filter for google shooping
* Fix Google Shopping wrong param name to froogle
* Fix search using empty search term

Co-authored-by: chistel <46791424+chistel@users.noreply.github.com>
  • Loading branch information
gabrielfs7 and chistel authored Nov 1, 2020
1 parent 3c4cc77 commit f513ad5
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 72 deletions.
74 changes: 39 additions & 35 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
# CHANGELOG

## 2.0.1

- Fix Google Shopping wrong param name to froogle

## 2.0.0

- Add PSR7 Search compatibility
- Add common SearchInterface (Breaking change)
- Add open api specs
- Allow test api locally with PHP builtin server

## 1.2.2

- Improve tests
- Fix rising and top metrics filter

## 1.2.1

- Fix composer install documentation

## 1.2.0

- Added support for interests by region
- Improve documentation and code standard

## 1.1.0

- Added support for interests over time series result
- Improve documentation and code standard

## 1.0.0

- First stable release
# CHANGELOG

## 2.0.2

- Fix search using empty search term

## 2.0.1

- Fix Google Shopping wrong param name to froogle

## 2.0.0

- Add PSR7 Search compatibility
- Add common SearchInterface (Breaking change)
- Add open api specs
- Allow test api locally with PHP builtin server

## 1.2.2

- Improve tests
- Fix rising and top metrics filter

## 1.2.1

- Fix composer install documentation

## 1.2.0

- Added support for interests by region
- Improve documentation and code standard

## 1.1.0

- Added support for interests over time series result
- Improve documentation and code standard

## 1.0.0

- First stable release
- Allow search using internal class for queries and results
19 changes: 11 additions & 8 deletions src/Search/AbstractRelatedSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ private function buildQuery(SearchFilter $searchFilter): string
],
'time' => $searchFilter->getTime(),
'originalTimeRangeForExploreUrl' => $searchFilter->getTime(),
'complexKeywordsRestriction' => [
'keyword' => [
[
'type' => 'BROAD',
'value' => $searchFilter->getSearchTerm(),
],
],
],
],
'keywordType' => $this->getKeywordType(),
'metric' => [
Expand All @@ -139,6 +131,17 @@ private function buildQuery(SearchFilter $searchFilter): string
'userCountryCode' => $searchFilter->getLocation(),
];

if (!empty($searchFilter->getSearchTerm())) {
$request['restriction']['complexKeywordsRestriction'] = [
'keyword' => [
[
'type' => 'BROAD',
'value' => $searchFilter->getSearchTerm(),
],
],
];
}

$query = [
'hl' => $searchFilter->getLanguage(),
'tz' => '-120',
Expand Down
5 changes: 4 additions & 1 deletion src/Search/ExploreSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private function buildUrl(SearchFilter $searchFilter): string
$request = [
'comparisonItem' => [
[
'keyword' => $searchFilter->getSearchTerm(),
'geo' => $searchFilter->getLocation(),
'time' => $searchFilter->getTime()
]
Expand All @@ -69,6 +68,10 @@ private function buildUrl(SearchFilter $searchFilter): string
'property' => $searchFilter->getSearchType(),
];

if (!empty($searchFilter->getSearchTerm())) {
$request['comparisonItem'][0]['keyword'] = $searchFilter->getSearchTerm();
}

$query = [
'hl' => $searchFilter->getLanguage(),
'tz' => '-120',
Expand Down
19 changes: 11 additions & 8 deletions src/Search/InterestByRegionSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ private function buildQuery(SearchFilter $searchFilter): string
'comparisonItem' => [
[
'time' => $searchFilter->getTime(),
'complexKeywordsRestriction' => [
'keyword' => [
[
'type' => 'BROAD',
'value' => $searchFilter->getSearchTerm(),
],
],
]
],
],
'resolution' => 'REGION',
Expand All @@ -109,6 +101,17 @@ private function buildQuery(SearchFilter $searchFilter): string
]
];

if (!empty($searchFilter->getSearchTerm())) {
$request['comparisonItem'][0]['complexKeywordsRestriction'] = [
'keyword' => [
[
'type' => 'BROAD',
'value' => $searchFilter->getSearchTerm(),
],
],
];
}

$query = [
'hl' => $searchFilter->getLanguage(),
'tz' => '-60',
Expand Down
19 changes: 11 additions & 8 deletions src/Search/InterestOverTimeSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ private function buildQuery(SearchFilter $searchFilter): string
'geo' => [
'country' => $searchFilter->getLocation(),
],
'complexKeywordsRestriction' => [
'keyword' => [
[
'type' => 'BROAD',
'value' => $searchFilter->getSearchTerm(),
],
],
]
],
],
'requestOptions' => [
Expand All @@ -108,6 +100,17 @@ private function buildQuery(SearchFilter $searchFilter): string
]
];

if (!empty($searchFilter->getSearchTerm())) {
$request['comparisonItem'][0]['complexKeywordsRestriction'] = [
'keyword' => [
[
'type' => 'BROAD',
'value' => $searchFilter->getSearchTerm(),
],
],
];
}

$query = [
'hl' => $searchFilter->getLanguage(),
'tz' => '-60',
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/Search/ExploreSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class ExploreSearchTest extends TestCase

public function setUp(): void
{
$this->searchFilter = new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00'));
$this->searchFilter = (new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00')))
->withSearchTerm('_keyword_');
$this->searchRequest = $this->createMock(SearchRequest::class);
$this->sut = new ExploreSearch($this->searchRequest);
}
Expand All @@ -45,7 +46,7 @@ public function testSearchWillReturnResult(): void
$this->searchRequest
->expects($this->once())
->method('search')
->with('https://trends.google.com/trends/api/explore?hl=en-US&tz=-120&req=%7B%22comparisonItem%22:%5B%7B%22keyword%22:%22%22,%22geo%22:%22US%22,%22time%22:%222010-09-10+2010-10-10%22%7D%5D,%22category%22:0,%22property%22:%22%22%7D')
->with('https://trends.google.com/trends/api/explore?hl=en-US&tz=-120&req=%7B%22comparisonItem%22:%5B%7B%22geo%22:%22US%22,%22time%22:%222010-09-10+2010-10-10%22,%22keyword%22:%22_keyword_%22%7D%5D,%22category%22:0,%22property%22:%22%22%7D')
->willReturn(
[
'widgets' => [
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/Search/InterestByRegionSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class InterestByRegionSearchTest extends TestCase
{
private const CURRENT_DATE = '2010-01-01 00:00:00';
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/comparedgeo?hl=en-US&tz=-60&req=%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22comparisonItem%22:%5B%7B%22time%22:%222010-09-10+2010-10-10%22,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22%22%7D%5D%7D%7D%5D,%22resolution%22:%22REGION%22,%22locale%22:%22en-US%22,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D%7D&token=TOKEN';
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/comparedgeo?hl=en-US&tz=-60&req=%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22comparisonItem%22:%5B%7B%22time%22:%222010-09-10+2010-10-10%22,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22_keyword_%22%7D%5D%7D%7D%5D,%22resolution%22:%22REGION%22,%22locale%22:%22en-US%22,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D%7D&token=TOKEN';

/**
* @var SearchRequest
Expand All @@ -41,7 +41,8 @@ class InterestByRegionSearchTest extends TestCase

public function setUp(): void
{
$this->searchFilter = new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00'));
$this->searchFilter = (new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00')))
->withSearchTerm('_keyword_');
$this->searchRequest = $this->createMock(SearchRequest::class);
$this->exploreSearch = $this->createMock(ExploreSearch::class);
$this->sut = new InterestByRegionSearch($this->exploreSearch, $this->searchRequest);
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/Search/InterestOverTimeSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class InterestOverTimeSearchTest extends TestCase
{
private const CURRENT_DATE = '2010-01-01 00:00:00';
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/multiline?hl=en-US&tz=-60&req=%7B%22time%22:%222010-09-10+2010-10-10%22,%22resolution%22:%22DAY%22,%22locale%22:%22en-US%22,%22comparisonItem%22:%5B%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22%22%7D%5D%7D%7D%5D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D%7D&token=TOKEN';
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/multiline?hl=en-US&tz=-60&req=%7B%22time%22:%222010-09-10+2010-10-10%22,%22resolution%22:%22DAY%22,%22locale%22:%22en-US%22,%22comparisonItem%22:%5B%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22_keyword_%22%7D%5D%7D%7D%5D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D%7D&token=TOKEN';

/**
* @var SearchRequest
Expand All @@ -41,7 +41,8 @@ class InterestOverTimeSearchTest extends TestCase

public function setUp(): void
{
$this->searchFilter = new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00'));
$this->searchFilter = (new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00')))
->withSearchTerm('_keyword_');
$this->searchRequest = $this->createMock(SearchRequest::class);
$this->exploreSearch = $this->createMock(ExploreSearch::class);
$this->sut = new InterestOverTimeSearch($this->exploreSearch, $this->searchRequest);
Expand Down
7 changes: 3 additions & 4 deletions tests/Integration/Search/RelatedQueriesSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class RelatedQueriesSearchTest extends TestCase
{
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/relatedsearches?hl=en-US&tz=-120&req=%7B%22restriction%22:%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22time%22:%222010-09-10+2010-10-10%22,%22originalTimeRangeForExploreUrl%22:%222010-09-10+2010-10-10%22,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22%22%7D%5D%7D%7D,%22keywordType%22:%22QUERY%22,%22metric%22:%5B%22TOP%22,%22RISING%22%5D,%22trendinessSettings%22:%7B%22compareTime%22:%222010-08-10+2010-09-09%22%7D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D,%22language%22:%22en%22,%22userCountryCode%22:%22US%22%7D&token=TOKEN';
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/relatedsearches?hl=en-US&tz=-120&req=%7B%22restriction%22:%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22time%22:%222010-09-10+2010-10-10%22,%22originalTimeRangeForExploreUrl%22:%222010-09-10+2010-10-10%22%7D,%22keywordType%22:%22QUERY%22,%22metric%22:%5B%22TOP%22,%22RISING%22%5D,%22trendinessSettings%22:%7B%22compareTime%22:%222010-08-10+2010-09-09%22%7D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D,%22language%22:%22en%22,%22userCountryCode%22:%22US%22%7D&token=TOKEN';

/**
* @var SearchRequest
Expand All @@ -40,7 +40,8 @@ class RelatedQueriesSearchTest extends TestCase

public function setUp(): void
{
$this->searchFilter = new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00'));
$this->searchFilter = (new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00')))
->withSearchTerm('_keyword_');
$this->searchRequest = $this->createMock(SearchRequest::class);
$this->exploreSearch = $this->createMock(ExploreSearch::class);
$this->sut = new RelatedQueriesSearch($this->exploreSearch, $this->searchRequest);
Expand Down Expand Up @@ -203,7 +204,6 @@ public function testSearchWillThrowExceptionWhenMissingRequiredKeys(): void
$this->searchRequest
->expects($this->once())
->method('search')
->with(self::SEARCH_URL)
->willReturn(
[
'default' => [
Expand Down Expand Up @@ -234,7 +234,6 @@ public function testSearchWillThrowExceptionWhenInvalidResult(): void
$this->searchRequest
->expects($this->once())
->method('search')
->with(self::SEARCH_URL)
->willReturn(
[
'a' => []
Expand Down
5 changes: 3 additions & 2 deletions tests/Integration/Search/RelatedTopicsSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class RelatedTopicsSearchTest extends TestCase
{
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/relatedsearches?hl=en-US&tz=-120&req=%7B%22restriction%22:%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22time%22:%222010-09-10+2010-10-10%22,%22originalTimeRangeForExploreUrl%22:%222010-09-10+2010-10-10%22,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22%22%7D%5D%7D%7D,%22keywordType%22:%22ENTITY%22,%22metric%22:%5B%22TOP%22,%22RISING%22%5D,%22trendinessSettings%22:%7B%22compareTime%22:%222010-08-10+2010-09-09%22%7D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D,%22language%22:%22en%22,%22userCountryCode%22:%22US%22%7D&token=TOKEN';
private const SEARCH_URL = 'https://trends.google.com/trends/api/widgetdata/relatedsearches?hl=en-US&tz=-120&req=%7B%22restriction%22:%7B%22geo%22:%7B%22country%22:%22US%22%7D,%22time%22:%222010-09-10+2010-10-10%22,%22originalTimeRangeForExploreUrl%22:%222010-09-10+2010-10-10%22,%22complexKeywordsRestriction%22:%7B%22keyword%22:%5B%7B%22type%22:%22BROAD%22,%22value%22:%22_keyword_%22%7D%5D%7D%7D,%22keywordType%22:%22ENTITY%22,%22metric%22:%5B%22TOP%22,%22RISING%22%5D,%22trendinessSettings%22:%7B%22compareTime%22:%222010-08-10+2010-09-09%22%7D,%22requestOptions%22:%7B%22property%22:%22%22,%22backend%22:%22IZG%22,%22category%22:0%7D,%22language%22:%22en%22,%22userCountryCode%22:%22US%22%7D&token=TOKEN';

/**
* @var SearchRequest
Expand All @@ -40,7 +40,8 @@ class RelatedTopicsSearchTest extends TestCase

public function setUp(): void
{
$this->searchFilter = new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00'));
$this->searchFilter = (new SearchFilter(new DateTimeImmutable('2010-10-10 00:00:00')))
->withSearchTerm('_keyword_');
$this->searchRequest = $this->createMock(SearchRequest::class);
$this->exploreSearch = $this->createMock(ExploreSearch::class);
$this->sut = new RelatedTopicsSearch($this->exploreSearch, $this->searchRequest);
Expand Down

0 comments on commit f513ad5

Please sign in to comment.