Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
bugfix/Infinite-DIC-loop Fix infitie DIC loop (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
joskfg authored Oct 10, 2018
1 parent 1242893 commit 6cbda18
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
7 changes: 2 additions & 5 deletions src/Scraper/Repositories/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,15 @@ class Configuration
*/
const CACHE_TTL = 30;

public function __construct(Configurator $crawler)
{
$this->configurator = $crawler;
}

public function findByType(string $type): Collection
{
return ConfigurationModel::withType($type)->get();
}

public function calculate(string $type): Collection
{
$this->configurator = $this->configurator ?? resolve(Configurator::class);

$cacheKey = $this->getCacheKey($type);
$config = Cache::get($cacheKey);
if (!$config) {
Expand Down
17 changes: 10 additions & 7 deletions tests/Unit/Scraper/Repositories/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Softonic\LaravelIntelligentScraper\Scraper\Repositories;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Softonic\LaravelIntelligentScraper\Scraper\Application\Configurator;
use Softonic\LaravelIntelligentScraper\Scraper\Models\Configuration as ConfigurationModel;
Expand Down Expand Up @@ -34,9 +35,7 @@ public function whenRetrieveAllConfigurationItShouldReturnIt()
'xpaths' => '//*[@id="author"]',
]);

$configurator = \Mockery::mock(Configurator::class);

$configuration = new Configuration($configurator);
$configuration = new Configuration();
$data = $configuration->findByType('post');

$this->assertCount(2, $data);
Expand All @@ -51,8 +50,9 @@ public function whenRecalculateButThereIsNotApostDatasetItShouldThrowAnException
$this->expectExceptionMessage('A dataset example is needed to recalculate xpaths for type post.');

$configurator = \Mockery::mock(Configurator::class);
App::instance(Configurator::class, $configurator);

$configuration = new Configuration($configurator);
$configuration = new Configuration();
$configuration->calculate('post');
}

Expand Down Expand Up @@ -109,13 +109,14 @@ public function whenRecalculateItShouldStoreTheNewXpaths()
->with(Configuration::class . '-config-post', $config, Configuration::CACHE_TTL);

$configurator = \Mockery::mock(Configurator::class);
App::instance(Configurator::class, $configurator);
$configurator->shouldReceive('configureFromDataset')
->withArgs(function ($posts) {
return 2 == $posts->count();
})
->andReturn($config);

$configuration = new Configuration($configurator);
$configuration = new Configuration();
$configs = $configuration->calculate('post');

$this->assertEquals($configs[0]['name'], 'title');
Expand Down Expand Up @@ -164,6 +165,7 @@ public function whenRecalculateFailsItShouldThrowAnException()
->andReturnNull();

$configurator = \Mockery::mock(Configurator::class);
App::instance(Configurator::class, $configurator);
$configurator->shouldReceive('configureFromDataset')
->withArgs(function ($posts) {
return 2 == $posts->count();
Expand All @@ -173,7 +175,7 @@ public function whenRecalculateFailsItShouldThrowAnException()
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('Recalculate fail');

$configuration = new Configuration($configurator);
$configuration = new Configuration();
$configuration->calculate('post');
}

Expand All @@ -183,6 +185,7 @@ public function whenRecalculateFailsItShouldThrowAnException()
public function whenCalculateAfterAnotherCalculateItShouldUseThePrecalclatedConfig()
{
$configurator = \Mockery::mock(Configurator::class);
App::instance(Configurator::class, $configurator);
$configurator->shouldReceive('configureFromDataset')
->never();

Expand All @@ -192,7 +195,7 @@ public function whenCalculateAfterAnotherCalculateItShouldUseThePrecalclatedConf
->with(Configuration::class . '-config-post')
->andReturn($config);

$configuration = new Configuration($configurator);
$configuration = new Configuration();
$this->assertEquals(
$config,
$configuration->calculate('post')
Expand Down

0 comments on commit 6cbda18

Please sign in to comment.