Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:45:54+00:00 2026-06-12T22:45:54+00:00

class Documentation { private $_text; public function __construct($text) { $this->_text = $text; } public

  • 0
class Documentation
{
    private $_text;

    public function __construct($text)
    {
        $this->_text = $text;
    }

    public function generate()
    {
        return new \DOMElement('documentation', $this->_text);
    }
}

The obvious solution I can think of is to create new DOMDocument, append the result of generate() function call and to compare with expected element using $this->assertEqualXMLStructure, but for some reason I don’t like it and am sure there are alternatives.

Any ideas?

UPD: seems like I’ve missed something important: what I want to make sure of is that the element of a particular type with a particular content is returned. How to do that?

UPD 2:

this is what I currently could create, but it’s ugly, isn’t it?

public function testGenerate()
{
    $expected = new \DOMDocument();
    $expected->loadXML('<?xml version="1.0" encoding="utf-8"?><documentation>foo</documentation>');

    $documentation = new Documentation('foo');
    $actual = new \DOMDocument('1.0', 'utf-8');
    $actual->appendChild($documentation->generate());

    $this->assertEqualXMLStructure($expected, $actual);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:45:56+00:00Added an answer on June 12, 2026 at 10:45 pm

    It’s such a simple class that there’s hardly anything that could possibly go wrong with it. There’s no branching in the code at all, and all the methods have a cyclomatic complexity of 1. There’s really no need to write a test suite for such a simple class.

    However, you could use PHPUnit to assert that the generate() method returns a DOMElement object and that the child node of that element is a text object and that the text object matches the input text.

    There’s just not a lot of point, really.

    EDIT TO ADD: This is an example method for doing the test (assuming PHPUnit as the test runner). It’s not tested so the syntax may be wrong but it should give you an idea of the test procedure.

    As you can see, this method is longer than the class being tested! I’m a big fan of unit testing, but in this particular case it seems to be overkill. Unless you have a code-coverage quota you must hit, or unless you’re especially cautious and want some assurance regarding your class, I just wouldn’t bother in this particular case.

    public function testGenerate ()
    {
        $expected = 'The quick brown fox jumps over the lazy dog';
        $this -> object = new Documentation ($expected);
        $actual = $this -> object -> generate ();
    
        // Check we got a DOM Element object
        $this -> assertInstanceOf ('\DOMElement', $actual);
    
        // Check that our DOM element is of the Documentation type
        $this -> assertEquals ('documentation', $actual -> tagName);
    
        // Check that our Documentation element has a single text node child
        $this -> assertEquals (1, $actual -> childNodes -> length);
        $this -> assertInstanceOf ('\DOMText', $actual -> firstChild);
    
        // Check that the text node has the value we passed in originally
        $this -> assertEquals ($expected, $actual -> firstChild -> wholeText);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the CLLocationManager class documentation, I find such explanation for the distanceFilter property: This
I am working on simple form to validate fields like this one. public class
From an example [1] in the documentation for boost::iterator_facade: class node_iterator : public boost::iterator_facade<
I've written this documentation of a rather simple class, but I feel it's not
I seen in android documentation where you use private class HelloWebViewClient extends WebViewClient {
class Map { private: std::vector<std::string> key; std::vector<std::string> storage; int i; public: Map(); Map* set(std::string,
@Entity @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Problem { @ManyToOne private Person person; } @Entity
I have this kind of class and i would like to take variable outside(return
Following the WebView documentation , I want to inject a public java function to
I'm using http://undesigned.org.za/2007/10/22/amazon-s3-php-class/documentation to access private files using php. I can get the data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.