Skip to content

Commit

Permalink
ScanEntity no longer extends any Entity class (which has been rem…
Browse files Browse the repository at this point in the history
…oved) and directly implements `ArrayAccess`
  • Loading branch information
mirko-pagliai committed Jan 7, 2024
1 parent 7b23478 commit 64d47d6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### 1.2.0
* requires at least PHP 8.1, CakePHP 5.0 and PHPUnit 10;
* provides its own `phpUri` library;
* `ScanEntity` no longer extends any `Entity` class (which has been removed). For now it implements `ArrayAccess`;
* `ScanEntity` no longer extends any `Entity` class (which has been removed) and directly implements `ArrayAccess`;
* added and fixed typehints, removed deprecations;
* added tests for PHP 8.3.

Expand Down
27 changes: 2 additions & 25 deletions src/ScanEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function __call(string $name, mixed $arguments): mixed
{
if (method_exists(Response::class, $name)) {
$Response = (new Response())
->withHeader('location', $this->get('location'))
->withStatus($this->get('code'));
->withHeader('location', $this['location'])
->withStatus($this['code']);
/** @var callable $name */
$name = [$Response, $name];
}
Expand All @@ -79,29 +79,6 @@ public function __call(string $name, mixed $arguments): mixed
return call_user_func_array($name, $arguments);
}

/**
* Get method.
*
* Alias for `offsetGet()`
* @param string $name Property name
* @return mixed
*/
public function get(string $name): mixed {
return $this->offsetGet($name);
}

/**
* Set method.
*
* Alias for `offsetSet()`
* @param string $name Property name
* @param mixed $value Property value
* @return void
*/
public function set(string $name, mixed $value): void {
$this->offsetSet($name, $value);
}

/**
* @inheritDoc
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Utility/LinkScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function export(?string $filename = null): string
}

if ($this->getConfig('exportOnlyBadResults')) {
$this->ResultScan = new ResultScan($this->ResultScan->filter(fn(ScanEntity $item): bool => $item->get('code') >= 400));
$this->ResultScan = new ResultScan($this->ResultScan->filter(fn(ScanEntity $item): bool => $item['code'] >= 400));
}

$filename = $this->_getAbsolutePath($filename ?: sprintf('results_%s_%s', $this->hostname, $this->startTime));
Expand Down
49 changes: 18 additions & 31 deletions tests/TestCase/ScanEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setUp(): void
{
parent::setUp();

$this->ScanEntity = new ScanEntity([
$this->ScanEntity ??= new ScanEntity([
'code' => 200,
'external' => false,
'location' => 'https://example.com/location',
Expand All @@ -50,24 +50,21 @@ public function setUp(): void
*/
public function testCall(): void
{
$statusCodes = [
200 => true,
301 => false,
404 => false,
];

foreach ($statusCodes as $code => $expectedValue) {
$this->ScanEntity->set('code', $code);
foreach ([
200 => true,
301 => false,
404 => false,
] as $code => $expectedValue) {
$this->ScanEntity['code'] = $code;
$this->assertEquals($expectedValue, $this->ScanEntity->isSuccess());
}
$statusCodes = [
200 => false,
301 => true,
404 => false,
];

foreach ($statusCodes as $code => $expectedValue) {
$this->ScanEntity->set('code', $code);
foreach ([
200 => false,
301 => true,
404 => false,
] as $code => $expectedValue) {
$this->ScanEntity['code'] = $code;
$this->assertEquals($expectedValue, $this->ScanEntity->isRedirect());
}

Expand Down Expand Up @@ -96,20 +93,10 @@ public function testConstruct(): void
*/
public function testOffsetMethods(): void
{
$this->ScanEntity['name'] = 'value';
$this->assertSame('value', $this->ScanEntity['name']);
$this->assertTrue(isset($this->ScanEntity['name']));
unset($this->ScanEntity['name']);
$this->assertFalse(isset($this->ScanEntity['name']));
}

/**
* @test
* @uses \LinkScanner\ScanEntity::get()
* @uses \LinkScanner\ScanEntity::set()
*/
public function testGetAndSetMethods(): void
{
$this->markTestIncomplete();
$this->ScanEntity->key = 'value';
$this->assertSame('value', $this->ScanEntity->key);
$this->assertTrue(isset($this->ScanEntity->key));
unset($this->ScanEntity->key);
$this->assertFalse(isset($this->ScanEntity->key));
}
}
16 changes: 8 additions & 8 deletions tests/TestCase/Utility/LinkScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@ public function testScan(): void
$hostname = get_hostname_from_url($this->fullBaseUrl);

foreach ($LinkScanner->ResultScan as $item) {
$this->assertMatchesRegularExpression(sprintf('/^https?:\/\/%s/', preg_quote($hostname)), $item->get('url'));
$this->assertContains($item->get('code'), [200, 500]);
$this->assertStringStartsWith('text/html', $item->get('type'));
$this->assertMatchesRegularExpression(sprintf('/^https?:\/\/%s/', preg_quote($hostname)), $item['url']);
$this->assertContains($item['code'], [200, 500]);
$this->assertStringStartsWith('text/html', $item['type']);
}

/** @var \LinkScanner\Utility\LinkScanner&\PHPUnit\Framework\MockObject\MockObject $LinkScanner */
Expand Down Expand Up @@ -408,11 +408,11 @@ public function testScanFromTests(): void
$LinkScanner->setConfig('maxDepth', 1)->scan();
$this->assertCount(1, $LinkScanner->ResultScan);
$item = $LinkScanner->ResultScan->first();
$this->assertSame($item->get('code'), 200);
$this->assertFalse($item->get('external'));
$this->assertEmpty($item->get('referer'));
$this->assertStringStartsWith('text/html', $item->get('type'));
$this->assertSame($item->get('url'), 'http://localhost');
$this->assertSame($item['code'], 200);
$this->assertFalse($item['external']);
$this->assertEmpty($item['referer']);
$this->assertStringStartsWith('text/html', $item['type']);
$this->assertSame($item['url'], 'http://localhost');

$LinkScanner = $this->getLinkScannerClientReturnsFromTests();
$LinkScanner->setConfig('exportOnlyBadResults', true)->scan();
Expand Down

0 comments on commit 64d47d6

Please sign in to comment.