From 115c012c79480ca3460533f5250df5e6f0ee62c9 Mon Sep 17 00:00:00 2001 From: William Hall Date: Mon, 15 Apr 2024 17:07:45 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- test/BrowserlessTest.php | 53 +++++++++++++++++----------------------- test/ChromeTest.php | 8 +++--- test/TestCase.php | 2 +- 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/composer.json b/composer.json index 969044c..4dffd91 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^9.0", + "phpunit/phpunit": "^10.0", "phpstan/phpstan": "^1.9" }, "scripts": { diff --git a/test/BrowserlessTest.php b/test/BrowserlessTest.php index d7f8893..4716293 100644 --- a/test/BrowserlessTest.php +++ b/test/BrowserlessTest.php @@ -9,7 +9,7 @@ use GuzzleHttp\Psr7\Request; use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\ClientException; - +use GuzzleHttp\Middleware; use SynergiTech\ChromePDF\Browserless; use SynergiTech\ChromePDF\Browserless\APIException; use SynergiTech\ChromePDF\Chrome; @@ -85,37 +85,14 @@ public function test_timeout() public function test_displayHeaderFooter() { - $client = $this->getMockedClient(); + $requests = []; - $client->expects($this->exactly(6)) - ->method('post') - ->withConsecutive( - [ - $this->anything(), - $this->logicalNot($this->hasKeyValue(['json', 'options', 'displayHeaderFooter'])) - ], - [ - $this->anything(), - $this->hasKeyValue(['json', 'options', 'displayHeaderFooter'], $this->isTrue()) - ], - [ - $this->anything(), - $this->hasKeyValue(['json', 'options', 'displayHeaderFooter'], $this->isFalse()) - ], - [ - $this->anything(), - $this->hasKeyValue(['json', 'options', 'displayHeaderFooter'], $this->isTrue()) - ], - [ - $this->anything(), - $this->hasKeyValue(['json', 'options', 'displayHeaderFooter'], $this->isTrue()) - ], - [ - $this->anything(), - $this->hasKeyValue(['json', 'options', 'displayHeaderFooter'], $this->isFalse()) - ] - ) - ->willReturn(new Response()); + $handlerStack = HandlerStack::create(new MockHandler(array_fill(0, 6, new Response()))); + $handlerStack->push(Middleware::history($requests)); + + $client = new Client([ + 'handler' => $handlerStack, + ]); $bl = new Browserless(client: $client); $bl->renderContent('test'); @@ -135,6 +112,20 @@ public function test_displayHeaderFooter() $bl->setFooter(null); $bl->renderContent('test'); + + $this->assertCount(6, $requests); + + $requests = array_map(fn ($r) => json_decode((string) $r['request']->getBody(), true), $requests); + + $options = array_column($requests, 'options'); + + $this->assertArrayNotHasKey('displayHeaderFooter', $options[0]); + + $this->assertTrue($options[1]['displayHeaderFooter']); + $this->assertFalse($options[2]['displayHeaderFooter']); + $this->assertTrue($options[3]['displayHeaderFooter']); + $this->assertTrue($options[4]['displayHeaderFooter']); + $this->assertFalse($options[5]['displayHeaderFooter']); } public function test_header() diff --git a/test/ChromeTest.php b/test/ChromeTest.php index b1e2588..71c3234 100644 --- a/test/ChromeTest.php +++ b/test/ChromeTest.php @@ -20,7 +20,7 @@ private function getMockedProcess() private function getMockedPDF() { return $this->getMockBuilder(Chrome::class) - ->setMethods(['createProcess']) + ->onlyMethods(['createProcess']) ->getMock(); } @@ -40,7 +40,7 @@ public function test_callsSpecifiedBinary() { $pdf = $this->getMockBuilder(Chrome::class) ->setConstructorArgs(['test-pdf-binary']) - ->setMethods(['createProcess']) + ->onlyMethods(['createProcess']) ->getMock(); $pdf->expects($this->once()) @@ -216,7 +216,7 @@ public function test_displayHeaderFooter() public function test_header() { $pdfBuilder = $this->getMockBuilder(Chrome::class) - ->setMethods(['createProcess']); + ->onlyMethods(['createProcess']); $pdf = $pdfBuilder->getMock(); $pdf->expects($this->exactly(2)) @@ -261,7 +261,7 @@ public function test_header() public function test_footer() { $pdfBuilder = $this->getMockBuilder(Chrome::class) - ->setMethods(['createProcess']); + ->onlyMethods(['createProcess']); $pdf = $pdfBuilder->getMock(); $pdf->expects($this->exactly(2)) diff --git a/test/TestCase.php b/test/TestCase.php index 97786c0..8765b82 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -15,7 +15,7 @@ public function hasKeyValue($key, $constraint = null) protected function getMockedClient() { return $this->getMockBuilder(Chrome::class) - ->setMethods(['post']) + ->addMethods(['post']) ->getMock(); } }