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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:16:56+00:00 2026-06-17T23:16:56+00:00

Why doesn’t PHPUnit do last exception assertion in this code? public function testConfigOverriding() {

  • 0

Why doesn’t PHPUnit do last exception assertion in this code?

public function testConfigOverriding()
{
    $this->dependencyContainer = new DependencyContainer(__DIR__ . "/../../Resources/valid_json.json");
    $this->assertEquals('overriden', $this->dependencyContainer->getConfig('shell_commander')['pygmentize_command']);

    $unexisting = "unexisting_file";
    $this->setExpectedException('Exception', "Configuration file at path \"$unexisting\" doesn't exist.");
    $this->dependencyContainer = new DependencyContainer($unexisting);

    $invalid = __DIR . "/../../Resources/invalid_json.json";
    $this->setExpectedException('Exception', "Configuration JSON file provided is not valid.");
    $this->dependencyContainer = new DependencyContainer($invalid);
}

So basically: it tests whether “unexsisting_file” exception was thrown, but completely ignores “invalid json” test. Do I need to make separate tests for each exception thrown?

  • 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-17T23:16:56+00:00Added an answer on June 17, 2026 at 11:16 pm

    Even with setExpectedException, your test is still regular PHP code, and follows PHP’s normal rules. If an exception is thrown, program flow immediately jumps out of the current context until it reaches a try/catch block.

    In PHPUnit, when you use setExpectedException, it tells PHPUnit’s core that when it should be expecting an exception from the code that’s about to run. It therefore waits for it with a try/catch block and passes the test if the catch is called with the type of exception it is expecting.

    However, within your test method, the normal PHP rules still apply — when the exception happens, that’s the end of the current code block. Nothing more in that method will be executed, unless you have your own try/catch block within the test method.

    So therefore, in order to test multiple exceptions, you have a few options:

    1. Add your own try/catch to the test method, so that you can carry on with further tests within that method after the first exception.

    2. Split the tests into separate methods, so that each exception is in its own test.

    3. This particular example looks like a good case to use PHPUnit’s dataProvider mechanism, because you’re basically testing the same functionality with two sets of data. The dataProvider feature allows you to define a separate function that contains an array of input data for each set of values you want to test. These values are then passed one set at a time into the test method. Your code would look something like this:

       /**
        * @dataProvider providerConfigOverriding
        */
       public function testConfigOverriding($filename, $expectedExceptionText) {
           $this->dependencyContainer = new DependencyContainer(__DIR__ . "/../../Resources/valid_json.json");
           $this->assertEquals('overriden', $this->dependencyContainer->getConfig('shell_commander')['pygmentize_command']);
      
           $this->setExpectedException('Exception', $expectedExceptionText);
           $this->dependencyContainer = new DependencyContainer($filename);
       }
      
       public function providerConfigOverriding() {
           return array(
               array('unexisting_file', 'Configuration file at path "unexisting_file" doesn\'t exist.'),
               array(__DIR__ . "/../../Resources/invalid_json.json", "Configuration JSON file provided is not valid."),
           );
       }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this code work? b if b = true Error: undefined local variable
Why doesn't the following code throw a java.net.BindException: Address already in use: JVM_Bind exception?
This doesn't work. I've got an exception from SQL databse that column does not
Why doesn't this code print test? #include <stdio.h> #include <stdlib.h> void foo ( void
Why doesn't this function: test <- function(somebool = FALSE) { mydata<-read.csv(mycsvfile.csv) if(somebool){ print(somebool) summary(mydata)
This doesn't work: int number = 1; String numberstring = IntToString(number); I get The
Why doesn't this work? Some Usercontrol (name: myUserControl) with an image control (name: myImage):
Why doesn't this test case work? <?php // cards with cyrillic inidices and suits
Doesn't matter what I do, I simply can't get this to play a sound
Why doesn't this work? struct O { O(int i, int j) : i(i) ,

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.