Skip to content

Commit

Permalink
[REFACTOR] PHP 8 Syntax, Add Types
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Oct 18, 2023
1 parent c4136e0 commit 5743e48
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 157 deletions.
59 changes: 0 additions & 59 deletions src/FluentDOM/DOM/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,65 +52,6 @@ class Element

private const NAMESPACE_XMLNS = 'http://www.w3.org/2000/xmlns/';

public function __isset(string $name): bool {
return match ($name) {
'nextElementSibling' => $this->getNextElementSibling() !== NULL,
'previousElementSibling' => $this->getPreviousElementSibling() !== NULL,
'firstElementChild' => $this->getFirstElementChild() !== NULL,
'lastElementChild' => $this->getLastElementChild() !== NULL,
'childElementCount' => TRUE,
default => FALSE,
};
}

public function __get(string $name): int|NULL|Element|\DOMNode {
switch ($name) {
case 'nextElementSibling' :
return $this->getNextElementSibling();
case 'previousElementSibling' :
return $this->getPreviousElementSibling();
case 'firstElementChild' :
return $this->getFirstElementChild();
case 'lastElementChild' :
return $this->getLastElementChild();
case 'childElementCount' :
return (int)$this->evaluate('count(*)');
}
throw new UndeclaredPropertyError($this, $name);
}

/**
* @throws \BadMethodCallException
* @throws UndeclaredPropertyError
*/
public function __set(string $name, mixed $value): void {
$this->blockReadOnlyProperties($name);
throw new UndeclaredPropertyError($this, $name);
}

/**
* @param string $name
* @throws \BadMethodCallException
*/
public function __unset(string $name) {
$this->blockReadOnlyProperties($name);
throw new UndeclaredPropertyError($this, $name);
}

/**
* @param string $name
* @throws \BadMethodCallException
*/
private function blockReadOnlyProperties(string $name): void {
switch ($name) {
case 'nextElementSibling' :
case 'previousElementSibling' :
case 'firstElementChild' :
case 'lastElementChild' :
throw new ReadOnlyPropertyError($this, $name);
}
}

/**
* Validate if an attribute exists
*
Expand Down
91 changes: 4 additions & 87 deletions tests/FluentDOM/DOM/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
use FluentDOM\Exceptions\UndeclaredPropertyError;
use FluentDOM\TestCase;

/**
* @covers \FluentDOM\DOM\Element
*/
class ElementTest extends TestCase {

/**
* @covers \FluentDOM\DOM\Element::__toString
*/
public function testMagicMethodToString(): void {
$document = new Document();
$document->appendElement('test', 'success');
Expand All @@ -30,9 +30,6 @@ public function testMagicMethodToString(): void {
);
}

/**
* @covers \FluentDOM\DOM\Element::__get
*/
public function testGetPropertyFirstElementChild(): void {
$document = new Document();
$document->loadXml('<foo>TEXT<bar attr="value"/></foo>');
Expand All @@ -42,27 +39,18 @@ public function testGetPropertyFirstElementChild(): void {
);
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyFirstElementChildExpectingTrue(): void {
$document = new Document();
$document->loadXml('<foo>TEXT<bar attr="value"/></foo>');
$this->assertTrue(isset($document->documentElement->firstElementChild));
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyFirstElementChildExpectingFalse(): void {
$document = new Document();
$document->loadXml('<foo>TEXT</foo>');
$this->assertFalse(isset($document->documentElement->firstElementChild));
}

/**
* @covers \FluentDOM\DOM\Element::__get
*/
public function testGetPropertyLastElementChild(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
Expand All @@ -72,27 +60,18 @@ public function testGetPropertyLastElementChild(): void {
);
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyLastElementChildExpectingTrue(): void {
$document = new Document();
$document->loadXml('<foo>TEXT<bar attr="value"/></foo>');
$this->assertTrue(isset($document->documentElement->lastElementChild));
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyLastElementChildExpectingFalse(): void {
$document = new Document();
$document->loadXml('<foo>TEXT</foo>');
$this->assertFalse(isset($document->documentElement->lastElementChild));
}

/**
* @covers \FluentDOM\DOM\Element::__get
*/
public function testGetPropertyNextElementSibling(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
Expand All @@ -102,27 +81,18 @@ public function testGetPropertyNextElementSibling(): void {
);
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyNextElementSiblingExpectingTrue(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->assertTrue(isset($document->documentElement->firstChild->nextElementSibling));
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyNextElementSiblingExpectingFalse(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->assertFalse(isset($document->documentElement->lastChild->nextElementSibling));
}

/**
* @covers \FluentDOM\DOM\Element::__get
*/
public function testGetPropertyPreviousElementSibling(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
Expand All @@ -132,73 +102,31 @@ public function testGetPropertyPreviousElementSibling(): void {
);
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyPreviousElementSiblingExpectingTrue(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->assertTrue(isset($document->documentElement->lastChild->previousElementSibling));
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyPreviousElementSiblingExpectingFalse(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->assertFalse(isset($document->documentElement->firstChild->previousElementSibling));
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetPropertyChildElementCountExpectingTrue(): void {
$document = new Document();
$document->loadXml('<foo/>');
$this->assertTrue(isset($document->documentElement->childElementCount));
}

/**
* @covers \FluentDOM\DOM\Element::__get
*/
public function testGetInvalidProperty(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->expectException(UndeclaredPropertyError::class);
$this->assertNull($document->documentElement->INVALID_PROPERTY);
}

/**
* @covers \FluentDOM\DOM\Element::__isset
*/
public function testIssetInvalidProperty(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->assertFalse(isset($document->documentElement->INVALID_PROPERTY));
}

/**
* @covers \FluentDOM\DOM\Element::__get
* @covers \FluentDOM\DOM\Element::__set
*/
public function testSetUnknownProperty(): void {
$document = new Document();
$node = $document->appendChild($document->createElement('foo'));
$this->expectException(UndeclaredPropertyError::class);
$node->SOME_PROPERTY = 'success';
}

/**
* @covers \FluentDOM\DOM\Element::__set
* @covers \FluentDOM\DOM\Element::blockReadOnlyProperties
* @dataProvider dataProviderDynamicElementProperties
* @param string $propertyName
*/
public function testSetDynamicPropertyExpectingException(string $propertyName): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->expectErrorMessageContains('Cannot write read-only property');
$this->expectErrorMessageContains('Cannot write');
$document->documentElement->$propertyName = $document->createElement('test');
}

Expand All @@ -211,17 +139,6 @@ public static function dataProviderDynamicElementProperties(): array {
];
}

/**
* @covers \FluentDOM\DOM\Element::__unset
* @covers \FluentDOM\DOM\Element::blockReadOnlyProperties
*/
public function testUnsetUnknownProperty(): void {
$document = new Document();
$document->loadXml('<foo><foo/>TEXT<bar attr="value"/></foo>');
$this->expectException(UndeclaredPropertyError::class);
unset($document->documentElement->SOME_PROPERTY);
}

/**
* @covers \FluentDOM\DOM\Element::getAttribute
*/
Expand Down
11 changes: 0 additions & 11 deletions tests/FluentDOM/DOM/Node/ParentNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,5 @@ public function testAppend(): void {
$document->saveXML()
);
}

public function testSetUnknownProperty(): void {
$document = new Document();
$this->expectException(\ErrorException::class);
$document->UNKNOWN_PROPERTY = 'FOO';
}

public function testGetUnknownProperty(): void {
$document = new Document();
$this->assertNull(@$document->UNKNOWN_PROPERTY);
}
}
}
1 change: 1 addition & 0 deletions tests/FluentDOM/Utility/ResourceWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function testStreamEof(): void {
public function testOpenWithInvalidContext(): void {
$inner = fopen('data://text/plain;base64,'.base64_encode('success'), 'rb');
[$uri] = ResourceWrapper::createContext($inner);
$this->expectException(\ErrorException::class);
$this->assertFalse(@fopen($uri, 'rb', FALSE));
}
}
Expand Down

0 comments on commit 5743e48

Please sign in to comment.