Guard Incomplete Quiz0:50
it sounds like we need to check right up here. If the quiz has not yet been completed, throw an exception. Okay, let's see, do we have anything like isComplete? No, we don't have anything yet. So let's say if not this.isComplete, then throw an exception. This quiz has not yet been completed. Okay, but now if we give it a run, it's going to fail because no method isComplete exists. Okay, so let's do that now. And how can we check if the quiz has been completed? We have a couple options, but you'll remember in the last episode we did add a method, or maybe not a method, but we are tracking whether or not an answer has been provided. So why don't we add another method here? We could do isComplete again, has this question been answered? And that'll just return whether or not the answer property is set. If it is, then something has been provided. Okay, and reformat. So now if I were
Implement isComplete Check1:51
has this question been answered? And that'll just return whether or not the answer property is set. If it is, then something has been provided. Okay, and reformat. So now if I were to switch back, why don't we do once again an array_filter the questions where each question has been answered, or filter it down to only the questions that have been answered. Okay, so we could say answeredQuestions. Then I could say questions. And actually, we should do count up here as well. So it's a little wordy, but I promise we'll clean this up later. So we're going to return whether or not the answeredQuestions equals the totalQuestions. Now if we scroll back down, let's see if this test passes. Give it a run, and it works. Great. So I see some refactoring opportunities, but I'm going to hold off just a little bit longer. We'll go back to QuizTest and scroll up to the other one. It
Test Next Question Order2:45
it a run, and it works. Great. So I see some refactoring opportunities, but I'm going to hold off just a little bit longer. We'll go back to QuizTest and scroll up to the other one. It correctly tracks the next question in the queue. Okay, so let's grab some of this. And we'll say given I have a Quiz that has not one Question, but two Questions, and I'll just stick with basic math here. And why don't we do this? So let's add a name to each of these. So this will be questionOne, and this will be questionTwo. Now if I get the next question, that actually should be questionOne. So $currentQuestion = questionOne. So at the moment, I believe that should work because we're hard coding it, right? We're just grabbing the first item in the $questions array. So as a quick recap, let's see. Yeah, that's part of the homework. So now if I do it again, this should then return questionTwo. Let's give it a run, and we know this is going to fail. We
Track Current Question Index3:36
quick recap, let's see. Yeah, that's part of the homework. So now if I do it again, this should then return question number two. Let's give it a run, and we know this is going to fail. We expected the what is 3 plus 3 question, but we still got 2 plus 2. Okay, so let's see. We're going to come back here. And again, just a little bit longer, I'm going to store this on the Quiz class. We'll say protected currentQuestion. And again, notice the compound words there. We talked about this in the last episode. That's often a sign that something might be missing. But anyways, what if it's to start? Let's default it to 1. You begin on the first question. So if I say the quiz begins, and we say, all right, let's get to the next question. What's the next one I need to work on? We could then do this currentQuestion, and then let's subtract 1 just to accommodate the zero index for arrays. So let's give it a run, but it's still going to fail. The first test will
work on? We could then do this currentQuestion, and then let's subtract one just to accommodate the zero index for arrays. So let's give it a run, but it's still going to fail. The first test will pass, but not the second. And that's because we never increment the currentQuestion. So how about this as our first step? Grab the question, and then before we return, increment currentQuestion by one, and then return it. Give it a run, and I think that'll work, and we're good to go. Okay, I can still see some issues here, but we'll get to them in a moment. Let's add another one here, and we'll say it returns false if there are no remaining next questions. Okay, so given I have a Quiz that has exactly one question, well, if I were to say quiz->nextQuestion(), and then I do it again, that should return false, because we're trying to get question two effectively, but there is no question two. So let's make sure that returns false. Okay, I think this is going to fail, and it
Handle No Remaining Questions5:23
again, that should return false, because we're trying to get questionTwo effectively, but there is no questionTwo. So let's make sure that returns false. Okay, I think this is going to fail, and it does. So notice undefined offset 1. Let's see what's going on here. Maybe we could add a check here. If not isset, this questions this currentQuestion minus one, then return false. Give it a run, and now that passes. Okay, so let's come on back. Let's run the full suite, and we're looking pretty good here. Pat yourself on the back. We've solved the homework. Things are looking okay, and yet I think there's still quite a bit of room for improvement. Here's our Quiz class, but notice it's now becoming more and more responsible for figuring things out, like what is the next question? What is the current question? How do we figure out if all the questions have been completed? How do we check to see which questions were correctly answered? So notice the more I work
