Skip to content

Commit

Permalink
Add attributes for dataproviders and add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Quadling authored and Richard Quadling committed Mar 8, 2024
1 parent 3ef6f55 commit bfae15e
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 42 deletions.
6 changes: 3 additions & 3 deletions src/Array2XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Array2XML
* @return DomDocument
* @throws Exception
*/
public static function createXML($node_name, $arr = [], $docType = [])
public static function createXML(string $node_name, array $arr = [], array $docType = [])
{
$xml = self::getXMLRoot();

Expand Down Expand Up @@ -78,13 +78,13 @@ private static function bool2str($v)
* Convert an Array to XML.
*
* @param string $node_name - name of the root node to be converted
* @param array $arr - array to be converted
* @param array|string $arr - array to be converted
*
* @return DOMNode
*
* @throws Exception
*/
private static function convert($node_name, $arr = [])
private static function convert(string $node_name, $arr = [])
{
//print_arr($node_name);
$xml = self::getXMLRoot();
Expand Down
4 changes: 1 addition & 3 deletions src/XML2Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ private static function convert(DOMNode $node)

/**
* Get the root XML node, if there isn't one, create it.
*
* @return DOMDocument
*/
private static function getXMLRoot()
private static function getXMLRoot(): DOMDocument
{
if (empty(self::$xml)) {
self::init();
Expand Down
2 changes: 1 addition & 1 deletion tests/Array2XMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use LaLit\Array2XML;

class Array2XMLTest extends \PHPUnit\Framework\TestCase
class Array2XMLTest extends TestCase
{
public function testInvalidAttributeNameThrowsException()
{
Expand Down
8 changes: 7 additions & 1 deletion tests/Issues/Issue020Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DiffMatchPatch\DiffMatchPatch;
use LaLit\Array2XML;
use LaLit\XML2Array;
use PHPUnit\Framework\TestCase;
use LaLitTests\TestCase;

class Issue020Test extends TestCase
{
Expand All @@ -32,6 +32,12 @@ public function testMultipleNamespaces()
//
// Whilst these differences exist, they do not alter the content of the XML file in any meaningful way.
$dmp = new DiffMatchPatch();
var_dump($dmp->patch_toText(
$dmp->patch_make(
$originalXML,
$newXML
)
));
$this->assertStringEqualsFile(
__DIR__.'/Fixtures/Issue020Test.diff',
$dmp->patch_toText(
Expand Down
24 changes: 9 additions & 15 deletions tests/MainLaLitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LaLitTests;

use Exception;
use LaLit\Array2XML;
use LaLit\XML2Array;

Expand All @@ -15,14 +16,14 @@
define('VALID_TEST_FOR', 'ValidTestFor');
define('ALL_TESTS', 'AllTests');

class MainLaLitTest extends \PHPUnit\Framework\TestCase
class MainLaLitTest extends TestCase
{
/**
* @param string|string[] $tags
*
* @return array
*/
private static function generateTags($tags)
private static function generateTags($tags): array
{
if (is_array($tags)) {
$tag = array_shift($tags);
Expand Down Expand Up @@ -229,7 +230,7 @@ private static function generateTags($tags)
return $results;
}

public static function provideTestData()
public static function provideTestData(): array
{
return array_merge(
self::generateTags(['root']),
Expand All @@ -243,12 +244,9 @@ public static function provideTestData()

/**
* @dataProvider provideTestData
*
* @param string $xml
* @param string $php
* @param string $structure
*/
public function testArrayToXML($xml, $php, $structure, $validTestFor)
#[\PHPUnit\Framework\Attributes\DataProvider('provideTestData')]
public function testArrayToXML(string $xml, string $php, string $structure)
{
// We build the expected XML as a single line of text, but as we have embedded new lines in some elements, don't
// format the output which would then make additional structural changes.
Expand All @@ -261,14 +259,10 @@ public function testArrayToXML($xml, $php, $structure, $validTestFor)

/**
* @dataProvider provideTestData
*
* @param string $xml
* @param string $php
* @param string $structure
*
* @throws \Exception
* @throws Exception
*/
public function testXMLToArray($xml, $php, $structure, $validTestFor)
#[DataProvider('provideTestData')]
public function testXMLToArray(string $xml, string $php, string $structure)
{
$xmlDOM = new \DOMDocument(1.0, 'UTF-8');
$xmlDOM->xmlStandalone = false;
Expand Down
9 changes: 2 additions & 7 deletions tests/PullRequests/Pr016Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
class Pr016Test extends TestCase
{
/**
* @param array $data
* @param string|null $labelAttributes
* @param string|null $labelCData
* @param string|null $labelDocType
* @param string|null $labelValue
*
* @dataProvider provideLabels
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideLabels')]
public function testLabels(
array $data,
string $labelAttributes = null,
Expand Down Expand Up @@ -74,7 +69,7 @@ public function testLabels(
$this->assertEquals($xml, $resultsFromArray2XmlCreateXml);
}

public static function provideLabels()
public static function provideLabels(): array
{
return [
'Normal labels' => [
Expand Down
11 changes: 7 additions & 4 deletions tests/XML2ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

class XML2ArrayTest extends \PHPUnit\Framework\TestCase
{
public static function provideInvalidObjects()
public static function provideInvalidObjects(): array
{
return [
[new \stdClass()],
];
}

public static function provideInvalidTypes()
public static function provideInvalidTypes(): array
{
return [
[0],
Expand All @@ -24,7 +24,7 @@ public static function provideInvalidTypes()
];
}

public static function provideInvalidXML()
public static function provideInvalidXML(): array
{
return [
['<?xml version="1.0" encoding="UTF-8"?><root>'],
Expand All @@ -38,6 +38,7 @@ public static function provideInvalidXML()
* @throws \Exception
* @dataProvider provideInvalidObjects
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidObjects')]
public function testXMLFileToArrayRejectsInvalidObjects($invalidObject)
{
$this->expectException(\Exception::class);
Expand All @@ -51,6 +52,7 @@ public function testXMLFileToArrayRejectsInvalidObjects($invalidObject)
* @throws \Exception
* @dataProvider provideInvalidTypes
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidTypes')]
public function testXMLFileToArrayRejectsInvalidTypes($invalidType)
{
$this->expectException(\Exception::class);
Expand All @@ -64,7 +66,8 @@ public function testXMLFileToArrayRejectsInvalidTypes($invalidType)
* @throws \Exception
* @dataProvider provideInvalidXML
*/
public function testXMLFileToArrayRejectsInvalidXML($invalidXML)
#[\PHPUnit\Framework\Attributes\DataProvider('provideInvalidXML')]
public function testXMLFileToArrayRejectsInvalidXML(string $invalidXML)
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('[XML2Array] Error parsing the XML string.');
Expand Down
3 changes: 2 additions & 1 deletion tests/bugs/Bug003Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace LaLitTests\Bugs;

use LaLit\Array2XML;
use LaLitTests\TestCase;

/**
* Class Bug003Test.
*/
class Bug003Test extends \PHPUnit\Framework\TestCase
class Bug003Test extends TestCase
{
public function testNullValues()
{
Expand Down
3 changes: 2 additions & 1 deletion tests/bugs/Bug006Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace LaLitTests\Bugs;

use LaLit\Array2XML;
use LaLitTests\TestCase;

/**
* Class Bug006Test.
*/
class Bug006Test extends \PHPUnit\Framework\TestCase
class Bug006Test extends TestCase
{
public function testZeroValues()
{
Expand Down
9 changes: 4 additions & 5 deletions tests/bugs/Bug008Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@

use LaLit\Array2XML;
use LaLit\XML2Array;
use LaLitTests\TestCase;

/**
* Class Bug0088Test.
*/
class Bug008Test extends \PHPUnit\Framework\TestCase
class Bug008Test extends TestCase
{
/**
* @param string $xml
* @param array $array
*
* @throws \Exception
*
* @dataProvider provideDataForRootAttributesAndDocTypes
*/
#[\PHPUnit\Framework\Attributes\DataProvider('provideDataForRootAttributesAndDocTypes')]
public function testRootAttributesAndDocTypes(string $xml, array $array)
{
$actualXMLResults = Array2XML::createXML('root', $array['root'] ?? [], $array['@docType'] ?? [])->saveXML();
Expand All @@ -27,7 +26,7 @@ public function testRootAttributesAndDocTypes(string $xml, array $array)
$this->assertEquals($array, $actualArrayResults, '');
}

public static function provideDataForRootAttributesAndDocTypes()
public static function provideDataForRootAttributesAndDocTypes(): array
{
return
[
Expand Down
2 changes: 1 addition & 1 deletion tests/bugs/Bug018Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use LaLit\Array2XML;
use LaLit\XML2Array;
use PHPUnit\Framework\TestCase;
use LaLitTests\TestCase;

/**
* Class Bug018Test.
Expand Down

0 comments on commit bfae15e

Please sign in to comment.