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 trialMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsInvalidArgumentException 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
Treehouse TeacherHi 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!
MIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsThank you for the reply!! I understood, but where is the InvalidArgumentException::class comes from?
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi there, MIHA W.LEE ! So let's take a look at this test:
The problem here is what we're passing into the
addIngredient
method of theRecipe
class. If you'll take a look in the "classes" folder and find Recipe.php on line 56 we have the declaration of theaddIngredient
method. It takes three things, an item, an amount, and a measurement. But the amount and measurement have default values ofnull
. 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 newInvalidArgumentException
.We tried to pass in the string "two" instead of the float
2.0
or the integer2
. 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 twoHope this helps!
MIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsSorry for my late reply. Thank you very much!! Finally I understood!!!
MIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsDear 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
Treehouse TeacherJennifer Nordell
Treehouse TeacherHey, MIHA W.LEE! Not sure what happened, but I've reactivated your account on slack. You should be able to log in now
MIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsMIHA W.LEE
PHP Development Techdegree Graduate 25,452 PointsThank 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.