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 1112461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:42:49+00:00 2026-05-17T02:42:49+00:00

How would you test the following code with phpUnit? class Product { protected $id

  • 0

How would you test the following code with phpUnit?

class Product {

    protected $id = null;

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

    public function getDescription($language_id) {
        $db_query = mysql_query("SELECT * FROM products_description WHERE products_id = " . (int) $this->id . " AND language = " . (int) $language_id);
        return mysql_fetch_array($db_query);
    }

}


$product = new Product(1);
$product->getDescription(1); // Test this

At the moment I test it like this:

public function testShouldGetProductsDescription() {
    $product = new Product(1);
    $description = $product->getDescription(1);
    $expected_description = array(
        'name' => 'car',
        'short_text' => 'this is a car',
        'long_text' => 'longer description for the car'
    );
    $this->assertEquals($description, $expected_description);
}

But it doesn’t seem to be a good solution for me.
(This code is only a example and might not work correctly)

  • 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-05-17T02:42:50+00:00Added an answer on May 17, 2026 at 2:42 am

    In simple cases, especially with the database coupling like that, I usually just use stub objects, to stub out the database calls. Then just write the test like this:

     public function productFixture($id)
     {  
        $constructor_arg = $id;
    
        //Array of methods to stub out
        $stubbed_method = array('getDescription'); 
    
        //Create a mock for the Product class.
        $product = $this->getMock('Product', $stubbed_method, $constructor_arg);
    
        //Stub out expected results
        $expected_description = array(
            'name'       => 'car',
            'short_text' => 'this is a car',
            'long_text'  => 'longer description for the car'
        );
    
        //Configure the stub method and its return value
        $product->expects($this->any())
                ->method('getDescription')
                ->will($this->returnValue($expected_description));
    
        return $product;
    }
    
    public function testShouldGetProductsDescription()
    {
        $product = $this->productFixture(1);
        $description = $product->getDescription(1);
    
        $this->assertNotNull($description);
    
        //You could test the description using assertEquals
        //but I find just testing the field names is less brittle
        $this->assertArrayHasKey('name', $description);
        $this->assertArrayHasKey('short_text', $description);
        $this->assertArrayHasKey('long_text', $description);
    }
    

    More info on Stubs/Mocks and PHPUnit : PHPUnit Manual: Test Doubles

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would you test the following code? public IList<T> Find(DetachedCriteria criteria) { return criteria.GetExecutableCriteria(session).List<T>();
Consider the following code snippet: class Test { public int Length{ get; set; }
How would I test the following code? Public Sub SetSerialIdForDevice() Try Dim component As
I got following code: function test () { this.testFunction = function () { //code
I have the following code: [test.h] class MyClass { public: string Name; MyClass(); void
Consider the following code: class Test() { public: Test() { memset( buffer, 0, sizeof(
Consider the following Java code: public class Test { private Foo< String, String >
The following code produces a failed test, not a passing test (as I would
Consider the following code: [Test] public void WidgetTest() { foreach (Widget widget in widgets)
I wanted to test the following code (which works fine for a non-null list)

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.