Manual Purchase Walkthrough0:00
In the previous episode, we finished up by writing all of the logic for our first feature. Then I noted that behind the scenes, I would make some tweaks to the underlying billing code to allow for this. So now let's see where we're at. Before we test this with Behat, let me show you the process manually. And I do apologize, it's not picking up the main font because we are working locally. Anyhow, let's say that we want to purchase some lesson. How about this one? Alright, well now I've set it up so that it will show you a few different options. You can log in to view the video, you could sign up for a subscription, or for the new option, if you just want to buy this video alone and nothing else, you can get it for $4. Now notice that it directs us to the signup page, but all of the subscription information has been removed. Because remember, if you are going to buy videos one at a time,
Now notice that it directs us to the signup page, but all of the subscription information has been removed. Because remember, if you are going to buy videos one at a time, well, I still need to grab your credit card information. So let's go through this process like a user would. John Doe, john@example.com, give some password, I will use dummy credit card here, like so, and click create an account. So now this won't create a subscription, it's just going to add you as a customer for my billing provider. Now it redirects us back to that page, gives you a little message that you're ready to begin purchasing. So now, if I click on this, watch this video for $4, I will confirm it. And that's it. So now I can download it, or I can watch the video below. And in this case, you can ignore sorry, that's just because the video embed for Vimeo won't work locally. But rest assured, this does work. Also, you can now go to your purchases
Run Behat Feature Test1:30
video below. And in this case, you can ignore sorry, that's just because the video embed for Vimeo won't work locally. But rest assured, this does work. Also, you can now go to your purchases area to view everything that you've bought, as well as download it. So let's do one more just to make sure you have the basic idea. Maybe you also want to grab form validation simplified, you click on the button, you confirm it, it processes the charge, and then you're ready to click Play. Pretty cool. Alright, so now let's run the test with Behat to verify that this does work. Behat features/single-purchases. So we'll test this out. And once we confirm that it does work, we will write a couple more scenarios. And then we will clean up our feature context a good bit. So it's going through basically that exact process that I just demonstrated. And sure enough, everything works just like we expected. Okay, let's start cleaning up our feature context. Now,
Refactor Logged-Out Scenario2:18
So it's going through basically that exact process that I just demonstrated. And sure enough, everything works just like we expected. Okay, let's start cleaning up our feature context. Now, I'm going to tweak this just a bit to make it a little more readable. Because think about it, given I'm not logged in, and there is a lesson when I go to the lesson, and I purchase the video, well, a little bit in my mind that's misleading. Because behind the scenes, what we're doing is we're following a link to sign up, then we are being redirected back. And then we are clicking on that button. So maybe we could rephrase it just a bit, we will change the scenario to purchasing a video when I'm logged out. And then I will change the benefactor to rather than visitor, why don't we say viewer. Alright, so now given I'm not logged in, and there is an example lesson, that's all good. When I go to this lesson page, then I will change this. What should happen at
why don't we say viewer. Alright, so now given I'm not logged in, and there is an example lesson, that's all good. When I go to this lesson page, then I will change this. What should happen at that point? Well, I should be asked to sign up, then I should be asked to sign up. Okay, let's run Behat again, but append that new snippet. And give that just a second. Okay, let's go back, feature context. And now to verify that this works, I'm just going to assert that I see some kind of link on the page. I can do that again using make context by saying assertPageContainsText. And that should be purchase it for $4. And maybe just to make it a little more readable, we could add one more, mostly for context, three ways to watch this video. Okay, let's run the test again and see if it works. All right, it passed. So now I want to write another scenario for when I'm logged in.
Add Logged-In Scenario3:54
Okay, let's run the test again and see if it works. All right, it passed. So now I want to write another scenario for when I'm logged in. Once again, this will be JavaScript. And the scenario is purchasing a video when I'm logged in. Okay, this time, it'll be a little different. So given I am logged in without a subscription, and that's important, I'm logged in, but I don't have any kind of recurring subscription. Next, I want to assert that there's still this example lesson. I want to assert that there's still this example lesson. And also, when I go to this lesson page, what should happen? Well, I want to purchase the video. So and I purchased the video, then what? Well, then I should be able to download it. Okay, let's run it again and append the snippets. Okay, so let's go back to feature context. Here we are. And now for this method,
then what? Well, then I should be able to download it. Okay, let's run it again and append the snippets. Okay, so let's go back to feature context. Here we are. And now for this method, I need to decide how we want to do this. Do we want to assume an existing account? Or if somebody is logged in without a subscription, do we actually want to go through the process of having them sign up? And which one you choose just kind of depends upon what you're trying to test. In this case, I'm going to have them actually sign up. So when I say given I'm logged in without a subscription, behind the scenes, I'm actually going to create a new User. So we can do that by extracting a little bit. This time, I'm going to create a signup method. And I am going to pass a query string here plan=none. This is just a way to say we want to sign up, but we're not going to set a subscription plan. Now we can create that method. And again, set the query string to null.
string here plan=none. This is just a way to say we want to sign up, but we're not going to set a subscription plan. Now we can create that method. And again, set the query string to null. Now if we scroll up much of this, which by the way, we will refactor can be removed. So let's cut all of that out and paste it within here. Also, I don't think I need to bother with a factory here, we can just add this stuff manually. So we'll do jtron. The email will be jtron@example.com. The password will be foobar. Repeat the password will be foobar. And then the credit card data can remain the same. Next, we've just called the method here, but we haven't specified what page we're on. So I will say this->visit, signup, and then pass in the query string if there is one. Okay, so let's go back and read this. Given I'm logged in without a subscription. So behind the scenes that translates to us visiting the signup page and signing up for
Okay, so let's go back and read this. Given I'm logged in without a subscription. So behind the scenes that translates to us visiting the signup page and signing up for an account that does not have a subscription. That means this stuff down here, it doesn't need to be there. It's not applicable to the method. Alright, so what else? And there is an example lesson. Well, we already wrote the logic for that in the previous scenario. Now, and I purchased the video. So if we go back, so now we can update this to press button, like I demonstrated. And the text for that button is watch this video for $4. Now next, I'm going to basically pause for about 100 milliseconds. And the reason why I'm doing that is, remember when I showed you how we click on that button, and then a little alert dialog pops up with JavaScript? Well, those are a bunch of animations that are occurring.
is, remember when I showed you how we click on that button, and then a little alert dialog pops up with JavaScript? Well, those are a bunch of animations that are occurring. So we want to give that just a second to process. So I'm going to tell the hat just to sit tight and wait for about 100 milliseconds. But now that should be it. So let's go back to our feature context. We purchased the video by clicking the button. And then finally, I should be able to download it. And we verify that by going to the purchases page that I showed you and verifying that we do see that same lesson. Once again, don't forget, if we ever need this to be dynamic so that we can pass in the name of the lesson to verify, then of course, you can extract a property at that point. So now let's see if we did everything correctly. We run the test. And now we have two scenarios. Give it just a few seconds. I always think of this as like an
Add Payments History Scenario7:39
extract a property at that point. So now let's see if we did everything correctly. We run the test. And now we have two scenarios. Give it just a few seconds. I always think of this as like an automated robot that just does all the work for you. It clicks on the button. And there you go, everything passed. So now I want to create one last scenario. And then I think we will be done with this series. Once again, it will be JavaScript. And this time, the scenario is when I want to review my past charges. So we can save reviewing my past purchase charges. All right, so let's set up the same given, given I'm logged in without a subscription. And then also, I want to assert that I have purchased a video. So we will say and I have purchased and then the name, example lesson. Next, when I go to the payments page, then what? Well, then I should see a $4 charge. Okay, let's run this and append our new snippets.
purchased and then the name, example lesson. Next, when I go to the payments page, then what? Well, then I should see a $4 charge. Okay, let's run this and append our new snippets. Okay, we have our pending steps. So let's take a look at that. All the way here at the bottom. So the first step is this will accept the lesson name that we passed in. And now how can we assert that we have purchased this? Well, I could say I purchased the video, we already have that method. But also before we do that, we need to make sure that the lesson does exist. Once again, we have these methods, there is a lesson and then pass it in. So remember, when we created that, that will simply create the lesson in the database. Next, we need to visit that lesson page. So once again, we have a method for that, I go to this lesson page. And then finally, we purchase the video where we will click on that button to buy the video.
visit that lesson page. So once again, we have a method for that, I go to this lesson page. And then finally, we purchase the video where we will click on that button to buy the video. Okay, so what's next? I go to the payments page. Well, we can do that by simply visiting /admin/subscription/payments, where it will simply list out every charge that has been made to the card on file. Finally, to finish this up, I should see a charge for how much. And then to verify that, we could simply do this assertPageContainsText in the amount provided. Alright, so why don't we try this out? This time, rather than running all of the scenarios, if you ever just want to run a single scenario, well, you can specify the line number to start at. So if I turn on line numbers, we can start at 23, but hat features, specify the name of our feature, and then start at 23. And there we go, it seems to be working. So because that moved really fast,
Verify Payment History UI10:12
we can start at 23, but hat features, specify the name of our feature, and then start at 23. And there we go, it seems to be working. So because that moved really fast, let me show you what that looks like. Here I have a fresh new account. So if we go to settings, and then down to payment history, you'll see that we haven't yet purchased anything. Okay, let's buy something. Any of these should be fine. Watch the video for $4. Now it's viewable. And if we go back to settings and check out payment history, there we go. Now it's listed. And even better, we have a specification to describe and verify that this works as we expect. Alright, so good job. We now have three scenarios for this new feature. All of the tests are passing. So that means it's ready to deploy to laracasts.com.
feature. All of the tests are passing. So that means it's ready to deploy to laracasts.com.
