Skip to content

Commit

Permalink
Merge pull request #23 from steverobbins/feature/catalogfix
Browse files Browse the repository at this point in the history
Fixing issue when seo sitemap only has one page
  • Loading branch information
steverobbins committed Mar 19, 2015
2 parents 903f3f6 + f5a733a commit 037933e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
8 changes: 6 additions & 2 deletions src/MGA/Check/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class Catalog
{
const COUNT_PATTERN = '/Items? -?[0-9]+[a-z0-9- ]+ of ([0-9]+)/';
const COUNT_PATTERN = '/class="amount"\>.*(Items? -?[0-9]+[a-z0-9- ]+ of ([0-9]+))|(([0-9]+) Item).*\</';

/**
* Try to figure out how many categories there are in the store
Expand Down Expand Up @@ -54,6 +54,10 @@ protected function countEntity($url, $entity)
$response = $request->fetch($url . 'catalog/seo_sitemap/' . $entity, array(
CURLOPT_FOLLOWLOCATION => true
));
return $request->findMatchInResponse($response, self::COUNT_PATTERN);
$match = $request->findMatchInResponse($response, '/Items? -?[0-9]+[a-z0-9- ]+ of ([0-9]+)/');
if (!$match) {
$match = $request->findMatchInResponse($response, '/([0-9]+) Item\(s\)/');
}
return $match;
}
}
2 changes: 1 addition & 1 deletion src/MGA/Command/ScanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ protected function getSitemapUrl()
}
$this->output
->writeln('<info>Sitemap is declared in robots.txt</info>');
return $sitemap;
return (string) $sitemap;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/MGA/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ public function parseHeader($rawData)
* Parse out the count from the response
*
* @param \stdClass $response
* @return string|boolean
* @param string $pattern
* @param boolean $returnAll
* @return string|array|boolean
*/
public function findMatchInResponse(\stdClass $response, $pattern)
public function findMatchInResponse(\stdClass $response, $pattern, $returnAll = false)
{
if ($response->code == 200) {
if (preg_match($pattern, $response->body, $match)
&& isset($match[1])
&& (isset($match[1]) || $returnAll)
) {
return $match[1];
return $returnAll ? $match : $match[1];
}
}
return false;
Expand Down
18 changes: 16 additions & 2 deletions tests/MGA/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public function testFindMatchInResponseGood()
}

/**
* Test a bed match
* Test a bad match
*/
public function testFindMatchInResponseTrue()
public function testFindMatchInResponseBad()
{
$response = new \stdClass();
$response->code = 200;
Expand All @@ -75,4 +75,18 @@ public function testFindMatchInResponseTrue()
$match = $request->findMatchInResponse($response, '/Hello (foo)!/');
$this->assertSame(false, $match);
}

/**
* Test a good all match
*/
public function testFindMatchInResponseAllGood()
{
$response = new \stdClass();
$response->code = 200;
$response->body = 'Hello world!';

$request = new Request;
$match = $request->findMatchInResponse($response, '/(Hello) (world)!/', true);
$this->assertSame('world', $match[2]);
}
}

0 comments on commit 037933e

Please sign in to comment.