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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:06:46+00:00 2026-05-14T23:06:46+00:00

I’m testing some legacy code that extends the default php exception object. This code

  • 0

I’m testing some legacy code that extends the default php exception object. This code prints out a custom HTML error message.

I would like to mock this exception object in such a way that when the tested code generates an exception it will just echo the basic message instead of giving me the whole HTML message.

I cannot figure out a way to do this. It seems like you can test for explicit exceptions, but you can’t change in a general way the behavior of an exception, and you also can’t mock up an object that extends a default php functionality. ( can’t think of another example of this beyond exceptions… but it would seem to be the case )

I guess the problem is, where would you attach the mocked object?? It seems like you can’t interfere with ‘throw new’ and this is the place that the object method is called….

Or if you could somehow use the existing phpunit exception functionality to change the exception behavior the way you want, in a general way for all your code… but this seems like it would be hacky and bad….

EDIT: here is some code to make things clearer:

class FooTest extends PHPUnit_Framework_TestCase{

    public function testBar(){
        include '/path/to/file.php'; //generates exception

        $this->assertTrue($baz);             
    }
}
 ...
//overridden exception class
class Foo_Exception extends ErrorException{
 ...

so, my question, is there a way to deal with this overriden class, without doing it on a case by case basis? what if I’m not testing the behavior of the exception, just the code that causes the exception?

  • 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-14T23:06:47+00:00Added an answer on May 14, 2026 at 11:06 pm

    I would first write a test that captures the exception generation behavior:

    include '/path/to/file.php'; //generates exception
    public function testCatchFooException() {
        try {
            $this->assertTrue($baz);             
        }
        catch (Exception $expected) {
            $this->assertEquals('This is expected html from exception', $expected->getMessage());
            return;
        }
    
        $this->fail('An expected Exception has not been raised Foo_Excpetion.');
    }
    

    Now you can do several things with this coverage test. You can either fix up the exception, or fix the code that causes the exception.

    Another thing you can do is wrap the entire file.php in a class:

     class FooClass {
    
        function runFoo() {
            include '/path/to/file.php'; //generates exception
    
        }   
    
    }
    

    Then add tests while using extract method until you isolate exception.

    [EDIT]

    Here is some serious procedural legacy code:

    <?php
    require_once 'helper.php';  //helper file
    
    function countNewMessages($user_id) {
    }
    
    function countNewOrders() {
    }
    
    function countNewReturns() {
    }
    
    function getDB($init = NULL) {
    }
    
    function getDisplay() {
    }
    
    getDisplay();
    
    ?>
    

    And here is the wrapped class:

    <?php
    require_once '';  //helper file
    
    class Displayer {
        function countNewMessages($user_id) {
        }
    
        function countNewOrders() {
        }
    
        function countNewReturns() {
        }
    
        function getDB($init = NULL) {
        }
    
        function getDisplay() {
        }
    }
    ?>
    

    And now I can test it:

        function testGetDisplay() {
        $display = new Displayer();
    
        $this->assertEquals('html code', $display->getDisplay());
    }
    

    And test the individual functions in it. And if I can further sprout methods on it.

    The above test would be considered a coverage test. There may be bugs in it, but that is what it does. So as I sprout methods the get more code coverage from tests by sprouting that I can make sure I don’t break the output.

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

Sidebar

Ask A Question

Stats

  • Questions 440k
  • Answers 440k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The setattr call is redundant, if foo.names is indeed a… May 15, 2026 at 5:06 pm
  • Editorial Team
    Editorial Team added an answer Go to Window->Preferences->Java->Code Style->Formatter. Create new formatter. Click on edit… May 15, 2026 at 5:06 pm
  • Editorial Team
    Editorial Team added an answer Java is probably the simplest way to get your application… May 15, 2026 at 5:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.