Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Getting Started with PHP Unit Testing Testing Existing Projects Testing Exceptions

InvalidArgumentException within PHPUnit

After watching the tutorial and teacher's note, I'm still not have clearly understanding of InvalidArgumentException come from. Could someone please tell me where exactly come from in detailed.

Thank you very much and have a nice day!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, MIHA W.LEE! It's built-in to PHP. It is the type of exception that is thrown if/when we pass in data that's an unexpected type. For instance, if we have a function/method that's meant to take an array and we pass in a single integer, then we should throw an InvalidArgumentException.

Check out the InvalidArgumentException docs.

Hope this helps! :sparkles:

Thank you for the reply!! I understood, but where is the InvalidArgumentException::class comes from?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, MIHA W.LEE ! So let's take a look at this test:

/** @test */
    function ingredientMustEnterValidAmount()
    {
        $this->expectException(InvalidArgumentException::class);
        //$this->expectExceptionMessageRegExp('/^The amount must be a float/');
        $recipe = new Recipe("Recipe Title");
        $recipe->addIngredient("garlic", "two", 'tbl'); 
    }

The problem here is what we're passing into the addIngredient method of the Recipe class. If you'll take a look in the "classes" folder and find Recipe.php on line 56 we have the declaration of the addIngredient method. It takes three things, an item, an amount, and a measurement. But the amount and measurement have default values of null. But take a look at lines 58 through 60. It says that if $amount is not null and it is also not of type float, then throw a new InvalidArgumentException.

if ($amount != null && !is_float($amount) && !is_int($amount)) {
            throw new InvalidArgumentException("The amount must be a float: " . gettype($amount) . " $amount given");
}

We tried to pass in the string "two" instead of the float 2.0 or the integer 2. This is what is throwing the exception. So it would produce "The amount must be a float: string two given". This method will not accept any data type for $amount except a float or int. And any integer can always be converted to a float. So it's important that $amount is one of the two :smiley:

Hope this helps! :sparkles:

Sorry for my late reply. Thank you very much!! Finally I understood!!!

Dear Teacher Jennifer Nordell,

I was trying to log into the slack, but I couldn't. The slack sent the email and tell me, 'you should reactivate your account'. I don't know how to do it, can teacher please tell me how to do?

Thank you very much and have nice day

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hey, MIHA W.LEE! Not sure what happened, but I've reactivated your account on slack. You should be able to log in now :smiley:

Thank you teacher Jennifer Nordell. I successfully logged into the slack, but I only saw the unit01 in the slack, the other units didn't appear over the slack.