From 71bb7ec4b8d2a75e4c66a26752c71090fb9319f1 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 29 Sep 2024 14:15:22 +0200 Subject: [PATCH] build(deps-dev): bump lunr/halo from 0.8.1 to 0.10.0 (#634) Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Sean Molenaar --- composer.json | 2 +- composer.lock | 14 ++-- src/PHPDraft/In/Tests/ApibFileParserTest.php | 41 +++++------- .../Model/Elements/ArrayStructureElement.php | 2 +- .../Model/Elements/BasicStructureElement.php | 4 +- .../Elements/ElementStructureElement.php | 4 +- .../Model/Elements/EnumStructureElement.php | 2 +- .../Tests/ArrayStructureElementTest.php | 7 +- .../Tests/BasicStructureElementTest.php | 14 ++-- .../Tests/ElementStructureElementTest.php | 6 +- .../Tests/EnumStructureElementTest.php | 11 ++- .../Tests/ObjectStructureElementTest.php | 11 +-- .../Elements/Tests/RequestBodyElementTest.php | 14 ++-- src/PHPDraft/Model/Tests/CategoryTest.php | 27 +++----- src/PHPDraft/Model/Tests/HTTPRequestTest.php | 18 ++--- src/PHPDraft/Model/Tests/HTTPResponseTest.php | 12 ++-- .../Tests/HierarchyElementChildTestBase.php | 2 +- .../Model/Tests/HierarchyElementTest.php | 67 ++++++------------- .../Model/Tests/ObjectElementTest.php | 24 ++++++- src/PHPDraft/Model/Tests/ResourceTest.php | 11 ++- src/PHPDraft/Model/Tests/TransitionTest.php | 27 ++++---- src/PHPDraft/Out/TemplateRenderer.php | 9 ++- .../Out/Tests/TemplateRendererTest.php | 6 +- src/PHPDraft/Out/Tests/VersionTest.php | 4 +- src/PHPDraft/Parse/BaseHtmlGenerator.php | 4 +- src/PHPDraft/Parse/Drafter.php | 10 +-- src/PHPDraft/Parse/ExecutionException.php | 4 +- src/PHPDraft/Parse/HtmlGenerator.php | 9 ++- src/PHPDraft/Parse/ResourceException.php | 4 +- .../Parse/Tests/BaseHtmlGeneratorTest.php | 45 ------------- src/PHPDraft/Parse/Tests/BaseParserTest.php | 41 +++++------- src/PHPDraft/Parse/Tests/DrafterAPITest.php | 50 +++++--------- src/PHPDraft/Parse/Tests/DrafterTest.php | 42 +++--------- .../Parse/Tests/HtmlGeneratorTest.php | 9 +-- .../Parse/Tests/ParserFactoryTest.php | 36 +++------- 35 files changed, 238 insertions(+), 355 deletions(-) delete mode 100644 src/PHPDraft/Parse/Tests/BaseHtmlGeneratorTest.php diff --git a/composer.json b/composer.json index 124c287b..d03bd933 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "rize/uri-template": "*" }, "require-dev": { - "lunr/halo": "~0.8.0", + "lunr/halo": "~0.10.0", "phpunit/phpunit": "~10.0", "theseer/autoload": "~1.0", "phing/phing": "~3.0", diff --git a/composer.lock b/composer.lock index 4b9e3018..a5a737a1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2a038d1c8e4da28a90c1e0ba72b967e7", + "content-hash": "b8e5ae8f736d4ff65d6d9cb918af30dc", "packages": [ { "name": "lukasoppermann/http-status", @@ -868,16 +868,16 @@ "packages-dev": [ { "name": "lunr/halo", - "version": "0.8.1", + "version": "0.10.0", "source": { "type": "git", "url": "https://github.com/lunr-php/lunr.halo.git", - "reference": "ce5f1c55a5666b299f670a154a3ec3b476b52d57" + "reference": "995985e039518b2544349c934da7b7c86ffdb18b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/lunr-php/lunr.halo/zipball/ce5f1c55a5666b299f670a154a3ec3b476b52d57", - "reference": "ce5f1c55a5666b299f670a154a3ec3b476b52d57", + "url": "https://api.github.com/repos/lunr-php/lunr.halo/zipball/995985e039518b2544349c934da7b7c86ffdb18b", + "reference": "995985e039518b2544349c934da7b7c86ffdb18b", "shasum": "" }, "require": { @@ -909,9 +909,9 @@ ], "support": { "issues": "https://github.com/lunr-php/lunr.halo/issues", - "source": "https://github.com/lunr-php/lunr.halo/tree/0.8.1" + "source": "https://github.com/lunr-php/lunr.halo/tree/0.10.0" }, - "time": "2023-11-21T15:17:44+00:00" + "time": "2024-08-05T09:50:50+00:00" }, { "name": "myclabs/deep-copy", diff --git a/src/PHPDraft/In/Tests/ApibFileParserTest.php b/src/PHPDraft/In/Tests/ApibFileParserTest.php index 37f92cb6..e4e12f6c 100644 --- a/src/PHPDraft/In/Tests/ApibFileParserTest.php +++ b/src/PHPDraft/In/Tests/ApibFileParserTest.php @@ -19,6 +19,9 @@ */ class ApibFileParserTest extends LunrBaseTest { + + private ApibFileParser $class; + /** * Set up tests. * @@ -26,8 +29,8 @@ class ApibFileParserTest extends LunrBaseTest */ public function setUp(): void { - $this->class = new ApibFileParser(__DIR__ . '/ApibFileParserTest.php'); - $this->reflection = new ReflectionClass('PHPDraft\In\ApibFileParser'); + $this->class = new ApibFileParser(__DIR__ . '/ApibFileParserTest.php'); + $this->baseSetUp($this->class); } /** @@ -36,9 +39,7 @@ public function setUp(): void */ public function testLocationSetup(): void { - $property = $this->reflection->getProperty('location'); - $property->setAccessible(true); - $this->assertSame(__DIR__ . '/', $property->getValue($this->class)); + $this->assertPropertyEquals('location', __DIR__ . '/'); } /** @@ -47,9 +48,7 @@ public function testLocationSetup(): void */ public function testFilenameSetup(): void { - $property = $this->reflection->getProperty('filename'); - $property->setAccessible(true); - $this->assertSame(__DIR__ . '/ApibFileParserTest.php', $property->getValue($this->class)); + $this->assertPropertySame('filename', __DIR__ . '/ApibFileParserTest.php'); } /** @@ -63,9 +62,7 @@ public function testFilenameSetupWrong(): void $this->expectExceptionMessageMatches('/API File not found: .*\/drafter\/non_existing_including_apib/'); $this->expectExceptionCode(1); - $property = $this->reflection->getProperty('filename'); - $property->setAccessible(true); - $property->setValue($this->class, TEST_STATICS . '/drafter/non_existing_including_apib'); + $this->set_reflection_property_value('filename', TEST_STATICS . '/drafter/non_existing_including_apib'); $this->class->parse(); } @@ -75,21 +72,15 @@ public function testFilenameSetupWrong(): void */ public function testParseBasic(): void { - $property = $this->reflection->getProperty('filename'); - $property->setAccessible(true); - $property->setValue($this->class, TEST_STATICS . '/drafter/apib/including.apib'); - $loc_property = $this->reflection->getProperty('location'); - $loc_property->setAccessible(true); - $loc_property->setValue($this->class, TEST_STATICS . '/drafter/apib/'); - - $this->mock_function('curl_exec', function () { - return 'hello'; - }); + $this->set_reflection_property_value('filename', TEST_STATICS . '/drafter/apib/including.apib'); + $this->set_reflection_property_value('location', TEST_STATICS . '/drafter/apib/'); + + + $this->mock_function('curl_exec', fn() => 'hello'); + $this->class->parse(); - $this->unmock_function('curl_exec'); - $full_property = $this->reflection->getProperty('full_apib'); - $full_property->setAccessible(true); + $this->unmock_function('curl_exec'); $text = "FORMAT: 1A\nHOST: https://owner-api.teslamotors.com\n"; $text .= "EXTRA_HOSTS: https://test.owner-api.teslamotors.com\nSOMETHING: INFO\n\n"; @@ -98,7 +89,7 @@ public function testParseBasic(): void $text .= " functionality to monitor and control the Model S remotely.\n\nTEST"; $text .= "\n\n# Hello\nThis is a test.\nhello"; - $this->assertSame($text, $full_property->getValue($this->class)); + $this->assertPropertyEquals('full_apib', $text); $this->assertSame($text, $this->class->__toString()); } diff --git a/src/PHPDraft/Model/Elements/ArrayStructureElement.php b/src/PHPDraft/Model/Elements/ArrayStructureElement.php index b2157c45..de19ec64 100644 --- a/src/PHPDraft/Model/Elements/ArrayStructureElement.php +++ b/src/PHPDraft/Model/Elements/ArrayStructureElement.php @@ -64,7 +64,7 @@ public function __toString(): string $desc = MarkdownExtra::defaultTransform($this->description); } - return "{$this->key}{$type}{$desc}"; + return "$this->key$type$desc"; } $return = ''; diff --git a/src/PHPDraft/Model/Elements/BasicStructureElement.php b/src/PHPDraft/Model/Elements/BasicStructureElement.php index 015f64b6..8dc886f9 100644 --- a/src/PHPDraft/Model/Elements/BasicStructureElement.php +++ b/src/PHPDraft/Model/Elements/BasicStructureElement.php @@ -23,7 +23,7 @@ abstract class BasicStructureElement implements StructureElement * * @var string|null */ - public ?string $type; + public ?string $type = null; /** * Object description. * @@ -65,7 +65,7 @@ abstract class BasicStructureElement implements StructureElement * * @var string[]|null */ - public ?array $deps; + public ?array $deps = null; /** * Parse a JSON object to a structure. diff --git a/src/PHPDraft/Model/Elements/ElementStructureElement.php b/src/PHPDraft/Model/Elements/ElementStructureElement.php index a1238735..27593acc 100644 --- a/src/PHPDraft/Model/Elements/ElementStructureElement.php +++ b/src/PHPDraft/Model/Elements/ElementStructureElement.php @@ -52,8 +52,8 @@ public function __toString(): string { $type = $this->get_element_as_html($this->type); - $desc = is_null($this->description) ? '' : " - {$this->description}"; - $value = is_null($this->value) ? '' : " - {$this->value}"; + $desc = is_null($this->description) ? '' : " - $this->description"; + $value = is_null($this->value) ? '' : " - $this->value"; return '
  • ' . $type . $desc . $value . '
  • '; } diff --git a/src/PHPDraft/Model/Elements/EnumStructureElement.php b/src/PHPDraft/Model/Elements/EnumStructureElement.php index 066f2e8b..15f10f10 100644 --- a/src/PHPDraft/Model/Elements/EnumStructureElement.php +++ b/src/PHPDraft/Model/Elements/EnumStructureElement.php @@ -91,7 +91,7 @@ public function __toString(): string $type = $this->get_element_as_html($this->element); $desc = $this->description === null ? '' : MarkdownExtra::defaultTransform($this->description); - return "{$this->key->value}{$type}{$desc}"; + return "{$this->key->value}$type$desc"; } /** diff --git a/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php index bdaecf8e..8ca65b54 100644 --- a/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/ArrayStructureElementTest.php @@ -26,8 +26,8 @@ class ArrayStructureElementTest extends LunrBaseTest */ public function setUp(): void { - $this->class = new ArrayStructureElement(); - $this->reflection = new \ReflectionClass('PHPDraft\Model\Elements\ArrayStructureElement'); + $this->class = new ArrayStructureElement(); + $this->baseSetUp($this->class); } /** @@ -179,8 +179,7 @@ public static function parseObjectProvider(): array */ public function testNewInstance(): void { - $method = $this->reflection->getMethod('new_instance'); - $method->setAccessible(true); + $method = $this->get_reflection_method('new_instance'); $return = $method->invoke($this->class); $this->assertInstanceOf(ArrayStructureElement::class, $return); } diff --git a/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php index b555cf67..ad9b6223 100644 --- a/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/BasicStructureElementTest.php @@ -22,18 +22,19 @@ */ class BasicStructureElementTest extends LunrBaseTest { + private BasicStructureElement $class; + /** * Set up tests * * @return void - * @throws \ReflectionException */ public function setUp(): void { $this->class = $this->getMockBuilder('\PHPDraft\Model\Elements\BasicStructureElement') ->disableOriginalConstructor() ->getMockForAbstractClass(); - $this->reflection = new ReflectionClass($this->class); + $this->baseSetUp($this->class); } /** @@ -41,9 +42,7 @@ public function setUp(): void */ public function testSetupCorrectly(): void { - $property = $this->reflection->getProperty('element'); - $property->setAccessible(true); - $this->assertNull($property->getValue($this->class)); + $this->assertPropertyEquals('element', NULL); } /** @@ -102,8 +101,7 @@ public function testParseCommonDeps(): void $answer->type = 'cat'; $answer->description = null; - $method = $this->reflection->getMethod('parse_common'); - $method->setAccessible(true); + $method = $this->get_reflection_method('parse_common'); $method->invokeArgs($this->class, [json_decode($json), &$dep]); $this->assertEquals($answer->key, $this->class->key); @@ -124,7 +122,7 @@ public function testParseCommonDeps(): void public function testParseCommon($value, BasicStructureElement $expected_value): void { $dep = []; - $method = $this->get_accessible_reflection_method('parse_common'); + $method = $this->get_reflection_method('parse_common'); $method->invokeArgs($this->class, [$value, &$dep]); $this->assertEquals($expected_value->key, $this->class->key); diff --git a/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php index 8b02450e..10318708 100644 --- a/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/ElementStructureElementTest.php @@ -12,13 +12,15 @@ */ class ElementStructureElementTest extends LunrBaseTest { + private ElementStructureElement $class; + /** * Set up */ public function setUp(): void { - $this->class = new ElementStructureElement(); - $this->reflection = new ReflectionClass('PHPDraft\Model\Elements\ElementStructureElement'); + $this->class = new ElementStructureElement(); + $this->baseSetUp($this->class); } /** diff --git a/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php index 494d39ca..dcb96800 100644 --- a/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/EnumStructureElementTest.php @@ -25,8 +25,8 @@ class EnumStructureElementTest extends LunrBaseTest */ public function setUp(): void { - $this->class = new EnumStructureElement(); - $this->reflection = new \ReflectionClass('PHPDraft\Model\Elements\EnumStructureElement'); + $this->class = new EnumStructureElement(); + $this->baseSetUp($this->class); } /** @@ -34,9 +34,7 @@ public function setUp(): void */ public function testSetupCorrectly(): void { - $property = $this->reflection->getProperty('element'); - $property->setAccessible(true); - $this->assertNull($property->getValue($this->class)); + $this->assertPropertyEquals('element', NULL); } /** @@ -44,8 +42,7 @@ public function testSetupCorrectly(): void */ public function testNewInstance(): void { - $method = $this->reflection->getMethod('new_instance'); - $method->setAccessible(true); + $method = $this->get_reflection_method('new_instance'); $return = $method->invoke($this->class); $this->assertInstanceOf(EnumStructureElement::class, $return); } diff --git a/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php b/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php index 631e42ed..03821d86 100644 --- a/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/ObjectStructureElementTest.php @@ -23,6 +23,8 @@ */ class ObjectStructureElementTest extends LunrBaseTest { + private ObjectStructureElement $class; + /** * Set up tests * @@ -30,8 +32,8 @@ class ObjectStructureElementTest extends LunrBaseTest */ public function setUp(): void { - $this->class = new ObjectStructureElement(); - $this->reflection = new ReflectionClass('PHPDraft\Model\Elements\ObjectStructureElement'); + $this->class = new ObjectStructureElement(); + $this->baseSetUp($this->class); } /** @@ -41,8 +43,7 @@ public function setUp(): void */ public function testNewInstance(): void { - $method = $this->reflection->getMethod('new_instance'); - $method->setAccessible(true); + $method = $this->get_reflection_method('new_instance'); $return = $method->invoke($this->class); $this->assertInstanceOf(ObjectStructureElement::class, $return); } @@ -210,7 +211,7 @@ public function testEmptyParse(): void $deps = []; $return = $this->class->parse(null, $deps); $this->assertInstanceOf(ObjectStructureElement::class, $return); - $object = new \stdClass(); + $object = (object)[]; $object->key = 'key'; $return = $this->class->parse($object, $deps); $this->assertInstanceOf(ObjectStructureElement::class, $return); diff --git a/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php b/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php index e2414295..e62e4ad8 100644 --- a/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php +++ b/src/PHPDraft/Model/Elements/Tests/RequestBodyElementTest.php @@ -29,7 +29,7 @@ class RequestBodyElementTest extends LunrBaseTest public function setUp(): void { $this->class = new RequestBodyElement(); - $this->reflection = new \ReflectionClass('PHPDraft\Model\Elements\RequestBodyElement'); + $this->baseSetUp($this->class); } /** @@ -37,9 +37,7 @@ public function setUp(): void */ public function testSetupCorrectly(): void { - $property = $this->reflection->getProperty('element'); - $property->setAccessible(true); - $this->assertNull($property->getValue($this->class)); + $this->assertPropertyEquals('element', NULL); } /** @@ -47,9 +45,9 @@ public function testSetupCorrectly(): void */ public function testNewInstance(): void { - $method = $this->reflection->getMethod('new_instance'); - $method->setAccessible(true); + $method = $this->get_reflection_method('new_instance'); $return = $method->invoke($this->class); + $this->assertInstanceOf(RequestBodyElement::class, $return); } @@ -117,7 +115,7 @@ public function testPrintJsonArray(): void * @param string $object JSON Object * @param ObjectStructureElement $expected Expected Object output * - * @covers \PHPDraft\Model\Elements\ObjectStructureElement::parse + * @covers \PHPDraft\Model\Elements\ObjectStructureElement::parse */ public function testSuccessfulParse(string $object, ObjectStructureElement $expected): void { @@ -272,7 +270,7 @@ public function testEmptyParse(): void $deps = []; $return = $this->class->parse(null, $deps); $this->assertInstanceOf(ObjectStructureElement::class, $return); - $object = new \stdClass(); + $object = (object) []; $object->key = 'key'; $return = $this->class->parse($object, $deps); $this->assertInstanceOf(ObjectStructureElement::class, $return); diff --git a/src/PHPDraft/Model/Tests/CategoryTest.php b/src/PHPDraft/Model/Tests/CategoryTest.php index e1a958dd..6ac956fb 100644 --- a/src/PHPDraft/Model/Tests/CategoryTest.php +++ b/src/PHPDraft/Model/Tests/CategoryTest.php @@ -18,14 +18,16 @@ */ class CategoryTest extends HierarchyElementChildTestBase { + private Category $class; + /** * Set up */ public function setUp(): void { parent::setUp(); - $this->class = new Category(); - $this->reflection = new ReflectionClass('\PHPDraft\Model\Category'); + $this->class = new Category(); + $this->baseSetUp($this->class); } /** @@ -33,9 +35,8 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->class); unset($this->parent); - unset($this->reflection); + parent::tearDown(); } /** @@ -70,7 +71,7 @@ public function testParseIsCalled(): void { $this->set_reflection_property_value('parent', $this->parent); - $obj = new \stdClass(); + $obj = (object) []; $obj->content = []; $this->class->parse($obj); @@ -91,9 +92,7 @@ public function testParseIsCalledResource(): void $this->assertSame($this->parent, $this->get_reflection_property_value('parent')); - $cproperty = $this->reflection->getProperty('children'); - $cproperty->setAccessible(true); - $this->assertNotEmpty($cproperty->getValue($this->class)); + $this->assertNotEmpty($this->get_reflection_property_value('children')); } /** @@ -108,10 +107,7 @@ public function testParseIsCalledObject(): void $this->class->parse(json_decode($json)); $this->assertSame($this->parent, $this->get_reflection_property_value('parent')); - - $s_property = $this->reflection->getProperty('structures'); - $s_property->setAccessible(true); - $this->assertNotEmpty($s_property->getValue($this->class)); + $this->assertNotEmpty($this->get_reflection_property_value('structures')); } /** @@ -172,10 +168,7 @@ public function testParseIsCalledObjectMetaID(): void $this->class->parse(json_decode($json)); $this->assertSame($this->parent, $this->get_reflection_property_value('parent')); - - $s_property = $this->reflection->getProperty('structures'); - $s_property->setAccessible(true); - $this->assertNotEmpty($s_property->getValue($this->class)); + $this->assertNotEmpty($this->get_reflection_property_value('structures')); } /** @@ -204,7 +197,7 @@ public function testGetHrefIsCalledWithParent(): void $this->parent->expects($this->once()) ->method('get_href') - ->will($this->returnValue('hello')); + ->willReturn('hello'); $result = $this->class->get_href(); diff --git a/src/PHPDraft/Model/Tests/HTTPRequestTest.php b/src/PHPDraft/Model/Tests/HTTPRequestTest.php index 114e2fae..2bbf98a1 100644 --- a/src/PHPDraft/Model/Tests/HTTPRequestTest.php +++ b/src/PHPDraft/Model/Tests/HTTPRequestTest.php @@ -21,6 +21,9 @@ */ class HTTPRequestTest extends LunrBaseTest { + + private HTTPRequest $class; + /** * Mock of the parent class * @@ -33,17 +36,17 @@ class HTTPRequestTest extends LunrBaseTest */ public function setUp(): void { - $parent = $this->createMock('\PHPDraft\Model\Transition'); + $parent = $this->getMockBuilder('\PHPDraft\Model\Transition') + ->disableOriginalConstructor() + ->getMock(); $this->parent = $this->getMockBuilder('\PHPDraft\Model\Transition') ->disableOriginalConstructor() ->getMock(); - $this->mock_function('microtime', function () { - return '1000'; - }); + $this->mock_function('microtime', fn() => '1000'); $this->class = new HTTPRequest($parent); $this->unmock_function('microtime'); - $this->reflection = new ReflectionClass('PHPDraft\Model\HTTPRequest'); + $this->baseSetUp($this->class); } /** @@ -51,8 +54,7 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->class); - unset($this->reflection); + parent::tearDown(); } /** @@ -205,7 +207,7 @@ public function testGetCurlCommandStructBodyFilled(): void $struct_ar->expects($this->once()) ->method('print_request') ->with(null) - ->will($this->returnValue('TEST')); + ->willReturn('TEST'); $struct->value = [ $struct_ar ]; $this->set_reflection_property_value('struct', $struct); diff --git a/src/PHPDraft/Model/Tests/HTTPResponseTest.php b/src/PHPDraft/Model/Tests/HTTPResponseTest.php index a2b49ceb..7065bd9c 100644 --- a/src/PHPDraft/Model/Tests/HTTPResponseTest.php +++ b/src/PHPDraft/Model/Tests/HTTPResponseTest.php @@ -22,6 +22,9 @@ */ class HTTPResponseTest extends LunrBaseTest { + + private HTTPResponse $class; + /** * Mock of the parent class * @@ -41,14 +44,16 @@ class HTTPResponseTest extends LunrBaseTest */ public function setUp(): void { - $this->parent_transition = $this->createMock('\PHPDraft\Model\Transition'); + $this->parent_transition = $this->getMockBuilder('\PHPDraft\Model\Transition') + ->disableOriginalConstructor() + ->getMock(); $this->parent = $this->getMockBuilder('\PHPDraft\Model\Transition') ->disableOriginalConstructor() ->getMock(); $this->mock_function('microtime', fn() => '1000'); $this->class = new HTTPResponse($this->parent_transition); $this->unmock_function('microtime'); - $this->reflection = new ReflectionClass('\PHPDraft\Model\HTTPResponse'); + $this->baseSetUp($this->class); } /** @@ -56,8 +61,7 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->class); - unset($this->reflection); + parent::tearDown(); } /** diff --git a/src/PHPDraft/Model/Tests/HierarchyElementChildTestBase.php b/src/PHPDraft/Model/Tests/HierarchyElementChildTestBase.php index adb60ee4..58120065 100644 --- a/src/PHPDraft/Model/Tests/HierarchyElementChildTestBase.php +++ b/src/PHPDraft/Model/Tests/HierarchyElementChildTestBase.php @@ -24,7 +24,7 @@ abstract class HierarchyElementChildTestBase extends LunrBaseTest * * @var HierarchyElement|MockObject */ - protected $parent; + protected HierarchyElement|MockObject $parent; public function setUp(): void { diff --git a/src/PHPDraft/Model/Tests/HierarchyElementTest.php b/src/PHPDraft/Model/Tests/HierarchyElementTest.php index 8cc47925..56f97932 100644 --- a/src/PHPDraft/Model/Tests/HierarchyElementTest.php +++ b/src/PHPDraft/Model/Tests/HierarchyElementTest.php @@ -20,6 +20,8 @@ */ class HierarchyElementTest extends LunrBaseTest { + private HierarchyElement $class; + /** * Mock of the parent class * @@ -37,7 +39,7 @@ public function setUp(): void ->disableOriginalConstructor() ->getMock(); $this->class = $this->getMockForAbstractClass('PHPDraft\Model\HierarchyElement'); - $this->reflection = new ReflectionClass('PHPDraft\Model\HierarchyElement'); + $this->baseSetUp($this->class); } /** @@ -45,8 +47,7 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->class); - unset($this->reflection); + parent::tearDown(); } /** @@ -54,9 +55,7 @@ public function tearDown(): void */ public function testSetupCorrectly(): void { - $property = $this->reflection->getProperty('parent'); - $property->setAccessible(true); - $this->assertNull($property->getValue($this->class)); + $this->assertPropertyEquals('parent', NULL); } /** @@ -64,19 +63,14 @@ public function testSetupCorrectly(): void */ public function testParseIsCalled(): void { - $property = $this->reflection->getProperty('parent'); - $property->setAccessible(true); - $property->setValue($this->class, $this->parent); + $this->set_reflection_property_value('parent', $this->parent); $obj = '{"meta":{"title":"TEST"}, "content":""}'; $this->class->parse(json_decode($obj)); - $this->assertSame($this->parent, $property->getValue($this->class)); - - $href_property = $this->reflection->getProperty('title'); - $href_property->setAccessible(true); - $this->assertSame('TEST', $href_property->getValue($this->class)); + $this->assertPropertySame('parent', $this->parent); + $this->assertPropertySame('title', 'TEST'); } /** @@ -84,23 +78,15 @@ public function testParseIsCalled(): void */ public function testParseIsCalledLoop(): void { - $property = $this->reflection->getProperty('parent'); - $property->setAccessible(true); - $property->setValue($this->class, $this->parent); + $this->set_reflection_property_value('parent', $this->parent); $obj = '{"meta":{"title":"TEST"}, "content":[{"element":"copy", "content":"hello"}]}'; $this->class->parse(json_decode($obj)); - $this->assertSame($this->parent, $property->getValue($this->class)); - - $href_property = $this->reflection->getProperty('title'); - $href_property->setAccessible(true); - $this->assertSame('TEST', $href_property->getValue($this->class)); - - $href_property = $this->reflection->getProperty('description'); - $href_property->setAccessible(true); - $this->assertSame('hello', $href_property->getValue($this->class)); + $this->assertPropertySame('parent', $this->parent); + $this->assertPropertySame('title', 'TEST'); + $this->assertPropertySame('description', 'hello'); } /** @@ -108,23 +94,15 @@ public function testParseIsCalledLoop(): void */ public function testParseIsCalledSlice(): void { - $property = $this->reflection->getProperty('parent'); - $property->setAccessible(true); - $property->setValue($this->class, $this->parent); + $this->set_reflection_property_value('parent', $this->parent); $obj = '{"meta":{"title":"TEST"}, "content":[{"element":"copy", "content":"hello"}, {"element":"test", "content":"hello"}]}'; $this->class->parse(json_decode($obj)); - $this->assertSame($this->parent, $property->getValue($this->class)); - - $href_property = $this->reflection->getProperty('title'); - $href_property->setAccessible(true); - $this->assertSame('TEST', $href_property->getValue($this->class)); - - $href_property = $this->reflection->getProperty('description'); - $href_property->setAccessible(true); - $this->assertSame('hello', $href_property->getValue($this->class)); + $this->assertPropertySame('parent', $this->parent); + $this->assertPropertySame('title', 'TEST'); + $this->assertPropertySame('description', 'hello'); } @@ -138,7 +116,7 @@ public function testGetHrefIsCalledWithParent(): void $this->parent->expects($this->once()) ->method('get_href') - ->will($this->returnValue('hello')); + ->willReturn('hello'); $result = $this->class->get_href(); @@ -161,17 +139,12 @@ public function testGetHrefIsCalledWithoutParent(): void */ public function testGetHrefIsCalledWithTitleWithSpaces(): void { - $property = $this->reflection->getProperty('title'); - $property->setAccessible(true); - $property->setValue($this->class, 'some title'); - - $parent_property = $this->reflection->getProperty('parent'); - $parent_property->setAccessible(true); - $parent_property->setValue($this->class, $this->parent); + $this->set_reflection_property_value('title', 'some title'); + $this->set_reflection_property_value('parent', $this->parent); $this->parent->expects($this->once()) ->method('get_href') - ->will($this->returnValue('hello')); + ->willReturn('hello'); $result = $this->class->get_href(); diff --git a/src/PHPDraft/Model/Tests/ObjectElementTest.php b/src/PHPDraft/Model/Tests/ObjectElementTest.php index fe15054c..817a172f 100644 --- a/src/PHPDraft/Model/Tests/ObjectElementTest.php +++ b/src/PHPDraft/Model/Tests/ObjectElementTest.php @@ -10,12 +10,34 @@ namespace PHPDraft\Model\Tests; use Lunr\Halo\LunrBaseTest; +use PHPDraft\Model\Elements\ObjectStructureElement; /** * Class ObjectElementTest */ class ObjectElementTest extends LunrBaseTest { + + private ObjectStructureElement $class; + + /** + * Set up + */ + public function setUp(): void + { + $this->class = new ObjectStructureElement(); + $this->baseSetUp($this->class); + } + + /** + * Tear down + */ + public function tearDown(): void + { + unset($this->parent); + parent::tearDown(); + } + /** * @covers \PHPDraft\Model\Elements\ObjectStructureElement */ @@ -61,7 +83,7 @@ public function testValueSetup(): void */ public function testStatusSetup(): void { - $this->assertSame(null, $this->class->status); + $this->assertSame('', $this->class->status); } /** diff --git a/src/PHPDraft/Model/Tests/ResourceTest.php b/src/PHPDraft/Model/Tests/ResourceTest.php index 2c97cd0c..1447c3f9 100644 --- a/src/PHPDraft/Model/Tests/ResourceTest.php +++ b/src/PHPDraft/Model/Tests/ResourceTest.php @@ -21,6 +21,8 @@ */ class ResourceTest extends LunrBaseTest { + private Resource $class; + /** * Mock of the parent class * @@ -38,7 +40,7 @@ public function setUp(): void $this->parent->href = null; $this->class = new Resource($this->parent); - $this->reflection = new ReflectionClass('PHPDraft\Model\Resource'); + $this->baseSetUp($this->class); } /** @@ -46,8 +48,7 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->class); - unset($this->reflection); + parent::tearDown(); } /** @@ -69,9 +70,7 @@ public function testParseIsCalled(): void $this->class->parse(json_decode($obj)); - $href_property = $this->reflection->getProperty('href'); - $href_property->setAccessible(true); - $this->assertSame('something', $href_property->getValue($this->class)); + $this->assertPropertyEquals('href', 'something'); } /** diff --git a/src/PHPDraft/Model/Tests/TransitionTest.php b/src/PHPDraft/Model/Tests/TransitionTest.php index bafd8688..445fe050 100644 --- a/src/PHPDraft/Model/Tests/TransitionTest.php +++ b/src/PHPDraft/Model/Tests/TransitionTest.php @@ -18,6 +18,8 @@ */ class TransitionTest extends HierarchyElementChildTestBase { + private Transition $class; + /** * Set up */ @@ -25,9 +27,11 @@ public function setUp(): void { parent::setUp(); - $this->parent = $this->createMock('\PHPDraft\Model\Resource'); + $this->parent = $this->getMockBuilder('\PHPDraft\Model\Resource') + ->disableOriginalConstructor() + ->getMock(); $this->class = new Transition($this->parent); - $this->reflection = new ReflectionClass('PHPDraft\Model\Transition'); + $this->baseSetUp($this->class); } /** @@ -35,8 +39,7 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->class); - unset($this->reflection); + parent::tearDown(); unset($this->parent); } @@ -205,11 +208,10 @@ public function testParseIsCalledIsArrayContentRequestList(): void ->getMock(); $mock_req->expects($this->once()) ->method('is_equal_to') - ->will($this->returnValue(true)); + ->willReturn(true); + $requests = [$mock_req]; - $req_property = $this->reflection->getProperty('requests'); - $req_property->setAccessible(true); - $req_property->setValue($this->class, $requests); + $this->set_reflection_property_value('requests', $requests); $obj = '{"element": "dataStructure", "attributes":{"href":"something", "data":{"content":{"hello":"world"}}}, "content":[{"element":"123", "content":[{"element":"httpRequest", "attributes":{"method":"TEST"}}]}]}'; @@ -240,11 +242,10 @@ public function testGetCurlCommandKey(): void $mock_req->expects($this->once()) ->method('get_curl_command') ->with('https://ur.l', []) - ->will($this->returnValue('curl_command')); + ->willReturn('curl_command'); + $requests = [$mock_req]; - $req_property = $this->reflection->getProperty('requests'); - $req_property->setAccessible(true); - $req_property->setValue($this->class, $requests); + $this->set_reflection_property_value('requests', $requests); $return = $this->class->get_curl_command('https://ur.l'); @@ -371,7 +372,7 @@ public function testBuildURLVars(): void $var1->expects($this->once()) ->method('string_value') - ->will($this->returnValue('STRING')); + ->willReturn('STRING'); $var1->key = $key1; diff --git a/src/PHPDraft/Out/TemplateRenderer.php b/src/PHPDraft/Out/TemplateRenderer.php index 7ad6a025..c9a503bb 100644 --- a/src/PHPDraft/Out/TemplateRenderer.php +++ b/src/PHPDraft/Out/TemplateRenderer.php @@ -20,6 +20,9 @@ use PHPDraft\Model\Elements\ObjectStructureElement; use PHPDraft\Parse\ExecutionException; use Twig\Environment; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; use Twig\Extra\Markdown\DefaultMarkdown; use Twig\Extra\Markdown\MarkdownExtension; use Twig\Extra\Markdown\MarkdownRuntime; @@ -55,9 +58,9 @@ public function __construct(string $template, ?string $image) * * @throws ExecutionException When template is not found * - * @throws \Twig\Error\LoaderError - * @throws \Twig\Error\RuntimeError - * @throws \Twig\Error\SyntaxError + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ public function get(object $object): string { diff --git a/src/PHPDraft/Out/Tests/TemplateRendererTest.php b/src/PHPDraft/Out/Tests/TemplateRendererTest.php index 72863cad..ad62eb82 100644 --- a/src/PHPDraft/Out/Tests/TemplateRendererTest.php +++ b/src/PHPDraft/Out/Tests/TemplateRendererTest.php @@ -22,7 +22,7 @@ class TemplateRendererTest extends LunrBaseTest /** * @var TemplateRenderer */ - protected $class; + protected TemplateRenderer $class; /** * Set up tests @@ -30,8 +30,8 @@ class TemplateRendererTest extends LunrBaseTest */ public function setUp(): void { - $this->class = new TemplateRenderer('default', 'none'); - $this->reflection = new \ReflectionClass('PHPDraft\Out\TemplateRenderer'); + $this->class = new TemplateRenderer('default', 'none'); + $this->baseSetUp($this->class); } /** diff --git a/src/PHPDraft/Out/Tests/VersionTest.php b/src/PHPDraft/Out/Tests/VersionTest.php index 7a75620b..6f0bc1f6 100644 --- a/src/PHPDraft/Out/Tests/VersionTest.php +++ b/src/PHPDraft/Out/Tests/VersionTest.php @@ -37,9 +37,7 @@ public function setUp(): void public function testReleaseIDIsNull(): void { $this->constant_redefine('VERSION', '0'); - $this->mock_function('exec', function () { - return '12'; - }); + $this->mock_function('exec', fn() => '12'); $return = $this->class->release_id(); $this->assertSame('12', $return); $this->unmock_function('exec'); diff --git a/src/PHPDraft/Parse/BaseHtmlGenerator.php b/src/PHPDraft/Parse/BaseHtmlGenerator.php index ab3e25b7..b4f05d67 100644 --- a/src/PHPDraft/Parse/BaseHtmlGenerator.php +++ b/src/PHPDraft/Parse/BaseHtmlGenerator.php @@ -12,9 +12,7 @@ namespace PHPDraft\Parse; -use PHPDraft\Out\BaseTemplateRenderer; -use stdClass; -use \Stringable; +use Stringable; abstract class BaseHtmlGenerator implements Stringable { diff --git a/src/PHPDraft/Parse/Drafter.php b/src/PHPDraft/Parse/Drafter.php index 452cd86a..1b1b92e3 100644 --- a/src/PHPDraft/Parse/Drafter.php +++ b/src/PHPDraft/Parse/Drafter.php @@ -13,6 +13,8 @@ namespace PHPDraft\Parse; use PHPDraft\In\ApibFileParser; +use RuntimeException; +use UnexpectedValueException; class Drafter extends BaseParser { @@ -35,7 +37,7 @@ public function init(ApibFileParser $apib): BaseParser parent::init($apib); $loc = self::location(); if ($loc === false) { - throw new \UnexpectedValueException("Could not find drafter location!"); + throw new UnexpectedValueException("Could not find drafter location!"); } $this->drafter = $loc; @@ -47,7 +49,7 @@ public function init(ApibFileParser $apib): BaseParser * * @return false|string */ - public static function location() + public static function location(): false|string { $returnVal = shell_exec('which drafter 2> /dev/null'); $returnVal = preg_replace('/^\s+|\n|\r|\s+$/m', '', $returnVal); @@ -62,10 +64,10 @@ public static function location() */ protected function parse(): void { - shell_exec("{$this->drafter} {$this->tmp_dir}/index.apib -f json -o {$this->tmp_dir}/index.json 2> /dev/null"); + shell_exec("$this->drafter $this->tmp_dir/index.apib -f json -o $this->tmp_dir/index.json 2> /dev/null"); $content = file_get_contents($this->tmp_dir . '/index.json'); if (!is_string($content)) { - throw new \RuntimeException('Could not read intermediary APIB file!'); + throw new RuntimeException('Could not read intermediary APIB file!'); } $this->json = json_decode($content); diff --git a/src/PHPDraft/Parse/ExecutionException.php b/src/PHPDraft/Parse/ExecutionException.php index b5f81c33..235097f3 100644 --- a/src/PHPDraft/Parse/ExecutionException.php +++ b/src/PHPDraft/Parse/ExecutionException.php @@ -12,11 +12,13 @@ namespace PHPDraft\Parse; +use Exception; + /** * Class ExecutionException. * * @package Parse */ -class ExecutionException extends \Exception +class ExecutionException extends Exception { } diff --git a/src/PHPDraft/Parse/HtmlGenerator.php b/src/PHPDraft/Parse/HtmlGenerator.php index 3dc813ce..27a9f15b 100644 --- a/src/PHPDraft/Parse/HtmlGenerator.php +++ b/src/PHPDraft/Parse/HtmlGenerator.php @@ -13,6 +13,9 @@ namespace PHPDraft\Parse; use PHPDraft\Out\TemplateRenderer; +use Twig\Error\LoaderError; +use Twig\Error\RuntimeError; +use Twig\Error\SyntaxError; /** * Class HtmlGenerator. @@ -30,9 +33,9 @@ class HtmlGenerator extends BaseHtmlGenerator * @return void HTML template to display * * @throws ExecutionException As a runtime exception - * @throws \Twig\Error\LoaderError - * @throws \Twig\Error\RuntimeError - * @throws \Twig\Error\SyntaxError + * @throws LoaderError + * @throws RuntimeError + * @throws SyntaxError */ public function build_html(string $template = 'default', ?string $image = null, ?string $css = null, ?string $js = null): void { diff --git a/src/PHPDraft/Parse/ResourceException.php b/src/PHPDraft/Parse/ResourceException.php index 4375c755..b4c6e5b2 100644 --- a/src/PHPDraft/Parse/ResourceException.php +++ b/src/PHPDraft/Parse/ResourceException.php @@ -12,11 +12,13 @@ namespace PHPDraft\Parse; +use RuntimeException; + /** * Class ResourceException. * * @package Parse */ -class ResourceException extends \RuntimeException +class ResourceException extends RuntimeException { } diff --git a/src/PHPDraft/Parse/Tests/BaseHtmlGeneratorTest.php b/src/PHPDraft/Parse/Tests/BaseHtmlGeneratorTest.php deleted file mode 100644 index 74c6c98e..00000000 --- a/src/PHPDraft/Parse/Tests/BaseHtmlGeneratorTest.php +++ /dev/null @@ -1,45 +0,0 @@ -class = $this->getMockForAbstractClass('PHPDraft\Parse\BaseHtmlGenerator'); - $this->reflection = new ReflectionClass('PHPDraft\Parse\BaseHtmlGenerator'); - } - - /** - * Tear down - */ - public function tearDown(): void - { - unset($this->class); - unset($this->reflection); - } - - /** - * * @covers \PHPDraft\Parse\BaseHtmlGenerator::init - */ - public function testInit(): void - { - $data = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')); - $init = $this->class->init($data); - - $this->assertPropertySame('object', $data); - } -} diff --git a/src/PHPDraft/Parse/Tests/BaseParserTest.php b/src/PHPDraft/Parse/Tests/BaseParserTest.php index 19a97d6f..a3e57f16 100644 --- a/src/PHPDraft/Parse/Tests/BaseParserTest.php +++ b/src/PHPDraft/Parse/Tests/BaseParserTest.php @@ -32,12 +32,8 @@ class BaseParserTest extends LunrBaseTest */ public function setUp(): void { - $this->mock_function('sys_get_temp_dir', function () { - return TEST_STATICS; - }); - $this->mock_function('shell_exec', function () { - return "/some/dir/drafter\n"; - }); + $this->mock_function('sys_get_temp_dir', fn() => TEST_STATICS); + $this->mock_function('shell_exec', fn() => "/some/dir/drafter\n"); $this->parser = $this->getMockBuilder('\PHPDraft\In\ApibFileParser') ->disableOriginalConstructor() @@ -48,7 +44,7 @@ public function setUp(): void $this->parser->set_apib_content(file_get_contents(TEST_STATICS . '/drafter/apib/index.apib')); $this->class->init($this->parser); - $this->reflection = new ReflectionClass($this->class); + $this->baseSetUp($this->class); $this->unmock_function('shell_exec'); $this->unmock_function('sys_get_temp_dir'); @@ -82,8 +78,8 @@ public function testSetupCorrectly(): void public function testParseToJSON(): void { $this->class->expects($this->once()) - ->method('parse') - ->will($this->returnValue(null)); + ->method('parse'); + $this->class->json = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')); $this->class->parseToJson(); $this->assertEquals(json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')), $this->class->json); @@ -97,8 +93,8 @@ public function testParseToJSON(): void public function testParseToJSONMkDir(): void { $this->class->expects($this->once()) - ->method('parse') - ->will($this->returnValue(null)); + ->method('parse'); + $this->class->json = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')); $this->class->parseToJson(); $this->assertEquals(json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')), $this->class->json); @@ -111,19 +107,19 @@ public function testParseToJSONMkDir(): void */ public function testParseToJSONMkTmp(): void { - $tmp_dir = dirname(dirname(TEST_STATICS)) . '/build/tmp'; + $tmp_dir = dirname(TEST_STATICS, 2) . '/build/tmp'; if (file_exists($tmp_dir . DIRECTORY_SEPARATOR . 'index.apib')) { unlink($tmp_dir . DIRECTORY_SEPARATOR . 'index.apib'); } if (file_exists($tmp_dir)) { rmdir($tmp_dir); } - $property = $this->reflection->getProperty('tmp_dir'); - $property->setAccessible(true); - $property->setValue($this->class, $tmp_dir); + + $this->set_reflection_property_value('tmp_dir', $tmp_dir); + $this->class->expects($this->once()) - ->method('parse') - ->will($this->returnValue(null)); + ->method('parse'); + $this->class->json = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')); $this->class->parseToJson(); $this->assertDirectoryExists($tmp_dir); @@ -138,19 +134,14 @@ public function testParseToJSONMkTmp(): void public function testParseToJSONWithInvalidJSON(): void { $this->class->expects($this->once()) - ->method('parse') - ->will($this->returnValue(null)); + ->method('parse'); $this->expectException('\PHPDraft\Parse\ExecutionException'); $this->expectExceptionMessage('Drafter generated invalid JSON (ERROR)'); $this->expectExceptionCode(2); - $this->mock_function('json_last_error', function () { - return JSON_ERROR_DEPTH; - }); - $this->mock_function('json_last_error_msg', function () { - return "ERROR"; - }); + $this->mock_function('json_last_error', fn() => JSON_ERROR_DEPTH); + $this->mock_function('json_last_error_msg', fn() => "ERROR"); $this->class->parseToJson(); $this->expectOutputString('ERROR: invalid json in /tmp/drafter/index.json'); $this->unmock_function('json_last_error_msg'); diff --git a/src/PHPDraft/Parse/Tests/DrafterAPITest.php b/src/PHPDraft/Parse/Tests/DrafterAPITest.php index a87d1fe4..b4dbfece 100644 --- a/src/PHPDraft/Parse/Tests/DrafterAPITest.php +++ b/src/PHPDraft/Parse/Tests/DrafterAPITest.php @@ -28,14 +28,14 @@ class DrafterAPITest extends LunrBaseTest */ private mixed $parser; + private DrafterAPI $class; + /** * Basic setup */ public function setUp(): void { - $this->mock_function('sys_get_temp_dir', function () { - return TEST_STATICS; - }); + $this->mock_function('sys_get_temp_dir', fn() => TEST_STATICS); $this->parser = $this->getMockBuilder('\PHPDraft\In\ApibFileParser') ->disableOriginalConstructor() @@ -44,7 +44,7 @@ public function setUp(): void $this->parser->set_apib_content(file_get_contents(TEST_STATICS . '/drafter/apib/index.apib')); $this->class = new DrafterAPI(); - $this->reflection = new ReflectionClass('PHPDraft\Parse\DrafterAPI'); + $this->baseSetUp($this->class); $this->class->init($this->parser); @@ -56,8 +56,7 @@ public function setUp(): void */ public function tearDown(): void { - unset($this->class); - unset($this->reflection); + parent::tearDown(); unset($this->parser); } @@ -78,12 +77,8 @@ public function testSetupCorrectly(): void */ public function testAvailableFails(): void { - $this->mock_function('curl_exec', function () { - return "/some/dir/drafter\n"; - }); - $this->mock_function('curl_errno', function () { - return 1; - }); + $this->mock_function('curl_exec', fn() => "/some/dir/drafter\n"); + $this->mock_function('curl_errno', fn() => 1); $this->assertFalse(DrafterAPI::available()); @@ -98,12 +93,8 @@ public function testAvailableFails(): void */ public function testAvailableSuccess(): void { - $this->mock_function('curl_exec', function () { - return "/some/dir/drafter\n"; - }); - $this->mock_function('curl_errno', function () { - return 0; - }); + $this->mock_function('curl_exec', fn() => "/some/dir/drafter\n"); + $this->mock_function('curl_errno', fn() => 0); $this->assertFalse(DrafterAPI::available()); @@ -122,9 +113,7 @@ public function testParseWithFailingWebservice(): void $this->expectExceptionMessage('Drafter webservice failed to parse input'); $this->expectExceptionCode(1); - $this->mock_function('curl_errno', function () { - return 1; - }); + $this->mock_function('curl_errno', fn() => 1); $this->class->parseToJson(); $this->unmock_function('curl_errno'); } @@ -136,21 +125,18 @@ public function testParseWithFailingWebservice(): void */ public function testParseSuccess(): void { - $this->mock_function('json_last_error', function () { - return 0; - }); - $this->mock_function('curl_errno', function () { - return 0; - }); - $this->mock_function('curl_exec', function () { - return '{"content":[{"element":"world"}]}'; - }); + $this->mock_function('json_last_error', fn() => 0); + $this->mock_function('curl_errno', fn() => 0); + $this->mock_function('curl_exec', fn() => '{"content":[{"element":"world"}]}'); + $this->class->parseToJson(); + $this->unmock_function('curl_exec'); $this->unmock_function('curl_errno'); $this->unmock_function('json_last_error'); - $obj = new \stdClass(); - $obj2 = new \stdClass(); + + $obj = (object)[]; + $obj2 = (object)[]; $obj2->element = 'world'; $obj->content = [ $obj2 ]; $this->assertEquals($obj, $this->class->json); diff --git a/src/PHPDraft/Parse/Tests/DrafterTest.php b/src/PHPDraft/Parse/Tests/DrafterTest.php index 22612ab6..9305a73e 100644 --- a/src/PHPDraft/Parse/Tests/DrafterTest.php +++ b/src/PHPDraft/Parse/Tests/DrafterTest.php @@ -35,12 +35,8 @@ class DrafterTest extends LunrBaseTest */ public function setUp(): void { - $this->mock_function('sys_get_temp_dir', function () { - return TEST_STATICS; - }); - $this->mock_function('shell_exec', function () { - return "/some/dir/drafter\n"; - }); + $this->mock_function('sys_get_temp_dir', fn() => TEST_STATICS); + $this->mock_function('shell_exec', fn() => "/some/dir/drafter\n"); $this->parser = $this->getMockBuilder('\PHPDraft\In\ApibFileParser') ->disableOriginalConstructor() @@ -49,7 +45,7 @@ public function setUp(): void $this->parser->set_apib_content(file_get_contents(TEST_STATICS . '/drafter/apib/index.apib')); $this->class = new Drafter(); - $this->reflection = new ReflectionClass('\PHPDraft\Parse\Drafter'); + $this->baseSetUp($this->class); $this->class->init($this->parser); @@ -88,12 +84,8 @@ public function testSetupCorrectly(): void */ public function testParseToJSON(): void { - $this->mock_function('json_last_error', function () { - return JSON_ERROR_NONE; - }); - $this->mock_function('shell_exec', function () { - return ""; - }); + $this->mock_function('json_last_error', fn() => JSON_ERROR_NONE); + $this->mock_function('shell_exec', fn() => ""); file_put_contents( TEST_STATICS . '/drafter/index.json', file_get_contents(TEST_STATICS . '/drafter/json/index.json') @@ -114,12 +106,8 @@ public function testParseToJSON(): void */ public function testParseToJSONInheritance(): void { - $this->mock_function('json_last_error', function () { - return JSON_ERROR_NONE; - }); - $this->mock_function('shell_exec', function () { - return ""; - }); + $this->mock_function('json_last_error', fn() => JSON_ERROR_NONE); + $this->mock_function('shell_exec', fn() => ''); file_put_contents( TEST_STATICS . '/drafter/index.json', file_get_contents(TEST_STATICS . '/drafter/json/inheritance.json') @@ -144,9 +132,7 @@ public function testParseToJSONWithErrors(): void $this->expectExceptionMessage('Parsing encountered errors and stopped'); $this->expectExceptionCode(2); - $this->mock_function('shell_exec', function () { - return ""; - }); + $this->mock_function('shell_exec', fn() => ''); file_put_contents( TEST_STATICS . '/drafter/index.json', file_get_contents(TEST_STATICS . '/drafter/json/error.json') @@ -163,9 +149,7 @@ public function testParseToJSONWithErrors(): void */ public function testSetupWithoutDrafter(): void { - $this->mock_function('shell_exec', function () { - return ""; - }); + $this->mock_function('shell_exec', fn() => ''); $this->assertFalse(Drafter::available()); $this->unmock_function('shell_exec'); } @@ -181,12 +165,8 @@ public function testParseToJSONWithInvalidJSON(): void $this->expectExceptionMessage('Drafter generated invalid JSON (ERROR)'); $this->expectExceptionCode(2); - $this->mock_function('json_last_error', function () { - return JSON_ERROR_DEPTH; - }); - $this->mock_function('json_last_error_msg', function () { - return "ERROR"; - }); + $this->mock_function('json_last_error', fn() => JSON_ERROR_DEPTH); + $this->mock_function('json_last_error_msg', fn() => 'ERROR'); file_put_contents(TEST_STATICS . '/drafter/index.json', '["hello: \'world}'); $this->class->parseToJson(); $this->expectOutputString('ERROR: invalid json in /tmp/drafter/index.json'); diff --git a/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php b/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php index 1925ddf7..723ad8d5 100644 --- a/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php +++ b/src/PHPDraft/Parse/Tests/HtmlGeneratorTest.php @@ -23,7 +23,7 @@ class HtmlGeneratorTest extends LunrBaseTest * Test Class * @var HtmlGenerator */ - protected $class; + protected HtmlGenerator $class; /** * Set up @@ -32,9 +32,10 @@ class HtmlGeneratorTest extends LunrBaseTest public function setUp(): void { define('ID_STATIC', 'SOME_ID'); - $data = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')); - $this->class = new HtmlGenerator(); - $this->reflection = new ReflectionClass('PHPDraft\Parse\HtmlGenerator'); + $data = json_decode(file_get_contents(TEST_STATICS . '/drafter/json/index.json')); + $this->class = new HtmlGenerator(); + + $this->baseSetUp($this->class); $this->class->init($data); $this->class->sorting = -1; diff --git a/src/PHPDraft/Parse/Tests/ParserFactoryTest.php b/src/PHPDraft/Parse/Tests/ParserFactoryTest.php index 8cba064d..5755cb18 100644 --- a/src/PHPDraft/Parse/Tests/ParserFactoryTest.php +++ b/src/PHPDraft/Parse/Tests/ParserFactoryTest.php @@ -16,9 +16,7 @@ class ParserFactoryTest extends LunrBaseTest */ public function testGetDrafter(): void { - $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], function () { - return true; - }); + $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], fn() => true); $this->assertInstanceOf('\PHPDraft\Parse\Drafter', ParserFactory::getDrafter()); @@ -30,12 +28,8 @@ public function testGetDrafter(): void */ public function testGetDrafterAPI(): void { - $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], function () { - return false; - }); - $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], function () { - return true; - }); + $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], fn() => false); + $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], fn() => true); $this->assertInstanceOf('\PHPDraft\Parse\DrafterAPI', ParserFactory::getDrafter()); @@ -50,12 +44,8 @@ public function testGetDrafterFails(): void { $this->expectException('\PHPDraft\Parse\ResourceException'); $this->expectExceptionMessage('Couldn\'t get an APIB parser'); - $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], function () { - return false; - }); - $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], function () { - return false; - }); + $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], fn() => false); + $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], fn() => false); ParserFactory::getDrafter(); @@ -68,12 +58,8 @@ public function testGetDrafterFails(): void */ public function testGetJson(): void { - $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], function () { - return false; - }); - $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], function () { - return true; - }); + $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], fn() => false); + $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], fn() => true); $this->assertInstanceOf('\PHPDraft\Parse\HtmlGenerator', ParserFactory::getJson()); @@ -88,12 +74,8 @@ public function testGetJsonFails(): void { $this->expectException('\PHPDraft\Parse\ResourceException'); $this->expectExceptionMessage('Couldn\'t get a JSON parser'); - $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], function () { - return false; - }); - $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], function () { - return false; - }); + $this->mock_method(['\PHPDraft\Parse\Drafter', 'available'], fn() => false); + $this->mock_method(['\PHPDraft\Parse\DrafterAPI', 'available'], fn() => false); ParserFactory::getJson();