File tree Expand file tree Collapse file tree 3 files changed +33
-1
lines changed
Expand file tree Collapse file tree 3 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 2828 "friendsofphp/php-cs-fixer" : " ^3.5" ,
2929 "league/flysystem" : " ^3.0" ,
3030 "pestphp/pest" : " ^1.21" ,
31- "spatie/ray" : " ^1.33"
31+ "spatie/ray" : " ^1.33" ,
32+ "symfony/dom-crawler" : " ^6.0"
33+ },
34+ "suggest" : {
35+ "symfony/dom-crawler" : " Required for the SaloonResponse dom() method to parse HTML and XML."
3236 },
3337 "minimum-stability" : " stable" ,
3438 "autoload" : {
Original file line number Diff line number Diff line change 88use Illuminate \Support \Collection ;
99use Psr \Http \Message \StreamInterface ;
1010use Illuminate \Support \Traits \Macroable ;
11+ use Symfony \Component \DomCrawler \Crawler ;
1112use GuzzleHttp \Exception \RequestException ;
1213use Sammyjo20 \Saloon \Exceptions \SaloonRequestException ;
1314
@@ -200,6 +201,19 @@ public function dto(): mixed
200201 return $ this ->dto ;
201202 }
202203
204+ /**
205+ * Parse the HTML or XML body into a Symfony DomCrawler instance.
206+ *
207+ * Requires Symfony Crawler (composer require symfony/dom-crawler)
208+ * @see https://symfony.com/doc/current/components/dom_crawler.html
209+ *
210+ * @return Crawler
211+ */
212+ public function dom (): Crawler
213+ {
214+ return new Crawler ($ this ->body ());
215+ }
216+
203217 /**
204218 * Get a header from the response.
205219 *
Original file line number Diff line number Diff line change 44use Illuminate \Support \Collection ;
55use Sammyjo20 \Saloon \Http \MockResponse ;
66use Sammyjo20 \Saloon \Clients \MockClient ;
7+ use Symfony \Component \DomCrawler \Crawler ;
78use Sammyjo20 \Saloon \Exceptions \SaloonRequestException ;
89use Sammyjo20 \Saloon \Tests \Fixtures \Requests \UserRequest ;
910
197198
198199 expect ($ simpleXml )->toBeInstanceOf (SimpleXMLElement::class);
199200});
201+
202+ test ('the dom method will return a crawler instance ' , function () {
203+ $ dom = '<p>Howdy <i>Partner</i></p> ' ;
204+
205+ $ mockClient = new MockClient ([
206+ new MockResponse ($ dom , 200 ),
207+ ]);
208+
209+ $ response = (new UserRequest ())->send ($ mockClient );
210+
211+ expect ($ response ->dom ())->toBeInstanceOf (Crawler::class);
212+ expect ($ response ->dom ())->toEqual (new Crawler ($ dom ));
213+ });
You can’t perform that action at this time.
0 commit comments