Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/UsesGoutte.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,38 @@ public function clickLink($titleOrUrl): self

return $this;
}

public function statusCode(): int
{
if ($this->currentPage === null) {
throw new \Exception('You can not access the status code before your first navigation using `go`.');
}

return $this->client->getResponse()->getStatusCode();
}

public function isSuccess(): bool
{
return $this->statusCode() >= 200 && $this->statusCode() <= 299;
}

public function isClientError(): bool
{
return $this->statusCode() >= 400 && $this->statusCode() <= 499;
}

public function isServerError(): bool
{
return $this->statusCode() >= 500 && $this->statusCode() <= 599;
}

public function isForbidden(): bool
{
return $this->statusCode() === 403;
}

public function isNotFound(): bool
{
return $this->statusCode() === 404;
}
}
22 changes: 0 additions & 22 deletions tests/NotFoundTest.php

This file was deleted.

65 changes: 65 additions & 0 deletions tests/StatusCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Spekulatius\PHPScraper\Tests;

class StatusCodeTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
*/
public function testAccessErrorBeforeNavigation()
{
$web = new \Spekulatius\PHPScraper\PHPScraper;

$this->expectException(\Exception::class);
$this->expectExceptionMessage('You can not access the status code before your first navigation using `go`.');

$web->statusCode;
}

/**
* @test
*/
public function testOk()
{
$web = new \Spekulatius\PHPScraper\PHPScraper;

// Navigate to the test page: This redirects to phpscraper.de
$web->go('https://phpscraper.de');

// Check the status itself.
$this->assertSame(200, $web->statusCode);

// Check the detailed states.
$this->assertTrue($web->isSuccess);
$this->assertFalse($web->isClientError);
$this->assertFalse($web->isServerError);

// Assert access-helpers
$this->assertFalse($web->isForbidden);
$this->assertFalse($web->isNotFound);
}

/**
* @test
*/
public function testNotFound()
{
$web = new \Spekulatius\PHPScraper\PHPScraper;

// Navigate to the test page which doesn't exist.
$web->go('https://test-pages.phpscraper.de/page-does-not-exist.html');

// Check the status itself.
$this->assertSame(404, $web->statusCode);

// Check the detailed states.
$this->assertFalse($web->isSuccess);
$this->assertTrue($web->isClientError);
$this->assertFalse($web->isServerError);

// Assert access-helpers
$this->assertFalse($web->isForbidden);
$this->assertTrue($web->isNotFound);
}
}