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

Commit 6cbda18

Browse files
authored
bugfix/Infinite-DIC-loop Fix infitie DIC loop (#7)
1 parent 1242893 commit 6cbda18

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

Diff for: src/Scraper/Repositories/Configuration.php

+2-5
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,15 @@ class Configuration
2323
*/
2424
const CACHE_TTL = 30;
2525

26-
public function __construct(Configurator $crawler)
27-
{
28-
$this->configurator = $crawler;
29-
}
30-
3126
public function findByType(string $type): Collection
3227
{
3328
return ConfigurationModel::withType($type)->get();
3429
}
3530

3631
public function calculate(string $type): Collection
3732
{
33+
$this->configurator = $this->configurator ?? resolve(Configurator::class);
34+
3835
$cacheKey = $this->getCacheKey($type);
3936
$config = Cache::get($cacheKey);
4037
if (!$config) {

Diff for: tests/Unit/Scraper/Repositories/ConfigurationTest.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Softonic\LaravelIntelligentScraper\Scraper\Repositories;
44

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

37-
$configurator = \Mockery::mock(Configurator::class);
38-
39-
$configuration = new Configuration($configurator);
38+
$configuration = new Configuration();
4039
$data = $configuration->findByType('post');
4140

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

5352
$configurator = \Mockery::mock(Configurator::class);
53+
App::instance(Configurator::class, $configurator);
5454

55-
$configuration = new Configuration($configurator);
55+
$configuration = new Configuration();
5656
$configuration->calculate('post');
5757
}
5858

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

111111
$configurator = \Mockery::mock(Configurator::class);
112+
App::instance(Configurator::class, $configurator);
112113
$configurator->shouldReceive('configureFromDataset')
113114
->withArgs(function ($posts) {
114115
return 2 == $posts->count();
115116
})
116117
->andReturn($config);
117118

118-
$configuration = new Configuration($configurator);
119+
$configuration = new Configuration();
119120
$configs = $configuration->calculate('post');
120121

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

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

176-
$configuration = new Configuration($configurator);
178+
$configuration = new Configuration();
177179
$configuration->calculate('post');
178180
}
179181

@@ -183,6 +185,7 @@ public function whenRecalculateFailsItShouldThrowAnException()
183185
public function whenCalculateAfterAnotherCalculateItShouldUseThePrecalclatedConfig()
184186
{
185187
$configurator = \Mockery::mock(Configurator::class);
188+
App::instance(Configurator::class, $configurator);
186189
$configurator->shouldReceive('configureFromDataset')
187190
->never();
188191

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

195-
$configuration = new Configuration($configurator);
198+
$configuration = new Configuration();
196199
$this->assertEquals(
197200
$config,
198201
$configuration->calculate('post')

0 commit comments

Comments
 (0)