From d1a043056c99e1959af871bdf26c0a45bf5a4976 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 12 Dec 2022 23:51:25 -0400 Subject: [PATCH] Backport `Rewrite phpunit tests to pest` (#183) This will help backporting changes from main branch --- .github/workflows/run-tests.yml | 3 +- composer.json | 10 +- tests/ArrayToXmlTest.php | 819 ++++++++---------- ...o_be_set_in_SimpleXMLElement_style__1.txt} | 0 ...o_be_set_in_SimpleXMLElement_style__1.txt} | 0 ...It_accepts_an_xml_standalone_value__1.xml} | 0 ...an_be_set_in_SimpleXMLElement_style__1.xml | 11 + ...so_be_set_in_SimpleXMLElement_style__1.txt | 2 + ...so_be_set_in_SimpleXMLElement_style__1.txt | 2 + ...so_be_set_in_SimpleXMLElement_style__1.xml | 2 + ...an_be_set_in_SimpleXMLElement_style__1.xml | 9 + ..._it_accepts_an_xml_standalone_value__1.xml | 2 + 12 files changed, 405 insertions(+), 455 deletions(-) rename tests/__snapshots__/{ArrayToXmlTest__and_cdata_values_can_also_be_set_in_simplexmlelement_style__1.txt => ArrayToXmlTest__And_cdata_values_can_also_be_set_in_SimpleXMLElement_style__1.txt} (100%) rename tests/__snapshots__/{ArrayToXmlTest__and_mixed_values_can_also_be_set_in_simplexmlelement_style__1.txt => ArrayToXmlTest__And_mixed_values_can_also_be_set_in_SimpleXMLElement_style__1.txt} (100%) rename tests/__snapshots__/{ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.txt => ArrayToXmlTest__It_accepts_an_xml_standalone_value__1.xml} (100%) create mode 100644 tests/__snapshots__/ArrayToXmlTest__and_attributes_also_can_be_set_in_SimpleXMLElement_style__1.xml create mode 100644 tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_SimpleXMLElement_style__1.txt create mode 100644 tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_SimpleXMLElement_style__1.txt create mode 100644 tests/__snapshots__/ArrayToXmlTest__and_root_element_attributes_can_also_be_set_in_SimpleXMLElement_style__1.xml create mode 100644 tests/__snapshots__/ArrayToXmlTest__and_value_also_can_be_set_in_SimpleXMLElement_style__1.xml create mode 100644 tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.xml diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 9da689b..38f51e9 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -28,10 +28,9 @@ jobs: - name: Setup problem matchers run: | echo "::add-matcher::${{ runner.tool_cache }}/php.json" - echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - name: Install dependencies run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction - name: Execute tests - run: vendor/bin/phpunit + run: vendor/bin/pest diff --git a/composer.json b/composer.json index dadbc3c..9f3b0bf 100755 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "require-dev": { "phpunit/phpunit" : "^9.0", "mockery/mockery": "^1.2", - "spatie/phpunit-snapshot-assertions": "^4.2" + "pestphp/pest": "^1.21", + "spatie/pest-plugin-snapshots": "^1.1" }, "autoload": { "psr-4": { @@ -36,6 +37,11 @@ } }, "scripts": { - "test": "vendor/bin/phpunit" + "test": "vendor/bin/pest" + }, + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } } } diff --git a/tests/ArrayToXmlTest.php b/tests/ArrayToXmlTest.php index 59edb50..21a4fb1 100644 --- a/tests/ArrayToXmlTest.php +++ b/tests/ArrayToXmlTest.php @@ -1,410 +1,302 @@ testArray = [ - 'Good guy' => [ - - 'name' => 'Luke Skywalker', - 'weapon' => 'Lightsaber', - +uses(MatchesSnapshots::class); + +beforeEach(function () { + $this->testArray = [ + 'Good guy' => [ + + 'name' => 'Luke Skywalker', + 'weapon' => 'Lightsaber', + + ], + 'Bad guy' => [ + 'name' => 'Sauron', + 'weapon' => 'Evil Eye', + + ], + ]; +}); + +it('can convert an array to xml', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert($this->testArray)); +}); + +it('can handle an empty array', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([])); +}); + +it('can receive name for the root element', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([], 'helloyouluckpeople')); +}); + +it('can receive name from array for the root element', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([], [ + 'rootElementName' => 'helloyouluckpeople', + ])); +}); + +it('can convert attributes to xml for the root element', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([], [ + '_attributes' => [ + 'xmlns' => 'https://github.com/spatie/array-to-xml', + ], + ])); +}); + +test('and root element attributes can also be set in SimpleXMLElement style', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([], [ + '@attributes' => [ + 'xmlns' => 'https://github.com/spatie/array-to-xml', + ], + ])); +}); + +it('throws an exception when converting an array with no keys', function () { + ArrayToXml::convert(['one', 'two', 'three']); +})->throws(DOMException::class); + +it('throws an exception when converting an array with invalid characters key names', function () { + echo ArrayToXml::convert(['tom & jerry' => 'cartoon characters'], '', false); +})->throws(DOMException::class); + +it('will raise an exception when spaces should not be replaced and a key contains a space', function () { + ArrayToXml::convert($this->testArray, '', false); +})->throws(DOMException::class); + +it('can handle values as basic collection', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([ + 'user' => ['one', 'two', 'three'], + ])); +}); + +it('can handle zero values in beginning of basic collection', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([ + 'user' => ['0', '1', '0'], + ])); +}); + + +it('accepts an xml encoding type', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([], '', false, 'UTF-8')); +}); + +it('accepts an xml version', function () { + assertMatchesSnapshot(ArrayToXml::convert([], '', false, null, '1.1')); +}); + +it('accepts an xml standalone value', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([], '', false, null, '1.0', [], false)); +}); + +it('can handle values as collection', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([ + 'user' => [ + [ + 'name' => 'een', + 'age' => 10, ], - 'Bad guy' => [ - 'name' => 'Sauron', - 'weapon' => 'Evil Eye', - + [ + 'name' => 'twee', + 'age' => 12, ], - ]; - } - - /** @test */ - public function it_can_convert_an_array_to_xml() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert($this->testArray)); - } - - /** @test */ - public function it_can_handle_an_empty_array() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([])); - } - - /** @test */ - public function it_can_receive_name_for_the_root_element() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], 'helloyouluckpeople')); - } - - /** @test */ - public function it_can_receive_name_from_array_for_the_root_element() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], [ - 'rootElementName' => 'helloyouluckpeople', - ])); - } - - /** @test */ - public function it_can_convert_attributes_to_xml_for_the_root_element() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], [ - '_attributes' => [ - 'xmlns' => 'https://github.com/spatie/array-to-xml', - ], - ])); - } - - /** @test */ - public function and_root_element_attributes_can_also_be_set_in_simplexmlelement_style() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], [ - '@attributes' => [ - 'xmlns' => 'https://github.com/spatie/array-to-xml', - ], - ])); - } - - /** @test */ - public function it_throws_an_exception_when_converting_an_array_with_no_keys() - { - $this->expectException('DOMException'); - - ArrayToXml::convert(['one', 'two', 'three']); - } - - /** @test */ - public function it_throws_an_exception_when_converting_an_array_with_invalid_characters_key_names() - { - $this->expectException('DOMException'); - - echo ArrayToXml::convert(['tom & jerry' => 'cartoon characters'], '', false); - } - - /** @test */ - public function it_will_raise_an_exception_when_spaces_should_not_be_replaced_and_a_key_contains_a_space() - { - $this->expectException('DOMException'); - - ArrayToXml::convert($this->testArray, '', false); - } - - /** @test */ - public function it_can_handle_values_as_basic_collection() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([ - 'user' => ['one', 'two', 'three'], - ])); - } - - /** @test */ - public function it_can_handle_zero_values_in_beginning_of_basic_collection() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([ - 'user' => ['0', '1', '0'], - ])); - } - - /** @test */ - public function it_accepts_an_xml_encoding_type() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], '', false, 'UTF-8')); - } - - /** @test */ - public function it_accepts_an_xml_version() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([], '', false, null, '1.1')); - } - - /** @test */ - public function it_accepts_an_xml_standalone_value() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([], '', false, null, '1.0', [], false)); - } - - /** @test */ - public function it_can_handle_values_as_collection() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([ - 'user' => [ - [ + ], + ])); +}); + +it('can handle values with special characters', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert(['name' => 'this & that'])); +}); + +it('can handle values with special control characters', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert(['name' => "i want tothis and \x03 that"])); +}); + +it('can group by values when values are in a numeric array', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert(['user' => ['foo', 'bar']])); +}); + +it('can convert attributes to xml', function () { + $withAttributes = $this->testArray; + $withAttributes['Good guy']['_attributes'] = ['nameType' => 1]; + + assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes)); +}); + +it('can handle attributes as collection', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([ + 'user' => [ + [ + '_attributes' => [ 'name' => 'een', 'age' => 10, ], - [ + ], + [ + '_attributes' => [ 'name' => 'twee', 'age' => 12, ], ], - ])); - } - - /** @test */ - public function it_can_handle_values_with_special_characters() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert(['name' => 'this & that'])); - } - - /** @test */ - public function it_can_handle_values_with_special_control_characters() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert(['name' => "i want tothis and \x03 that"])); - } - - /** - * @test - */ - public function it_can_group_by_values_when_values_are_in_a_numeric_array() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert(['user' => ['foo', 'bar']])); - } - - /** @test */ - public function it_can_convert_attributes_to_xml() - { - $withAttributes = $this->testArray; - - $withAttributes['Good guy']['_attributes'] = ['nameType' => 1]; - - $this->assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes)); - } - - /** @test */ - public function it_can_handle_attributes_as_collection() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([ - 'user' => [ - [ - '_attributes' => [ - 'name' => 'een', - 'age' => 10, - ], - ], - [ - '_attributes' => [ - 'name' => 'twee', - 'age' => 12, - ], + ], + ])); +}); + +test('and attributes also can be set in SimpleXMLElement style', function () { + $withAttributes = $this->testArray; + $withAttributes['Good guy']['@attributes'] = ['nameType' => 1]; + + assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes)); +}); + +it('can handle values set with attributes with special characters', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([ + 'movie' => [ + [ + 'title' => [ + '_attributes' => ['category' => 'SF'], + '_value' => 'STAR WARS', ], ], - ])); - } - - /** @test */ - public function and_attributes_also_can_be_set_in_simplexmlelement_style() - { - $withAttributes = $this->testArray; - - $withAttributes['Good guy']['@attributes'] = ['nameType' => 1]; - - $this->assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes)); - } - - /** @test */ - public function it_can_handle_values_set_with_attributes_with_special_characters() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([ - 'movie' => [ - [ - 'title' => [ - '_attributes' => ['category' => 'SF'], - '_value' => 'STAR WARS', - ], - ], - [ - 'title' => [ - '_attributes' => ['category' => 'Children'], - '_value' => 'tom & jerry', - ], + [ + 'title' => [ + '_attributes' => ['category' => 'Children'], + '_value' => 'tom & jerry', ], ], - ])); - } - - /** @test */ - public function and_value_also_can_be_set_in_simplexmlelement_style() - { - $this->assertMatchesXmlSnapshot(ArrayToXml::convert([ - 'movie' => [ - [ - 'title' => [ - '@attributes' => ['category' => 'SF'], - '@value' => 'STAR WARS', - ], - ], - [ - 'title' => [ - '@attributes' => ['category' => 'Children'], - '@value' => 'tom & jerry', - ], + ], + ])); +}); + +test('and value also can be set in SimpleXMLElement style', function () { + assertMatchesXmlSnapshot(ArrayToXml::convert([ + 'movie' => [ + [ + 'title' => [ + '@attributes' => ['category' => 'SF'], + '@value' => 'STAR WARS', ], ], - ])); - } - - /** @test */ - public function it_can_handle_values_set_as_cdata() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - 'movie' => [ - [ - 'title' => [ - '_attributes' => ['category' => 'SF'], - '_cdata' => '

STAR WARS

', - ], - ], - [ - 'title' => [ - '_attributes' => ['category' => 'Children'], - '_cdata' => '

tom & jerry

', - ], + [ + 'title' => [ + '@attributes' => ['category' => 'Children'], + '@value' => 'tom & jerry', ], ], - ])); - } - - /** @test */ - public function and_cdata_values_can_also_be_set_in_simplexmlelement_style() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - 'movie' => [ - [ - 'title' => [ - '@attributes' => ['category' => 'SF'], - '@cdata' => '

STAR WARS

', - ], - ], - [ - 'title' => [ - '@attributes' => ['category' => 'Children'], - '@cdata' => '

tom & jerry

', - ], + ], + ])); +}); + +it('can handle values set as cdata', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + 'movie' => [ + [ + 'title' => [ + '_attributes' => ['category' => 'SF'], + '_cdata' => '

STAR WARS

', ], ], - ])); - } - - /** @test */ - public function it_doesnt_pollute_attributes_in_collection_and_sequential_nodes() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - 'books' => [ - 'book' => [ - ['name' => 'A', '@attributes' => ['z' => 1]], - ['name' => 'B'], - ['name' => 'C'], + [ + 'title' => [ + '_attributes' => ['category' => 'Children'], + '_cdata' => '

tom & jerry

', ], ], - ])); - } - - /** @test */ - public function it_can_convert_array_to_dom() - { - $resultDom = (new ArrayToXml($this->testArray))->toDom(); - - $this->assertSame('Luke Skywalker', $resultDom->getElementsByTagName('name')->item(0)->nodeValue); - $this->assertSame('Sauron', $resultDom->getElementsByTagName('name')->item(1)->nodeValue); - $this->assertSame('Lightsaber', $resultDom->getElementsByTagName('weapon')->item(0)->nodeValue); - $this->assertSame('Evil Eye', $resultDom->getElementsByTagName('weapon')->item(1)->nodeValue); - } - - /** @test */ - public function it_can_handle_values_set_as_mixed() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - 'movie' => [ - [ - 'title' => [ - '@attributes' => ['category' => 'SF'], - '_mixed' => 'STAR WARS Figure 1', - ], + ], + ])); +}); + +test('and cdata values can also be set in SimpleXMLElement style', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + 'movie' => [ + [ + 'title' => [ + '@attributes' => ['category' => 'SF'], + '@cdata' => '

STAR WARS

', ], - [ - 'title' => [ - '@attributes' => ['category' => 'Action'], - '_mixed' => 'ROBOCOP Figure 2', - ], + ], + [ + 'title' => [ + '@attributes' => ['category' => 'Children'], + '@cdata' => '

tom & jerry

', ], ], - ])); - } - - /** @test */ - public function and_mixed_values_can_also_be_set_in_simplexmlelement_style() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - 'movie' => [ - [ - 'title' => [ - '@attributes' => ['category' => 'SF'], - '@mixed' => 'STAR WARS Figure 1', - ], + ], + ])); +}); + +it('doesnt pollute attributes in collection and sequential nodes', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + 'books' => [ + 'book' => [ + ['name' => 'A', '@attributes' => ['z' => 1]], + ['name' => 'B'], + ['name' => 'C'], + ], + ], + ])); +}); + +it('can convert array to dom', function () { + $resultDom = (new ArrayToXml($this->testArray))->toDom(); + + expect($resultDom->getElementsByTagName('name')->item(0)->nodeValue)->toBe('Luke Skywalker'); + expect($resultDom->getElementsByTagName('name')->item(1)->nodeValue)->toBe('Sauron'); + expect($resultDom->getElementsByTagName('weapon')->item(0)->nodeValue)->toBe('Lightsaber'); + expect($resultDom->getElementsByTagName('weapon')->item(1)->nodeValue)->toBe('Evil Eye'); +}); + +it('can handle values set as mixed', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + 'movie' => [ + [ + 'title' => [ + '@attributes' => ['category' => 'SF'], + '_mixed' => 'STAR WARS Figure 1', ], - [ - 'title' => [ - '@attributes' => ['category' => 'Action'], - '@mixed' => 'ROBOCOP Figure 2', - ], + ], + [ + 'title' => [ + '@attributes' => ['category' => 'Action'], + '_mixed' => 'ROBOCOP Figure 2', ], ], - ])); - } - - /** @test */ - public function it_can_handle_numeric_keys() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - '__numeric' => [ - 16 => [ - 'parent' => 'aaa', - 'numLinks' => 3, - 'child' => [ - 16 => [ - 'parent' => 'abc', - 'numLinks' => 3, - ], - ], + ], + ])); +}); + +test('and mixed values can also be set in SimpleXMLElement style', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + 'movie' => [ + [ + 'title' => [ + '@attributes' => ['category' => 'SF'], + '@mixed' => 'STAR WARS Figure 1', ], - 17 => [ - 'parent' => 'bb', - 'numLinks' => 3, - 'child' => [ - 16 => [ - 'parent' => 'abb', - 'numLinks' => 3, - 'child' => [ - 16 => [ - 'parent' => 'abc', - 'numLinks' => 3, - ], - ], - ], - 17 => [ - 'parent' => 'acb', - 'numLinks' => 3, - ], - ], + ], + [ + 'title' => [ + '@attributes' => ['category' => 'Action'], + '@mixed' => 'ROBOCOP Figure 2', ], ], - ])); - } - - /** @test */ - public function it_can_handle_custom_keys() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - '__custom:custom-key:01' => [ + ], + ])); +}); + +it('can handle numeric keys', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + '__numeric' => [ + 16 => [ 'parent' => 'aaa', 'numLinks' => 3, 'child' => [ @@ -414,106 +306,131 @@ public function it_can_handle_custom_keys() ], ], ], - '__custom:custom-key:02' => [ + 17 => [ 'parent' => 'bb', 'numLinks' => 3, 'child' => [ - '__custom:custom-subkey:01' => [ + 16 => [ 'parent' => 'abb', 'numLinks' => 3, 'child' => [ - '__custom:custom-subsubkey:01' => [ + 16 => [ 'parent' => 'abc', 'numLinks' => 3, ], ], ], - '__custom:custom-subkey:02' => [ + 17 => [ 'parent' => 'acb', 'numLinks' => 3, ], ], ], - ])); - } - - /** @test */ - public function it_can_handle_custom_keys_containing_colon_character() - { - $this->assertMatchesSnapshot(ArrayToXml::convert([ - '__custom:custom\:key:01' => [ - 'parent' => 'aaa', - 'numLinks' => 3, - 'child' => [ - 16 => [ - 'parent' => 'abc', - 'numLinks' => 3, - ], + ], + ])); +}); + +it('can handle custom keys', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + '__custom:custom-key:01' => [ + 'parent' => 'aaa', + 'numLinks' => 3, + 'child' => [ + 16 => [ + 'parent' => 'abc', + 'numLinks' => 3, ], ], - '__custom:custom\:key:02' => [ - 'parent' => 'bb', - 'numLinks' => 3, - 'child' => [ - '__custom:custom\:subkey:01' => [ - 'parent' => 'abb', - 'numLinks' => 3, - 'child' => [ - '__custom:custom\:subsubkey:01' => [ - 'parent' => 'abc', - 'numLinks' => 3, - ], + ], + '__custom:custom-key:02' => [ + 'parent' => 'bb', + 'numLinks' => 3, + 'child' => [ + '__custom:custom-subkey:01' => [ + 'parent' => 'abb', + 'numLinks' => 3, + 'child' => [ + '__custom:custom-subsubkey:01' => [ + 'parent' => 'abc', + 'numLinks' => 3, ], ], - '__custom:custom\:subkey:02' => [ - 'parent' => 'acb', - 'numLinks' => 3, - ], + ], + '__custom:custom-subkey:02' => [ + 'parent' => 'acb', + 'numLinks' => 3, ], ], - ])); - } - - /** @test */ - public function setting_invalid_properties_will_result_in_an_exception() - { - $this->expectException(\Exception::class); - $xml2Array = new ArrayToXml($this->testArray); - - $xml2Array->setDomProperties(['foo' => 'bar']); - } - - /** @test */ - public function it_can_set_dom_properties() - { - $xml2Array = new ArrayToXml($this->testArray); - $xml2Array->setDomProperties([ - 'formatOutput' => true, - 'version' => '1234567', - ]); - - $dom = $xml2Array->toDom(); - $this->assertTrue($dom->formatOutput); - $this->assertEquals('1234567', $dom->version); - } - - /** @test */ - public function it_can_drop_xml_declaration() - { - $root = [ - 'rootElementName' => 'soap:Envelope', - '_attributes' => [ - 'xmlns:soap' => 'http://www.w3.org/2003/05/soap-envelope/', + ], + ])); +}); + +it('can handle custom keys containing colon character', function () { + assertMatchesSnapshot(ArrayToXml::convert([ + '__custom:custom\:key:01' => [ + 'parent' => 'aaa', + 'numLinks' => 3, + 'child' => [ + 16 => [ + 'parent' => 'abc', + 'numLinks' => 3, + ], ], - ]; - $array = [ - 'soap:Header' => [], - 'soap:Body' => [ - 'soap:key' => 'soap:value', + ], + '__custom:custom\:key:02' => [ + 'parent' => 'bb', + 'numLinks' => 3, + 'child' => [ + '__custom:custom\:subkey:01' => [ + 'parent' => 'abb', + 'numLinks' => 3, + 'child' => [ + '__custom:custom\:subsubkey:01' => [ + 'parent' => 'abc', + 'numLinks' => 3, + ], + ], + ], + '__custom:custom\:subkey:02' => [ + 'parent' => 'acb', + 'numLinks' => 3, + ], ], - ]; - $arrayToXml = new ArrayToXml($array, $root); - - $this->assertMatchesSnapshot($arrayToXml->dropXmlDeclaration()->toXml()); - } -} + ], + ])); +}); + +test('Setting invalid properties will result in an exception', function () { + $xml2Array = new ArrayToXml($this->testArray); + $xml2Array->setDomProperties(['foo' => 'bar']); +})->throws(Exception::class); + +it('can set dom properties', function () { + $xml2Array = new ArrayToXml($this->testArray); + $xml2Array->setDomProperties([ + 'formatOutput' => true, + 'version' => '1234567', + ]); + + $dom = $xml2Array->toDom(); + expect($dom->formatOutput)->toBeTrue(); + expect($dom->version)->toBe('1234567'); +}); + +it('can drop xml declaration', function () { + $root = [ + 'rootElementName' => 'soap:Envelope', + '_attributes' => [ + 'xmlns:soap' => 'http://www.w3.org/2003/05/soap-envelope/', + ], + ]; + $array = [ + 'soap:Header' => [], + 'soap:Body' => [ + 'soap:key' => 'soap:value', + ], + ]; + $arrayToXml = new ArrayToXml($array, $root); + + assertMatchesSnapshot($arrayToXml->dropXmlDeclaration()->toXml()); +}); diff --git a/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_simplexmlelement_style__1.txt b/tests/__snapshots__/ArrayToXmlTest__And_cdata_values_can_also_be_set_in_SimpleXMLElement_style__1.txt similarity index 100% rename from tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_simplexmlelement_style__1.txt rename to tests/__snapshots__/ArrayToXmlTest__And_cdata_values_can_also_be_set_in_SimpleXMLElement_style__1.txt diff --git a/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_simplexmlelement_style__1.txt b/tests/__snapshots__/ArrayToXmlTest__And_mixed_values_can_also_be_set_in_SimpleXMLElement_style__1.txt similarity index 100% rename from tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_simplexmlelement_style__1.txt rename to tests/__snapshots__/ArrayToXmlTest__And_mixed_values_can_also_be_set_in_SimpleXMLElement_style__1.txt diff --git a/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.txt b/tests/__snapshots__/ArrayToXmlTest__It_accepts_an_xml_standalone_value__1.xml similarity index 100% rename from tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.txt rename to tests/__snapshots__/ArrayToXmlTest__It_accepts_an_xml_standalone_value__1.xml diff --git a/tests/__snapshots__/ArrayToXmlTest__and_attributes_also_can_be_set_in_SimpleXMLElement_style__1.xml b/tests/__snapshots__/ArrayToXmlTest__and_attributes_also_can_be_set_in_SimpleXMLElement_style__1.xml new file mode 100644 index 0000000..286a1ed --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__and_attributes_also_can_be_set_in_SimpleXMLElement_style__1.xml @@ -0,0 +1,11 @@ + + + + Luke Skywalker + Lightsaber + + + Sauron + Evil Eye + + diff --git a/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_SimpleXMLElement_style__1.txt b/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_SimpleXMLElement_style__1.txt new file mode 100644 index 0000000..34a7995 --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_SimpleXMLElement_style__1.txt @@ -0,0 +1,2 @@ + +<![CDATA[<p>STAR WARS</p>]]><![CDATA[<p>tom & jerry</p>]]> diff --git a/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_SimpleXMLElement_style__1.txt b/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_SimpleXMLElement_style__1.txt new file mode 100644 index 0000000..a7a95ac --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_SimpleXMLElement_style__1.txt @@ -0,0 +1,2 @@ + +STAR WARS <xref ref-type="fig" rid="f1">Figure 1</xref>ROBOCOP <xref ref-type="fig" rid="f2">Figure 2</xref> diff --git a/tests/__snapshots__/ArrayToXmlTest__and_root_element_attributes_can_also_be_set_in_SimpleXMLElement_style__1.xml b/tests/__snapshots__/ArrayToXmlTest__and_root_element_attributes_can_also_be_set_in_SimpleXMLElement_style__1.xml new file mode 100644 index 0000000..177125b --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__and_root_element_attributes_can_also_be_set_in_SimpleXMLElement_style__1.xml @@ -0,0 +1,2 @@ + + diff --git a/tests/__snapshots__/ArrayToXmlTest__and_value_also_can_be_set_in_SimpleXMLElement_style__1.xml b/tests/__snapshots__/ArrayToXmlTest__and_value_also_can_be_set_in_SimpleXMLElement_style__1.xml new file mode 100644 index 0000000..61d5db3 --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__and_value_also_can_be_set_in_SimpleXMLElement_style__1.xml @@ -0,0 +1,9 @@ + + + + STAR WARS + + + tom & jerry + + diff --git a/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.xml b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.xml new file mode 100644 index 0000000..2589464 --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.xml @@ -0,0 +1,2 @@ + +