Skip to content

Commit 0cfcafe

Browse files
committed
Use fixtures for scraper tests
1 parent 742962b commit 0cfcafe

30 files changed

+17096
-28
lines changed

app/Scraper/Sites/CocktailParty.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Kami\Cocktail\Scraper\Sites;
66

77
use Kami\RecipeUtils\RecipeIngredient;
8-
use Symfony\Component\DomCrawler\Crawler;
98
use Kami\Cocktail\Scraper\AbstractSite;
9+
use Symfony\Component\DomCrawler\Crawler;
1010

1111
class CocktailParty extends AbstractSite
1212
{

app/Scraper/Sites/DefaultScraper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use Kami\RecipeUtils\AmountValue;
99
use Kami\Cocktail\Scraper\SchemaModel;
1010
use Kami\RecipeUtils\RecipeIngredient;
11-
use Kami\Cocktail\Scraper\Concerns\ReadsLDJson;
1211
use Kami\Cocktail\Scraper\AbstractSite;
12+
use Kami\Cocktail\Scraper\Concerns\ReadsLDJson;
1313
use Kami\Cocktail\Scraper\Concerns\ReadsHTMLSchema;
1414

1515
class DefaultScraper extends AbstractSite

app/Scraper/Sites/KindredCocktails.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use Exception;
88
use Throwable;
9-
use Symfony\Component\DomCrawler\Crawler;
109
use Kami\Cocktail\Scraper\AbstractSite;
10+
use Symfony\Component\DomCrawler\Crawler;
1111

1212
class KindredCocktails extends AbstractSite
1313
{

tests/Scrapers/GenericScraperTest.php

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,65 @@
55
namespace Tests\Scrapers;
66

77
use Tests\TestCase;
8-
use Kami\Cocktail\Scraper\Manager;
8+
use Kami\Cocktail\Scraper\Sites\TuxedoNo2;
9+
use Kami\Cocktail\Scraper\Sites\HausAlpenz;
10+
use Kami\Cocktail\Scraper\Sites\LiberAndCo;
11+
use Kami\Cocktail\Scraper\Sites\PunchDrink;
12+
use Kami\Cocktail\Scraper\Sites\CocktailParty;
13+
use Kami\Cocktail\Scraper\Sites\DiffordsGuide;
14+
use Kami\Cocktail\Scraper\Sites\TheCocktailDB;
915
use PHPUnit\Framework\Attributes\DataProvider;
16+
use Kami\Cocktail\Scraper\Sites\ImbibeMagazine;
17+
use Kami\Cocktail\Scraper\Sites\MakeMeACocktail;
18+
use Kami\Cocktail\Scraper\Sites\KindredCocktails;
19+
use Kami\Cocktail\Scraper\Sites\SteveTheBartender;
20+
use Kami\Cocktail\Scraper\Sites\CocktailsDistilled;
21+
use Kami\Cocktail\Scraper\Sites\EricsCocktailGuide;
1022

1123
class GenericScraperTest extends TestCase
1224
{
13-
#[DataProvider('provideUrls')]
14-
public function testScrape(string $url): void
25+
#[DataProvider('provideFixtures')]
26+
public function testScrapeContent(string $url, string $class): void
1527
{
16-
$this->markTestSkipped('These tests are still too flakey, will be updated when scraping gets refactored.');
28+
$fixtureName = basename(str_replace('\\', '/', $class));
29+
$fixtureContent = file_get_contents(__DIR__ . '/../fixtures/sites/' . $fixtureName . '.html');
1730

18-
// We test simple data here, it's too much work to keep everything in sync with the website changes
19-
$scraper = Manager::scrape($url);
31+
$this->assertNotFalse($fixtureContent, 'Fixture content not found for ' . $class);
32+
33+
$scraper = new $class($url, $fixtureContent);
2034
$result = $scraper->toArray();
21-
$recipe = $result['schema']['recipe'];
22-
$ingredients = $result['schema']['ingredients'];
2335

24-
$this->assertNotEmpty($recipe['name']);
25-
$this->assertNotEmpty($recipe['ingredients']);
26-
$this->assertNotEmpty($ingredients);
36+
// Ensure that the result contains the expected structure for dynamic fields
37+
data_set($result, 'schema.recipe.images.*.uri', 'TEST');
38+
data_set($result, 'schema.ingredients.*._id', 'TEST');
39+
data_set($result, 'schema.recipe.ingredients.*._id', 'TEST');
40+
data_set($result, 'scraper_meta.*._id', 'TEST');
41+
42+
$expected = json_decode(file_get_contents(__DIR__ . '/../fixtures/sites/' . $fixtureName . '.json'), true);
43+
$this->assertNotFalse($fixtureContent, 'Expected content not found for ' . $fixtureName);
44+
45+
$this->assertEquals($expected, $result);
2746
}
2847

2948
/**
3049
* @return array<array<string>>
3150
*/
32-
public static function provideUrls(): array
51+
public static function provideFixtures(): array
3352
{
3453
return [
35-
['https://www.ericscocktailguide.com/recipes/negroni'],
36-
['https://cocktailpartyapp.com/drinks/negroni/'],
37-
['https://cocktailsdistilled.com/recipe/rakia-negroni/'],
38-
['https://www.diffordsguide.com/cocktails/recipe/1392/negroni-cocktail'],
39-
['https://alpenz.com/recipe-ikfCvvprSL.html'],
40-
['https://imbibemagazine.com/recipe/chocolate-strawberry-negroni'],
41-
['https://kindredcocktails.com/cocktail/negroni'],
42-
['https://www.liberandcompany.com/products/strawberry-negroni'],
43-
['https://makemeacocktail.com/cocktail/6781/negroni'],
44-
['https://punchdrink.com/recipes/milano-torino'],
45-
['https://stevethebartender.com.au/oaxacanite-cocktail-recipe'],
46-
['https://www.thecocktaildb.com/drink/11003-Negroni'],
47-
['https://tuxedono2.com/negroni-cocktail-recipe'],
54+
['https://punchdrink.com/recipes/negroni', PunchDrink::class],
55+
['https://www.ericscocktailguide.com/recipes/negroni', EricsCocktailGuide::class],
56+
['https://cocktailpartyapp.com/drinks/negroni/', CocktailParty::class],
57+
['https://imbibemagazine.com/recipe/negroni-recipe/', ImbibeMagazine::class],
58+
['https://cocktailsdistilled.com/recipe/kingston-negroni/', CocktailsDistilled::class],
59+
['https://www.diffordsguide.com/cocktails/recipe/1392/negroni-cocktail', DiffordsGuide::class],
60+
['https://alpenz.com/recipe-pHi4g8Upgz.html', HausAlpenz::class],
61+
['https://kindredcocktails.com/cocktail/negroni', KindredCocktails::class],
62+
['https://www.liberandcompany.com/products/strawberry-negroni', LiberAndCo::class],
63+
['https://makemeacocktail.com/cocktail/6781/negroni', MakeMeACocktail::class],
64+
['https://tuxedono2.com/negroni-cocktail-recipe', TuxedoNo2::class],
65+
['https://www.thecocktaildb.com/drink/11003-Negroni', TheCocktailDB::class],
66+
['https://stevethebartender.com.au/negroni-cocktail-recipe/', SteveTheBartender::class],
4867
];
4968
}
5069
}

tests/fixtures/sites/CocktailParty.html

Lines changed: 1127 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
{
2+
"schema_version": "draft2",
3+
"schema": {
4+
"recipe": {
5+
"_id": "negroni",
6+
"name": "Negroni",
7+
"instructions": "Build in an ice-filled rocks glass \u2013 the order doesn\u2019t matter. Just give it a stir and garnish with an orange twist.",
8+
"created_at": null,
9+
"updated_at": null,
10+
"description": "Cocktail snobs have a bad habit of using the Negroni as the threshold through which all drinkers must pass before they can be considered true connoisseurs. There\u2019s an element of truth to this view; the balance of flavors requires an experienced tongue to appreciate. But make no mistake \u2013 it is a very bitter drink, and it\u2019s not for everyone.\n\nSome folks have a genetic variation which allows them to taste bitter compounds, while other people don\u2019t. Those who can are called \u201csupertasters,\u201d and it\u2019s likely that no amount of epicurean taste-acquiring can make this drink palatable to those so afflicted.",
11+
"source": "https:\/\/cocktailpartyapp.com\/drinks\/negroni\/",
12+
"garnish": null,
13+
"abv": null,
14+
"tags": [
15+
"Simple Cocktails",
16+
"Spirit-Forward Cocktails"
17+
],
18+
"glass": null,
19+
"method": null,
20+
"utensils": [],
21+
"images": [
22+
{
23+
"uri": "TEST",
24+
"sort": 0,
25+
"placeholder_hash": null,
26+
"copyright": "CocktailParty"
27+
}
28+
],
29+
"ingredients": [
30+
{
31+
"_id": "TEST",
32+
"amount": 1.5,
33+
"units": "oz",
34+
"optional": false,
35+
"is_specified": false,
36+
"amount_max": null,
37+
"note": null,
38+
"substitutes": [],
39+
"sort": 1
40+
},
41+
{
42+
"_id": "TEST",
43+
"amount": 1.5,
44+
"units": "oz",
45+
"optional": false,
46+
"is_specified": false,
47+
"amount_max": null,
48+
"note": null,
49+
"substitutes": [],
50+
"sort": 2
51+
},
52+
{
53+
"_id": "TEST",
54+
"amount": 1.5,
55+
"units": "oz",
56+
"optional": false,
57+
"is_specified": false,
58+
"amount_max": null,
59+
"note": null,
60+
"substitutes": [],
61+
"sort": 3
62+
}
63+
]
64+
},
65+
"ingredients": [
66+
{
67+
"_id": "TEST",
68+
"name": "Bitter orange ap\u00e9ritif",
69+
"strength": 0,
70+
"description": null,
71+
"origin": null,
72+
"category": null
73+
},
74+
{
75+
"_id": "TEST",
76+
"name": "Sweet vermouth",
77+
"strength": 0,
78+
"description": null,
79+
"origin": null,
80+
"category": null
81+
},
82+
{
83+
"_id": "TEST",
84+
"name": "Gin",
85+
"strength": 0,
86+
"description": null,
87+
"origin": null,
88+
"category": null
89+
}
90+
]
91+
},
92+
"scraper_meta": [
93+
{
94+
"_id": "TEST",
95+
"source": "1\u00bd parts Bitter orange ap\u00e9ritif"
96+
},
97+
{
98+
"_id": "TEST",
99+
"source": "1\u00bd parts Sweet vermouth"
100+
},
101+
{
102+
"_id": "TEST",
103+
"source": "1\u00bd parts Gin or Aged gin"
104+
}
105+
]
106+
}

tests/fixtures/sites/CocktailsDistilled.html

Lines changed: 641 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"schema_version": "draft2",
3+
"schema": {
4+
"recipe": {
5+
"_id": "kingston-negroni",
6+
"name": "Kingston Negroni",
7+
"instructions": "Combine all ingredients and stir with ice. Strain into an ice-filled rocks glass.",
8+
"created_at": null,
9+
"updated_at": null,
10+
"description": "What happens when Campari and Italian Vermouth meet the sand and sun of the Caribbean? Well, that would be a Kingston Negroni. Overproof Jamaican Rum stands up to the bombastic chocolate and bitter orange notes in the vermouth while drying out the Campari’s richness and tempering its bitterness.",
11+
"source": "https:\/\/cocktailsdistilled.com\/recipe\/kingston-negroni\/",
12+
"garnish": "Orange Slice",
13+
"abv": null,
14+
"tags": [],
15+
"glass": "Rocks",
16+
"method": "Stir",
17+
"utensils": [],
18+
"images": [
19+
{
20+
"uri": "TEST",
21+
"sort": 0,
22+
"placeholder_hash": null,
23+
"copyright": "Cocktails Distilled"
24+
}
25+
],
26+
"ingredients": [
27+
{
28+
"_id": "TEST",
29+
"amount": 30,
30+
"units": "ml",
31+
"optional": false,
32+
"is_specified": false,
33+
"amount_max": null,
34+
"note": null,
35+
"substitutes": [],
36+
"sort": 1
37+
},
38+
{
39+
"_id": "TEST",
40+
"amount": 30,
41+
"units": "ml",
42+
"optional": false,
43+
"is_specified": false,
44+
"amount_max": null,
45+
"note": null,
46+
"substitutes": [],
47+
"sort": 2
48+
},
49+
{
50+
"_id": "TEST",
51+
"amount": 30,
52+
"units": "ml",
53+
"optional": false,
54+
"is_specified": false,
55+
"amount_max": null,
56+
"note": null,
57+
"substitutes": [],
58+
"sort": 3
59+
}
60+
]
61+
},
62+
"ingredients": [
63+
{
64+
"_id": "TEST",
65+
"name": "Jamaican Rum (overproof)",
66+
"strength": 0,
67+
"description": null,
68+
"origin": null,
69+
"category": null
70+
},
71+
{
72+
"_id": "TEST",
73+
"name": "Sweet Vermouth",
74+
"strength": 0,
75+
"description": null,
76+
"origin": null,
77+
"category": null
78+
},
79+
{
80+
"_id": "TEST",
81+
"name": "Campari",
82+
"strength": 0,
83+
"description": null,
84+
"origin": null,
85+
"category": null
86+
}
87+
]
88+
},
89+
"scraper_meta": [
90+
{
91+
"_id": "TEST",
92+
"source": "Jamaican Rum (overproof)"
93+
},
94+
{
95+
"_id": "TEST",
96+
"source": "Sweet Vermouth"
97+
},
98+
{
99+
"_id": "TEST",
100+
"source": "Campari"
101+
}
102+
]
103+
}

tests/fixtures/sites/DiffordsGuide.html

Lines changed: 2713 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)