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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:19:24+00:00 2026-06-06T21:19:24+00:00

I am working on setting up a testing suite for a PHP Propel project

  • 0

I am working on setting up a testing suite for a PHP Propel project
using Phactory, and PHPUnit. I am currently trying to unit test a
function that makes an external request, and I want to stub in a mock
response for that request.

Here’s a snippet of the class I am trying to test:

class Endpoint {
  ...
  public function parseThirdPartyResponse() {
    $response = $this->fetchUrl("www.example.com/api.xml");
    // do stuff and return 
    ...
  }

  public function fetchUrl($url) {
    return file_get_contents($url);
  }
  ...

And here’s the test function I am trying to write.

// my factory, defined in a seperate file
Phactory::define('endpoint', array('identifier'  => 'endpoint_$n');

// a test case in my endpoint_test file
public function testParseThirdPartyResponse() {
  $phEndpoint = Phactory::create('endpoint', $options);
  $endpoint = new EndpointQuery()::create()->findPK($phEndpoint->id);

  $stub = $this->getMock('Endpoint');
  $xml = "...<target>test_target</target>...";  // sample response from third party api

  $stub->expects($this->any())
       ->method('fetchUrl')
       ->will($this->returnValue($xml));

  $result = $endpoint->parseThirdPartyResponse();
  $this->assertEquals('test_target', $result);
}

I can see now, after I tried my test code, that I am creating a mock object
with getMock, and then never using it. So the function fetchUrl
actually executes, which I do not want. But I still want to be able to use
the Phactory created endpoint object, since it has all the right fields
populated from my factory definition.

Is there a way for me to stub a method on an existing object? So I could stub
fetch_url on the $endpoint Endpoint object I just created?

Or am I going about this all wrong; is there a better way for me to unit test
my functions that rely on external web requests?

I did read the PHPUnit documentation regarding “Stubbing and Mocking Web Services”, but their sample code for doing so is 40 lines long, not including having to define your own wsdl. I’m hard pressed to believe that’s the most convenient way for me to handle this, unless the good people of SO feel strongly otherwise.

Greatly appreciate any help, I’ve been hung up on this all day. Thanks!!

  • 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-06T21:19:26+00:00Added an answer on June 6, 2026 at 9:19 pm

    From a testing perspective, your code has two problems:

    1. The url is hardcoded, leaving you no way of altering it for development, testing or production
    2. The Endpoint knows about how to retrieve data. From your code I cannot say what the endpoint really does, but if it’s not a low level “Just get me Data” object, it should not know about how to retrieve the data.

    With your code like this, there is no good way to test your code. You could work with Reflections, changing your code and so on. The problem with this approach is that you don’t test your actual object but some reflection which got change to work with the test.

    If you want to write “good” tests, your endpoint should look something like this:

    class Endpoint {
    
        private $dataParser;
        private $endpointUrl;
    
        public function __construct($dataParser, $endpointUrl) {
            $this->dataPartser = $dataParser;
            $this->endpointUrl = $endpointUrl;
        }
    
        public function parseThirdPartyResponse() {
            $response = $this->dataPartser->fetchUrl($this->endpointUrl);
            // ...
        }
    }
    

    Now you could inject a Mock of the DataParser which returns some default response depending on what you want to test.

    The next question might be: How do I test the DataParser? Mostly, you don’t. If it is just a wrapper around php standard functions, you don’t need to. Your DataParser should really be very low level, looking like this:

    class DataParser {
        public function fetchUrl($url) {
            return file_get_contents($url);
        }
    }
    

    If you need or want to test it, you could create a Webservice which lives within your tests and acts as a “Mock”, always returning preconfigured data. You could then call this mock url instead of the real one and evaluate the return.

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

Sidebar

Related Questions

I'm working on setting up NHibernate for a project and I have a few
I'm trying to use cURL with the proxy setting, which was working fine before
I am working on a test environment for a project, and am looking into
In order to test some stuff, I've been using the trick of setting a
I'm rather new to Powershell and am working on setting up my profile.ps1 file.
I'm working on making a script with node.js for setting up my dzen2 in
I am working in flex and I am just setting a variable to false.
I am setting scrollTop and scrollLeft for a div that I am working with.
I'm working on an app (Written in C#) that have a setting to run
I am working with MODO python scripts. I imported the MODO setting data into

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.