Unit testing for converting xml file to php classes

Hi there,
I need help.
I convert a xml file to php classes and I’d like to unit test this task.

Which part are you having problems with?

I convert php class to array, and xml string to array, then $this->assertEquals($array1,$array2);
but do not work because even the arrays have the same value the keys are different.

If the keys are important, then it’s right that your assertion would fail: one of the two methods of arriving at the result needs to be corrected. If the keys are not important, maybe it would suffice to use $this->assertEquals(array_values($array1),array_values($array2));?

this is xml code and for each node i created a php class with their atributes respectively. I have to write unit test regarding that task.

Your $res_array is very simply an associative (partially nested) array with string keys and string values. Your $array is a numerically-indexed array with a single value, being an object of your ExchangedDocument class. These are fundamentally different things. If your goal is to convert the XML into an array of ExchangedDocument objects, this code is not going to do it.

Did you perhaps mean to do $xml = simplexml_load_string($xmlstr, ‘ExchangedDocument’, LIBXML_NOCDATA);?

I don’t have any function that convert xml into array. I have created manually php classes based on above xml, so for each node i created php classes with atributes, so i need to create test for that

i tried this $xml = simplexml_load_string($xmlstr, ‘ExchangedDocument’, LIBXML_NOCDATA); but it shows this error
Warning Error: simplexml_load_string() expects parameter 2 to be a class name derived from SimpleXMLElement, ‘ExchangedDocument’

You load the XML, getting a SimpleXMLElement object, which you then json_encode and then json_decode into an associative array. That’s where that array is coming from.

The error you’re getting with my suggested code is easily resolved by making ExchangedDocument extend the SimpleXMLElement class. But I have no idea whether that’s the right solution. It would really depend on the other details of your ExchangedDocument class. Are you expecting that what you’ll get as a result of parsing the XML is an ExchangedDocument object?

None of this, by the way, has anything at all to do with CakePHP, it’s all completely standard PHP.

The reason why I am writing this test is to check if converting from xml to php object is done correctly. So, in order to do that, my idea was to convert both into array and if these arrays are equals it means that converting is done correctly. That is not the right solution but i don’t have any other idea :slight_smile:

And yes, you are right, maybe hasn’t to do with cakephp.
Thank you so much for your help, really appreciate it :slight_smile:

So, specifically what kind of PHP object are you expecting to result from the XML conversion? Because it seems like that is a pretty fundamental question to answer before proceeding. Should it be an ExchangedDocument?

I sent you a message