تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Testing Without Fake0:00

Okay, so that we know that our test with our fake Twitter client is working, so we've seen this in the last video, we also want to make sure now that it, of course, also works without the fake client. And of course, we also already know that it's not going to work because we haven't implemented the real Twitter application, the Twitter client yet. And I'm going to test this just by commenting out the fake method where we're going to switch out the real client with the fake one, and also the assertion. And now let's see what is going to happen when we run this now. Call to undefined method tweet on the facade. Yeah, so now that's happening, it's also something that you know when you start working with

Creating Twitter Client0:40

Call to undefined methods tweet on the facade. Yeah, so now that's happening, it's also something that you know when you start working with facades. So let's take a look at our one. Now it tries to call the tweet method on the facade itself. But under the hood, every facade is using some kind of service class. And also here, there should be one service class, but we haven't implemented it yet. And that's why it tries to call the tweet method here on the facade itself. So this means for us, our next step is to create a new Twitter client. And let's just do this right here inside our app directory, and let's call this Twitter.

So this means for us, our next step is to create a new TwitterClient. And let's just do this right here inside our app directory, and let's call this TwitterClient. All right, and here we're going to have a constructor. And we're going to have here now another tweet method, similar to the one which we have inside our fake method. So we're going to get here a string, which is called status. We're going to return an array here. And now here, what we want to do is we want to call the real Twitter service, which we don't build ourselves, but which we want to implement here.

Installing TwitterOAuth Package1:36

And now here, what we want to do is we want to call the real Twitter service, which we don't build ourselves, but which we want to implement here. And the one which I want to use is called TwitterAuth by Abraham. And that's one I've been using already for many years. It's a very good way, and we're just going to install it now. Later, you can also see that in order to create a new connection, in order to tweet something, we need to have here some keys, which we need to provide to the TwitterAuth class. But let's start by bringing in this package now. All right, and now let's think again about how we want to deal with this. I want here to have access now to this new instance.

All right, and now let's think again about how we want to deal with this. I want here to have access now to this new instance. It's called TwitterAuth by Abraham, and we're going to call it just Twitter. And then here, what we want to do is we want to call through the Twitter service. This is now, I think, called post here, it's called post. And we need to provide here the path, which is for Twitter statuses slash updates. And then we're going to provide the status, and we can also do this here with the compact method because this will be an array with a key called status and the value what is inside here. And in order to get an array back, we're also going to return what we get back here.

inside here. And in order to get an array back, we're also going to return what we get back here. All right, let's run the test again, and let's see what's happening now. Call to undefined method tweet. All right, so we're still, the application is still trying to call the tweet method on the facade, but not inside our client. And that's because we haven't told our application yet that whenever the Twitter facade is looking for something called Twitter through the facade accessor, where do we have it again? Here we have it. So the service container is looking for a class with the alias Twitter.

Binding via Service Provider3:24

Here we have it. So the service container is looking for a class with the alias Twitter. We don't have it yet. And that's why we try to call the tweet method on the facade currently, which is not what we want to do. So this means the next step is to create a service provider where we're going to deal with that. So again, Laravel Idea helps me to create a service provider, and we're going to call this TwitterServiceProvider. And as you can see, we can already register it in our application.

this Twitter service provider. And as you can see, we can already register it in our application. That's also good. All right, so now we want to bind something inside our, or into our service container. So whenever a service is asking for the TwitterOAuth class, that's exactly what we are doing here inside our TwitterClient. We're asking here for dependency injection for this client, but we can't just get it because we need to provide also the keys. That's also what we have already seen here in the documentation. So in order to create a new instance, we need those keys.

That's also what we have already seen here in the documentation. So in order to create a new instance, we need those keys. And of course, level has no idea how to do this. That's why we need the service provider. So the second argument here is a callback. And here we see now what we want to return. We want to return a new TwitterOAuth class, which is the same, but now we're going to provide here some credentials in order to connect to our real Twitter application. And I'm going to copy this in here. I have already prepared this.

And I'm going to copy this in here. I have already prepared this. So these are the things that we need, the Twitter consumer key, secret token, and token secret. You can read more about this in the TwitterOAuth package. And we don't load this directly from our .env files, but from our config. And inside our config, there is a services.php config file here where we already see that this is done for other services as well, like PayPal, which we already did. And now let's also bring this in for Twitter. And this is also something that I have already prepared that you don't see me typing this.

And now let's also bring this in for Twitter. And this is also something that I have already prepared that you don't see me typing this. So this means inside your .env file, you also need now to provide all these keys. And on the documentation for the package, you also see where you get them. So that's not something that I'm going into any details here. All right, back to our ServiceProvider. So we're telling now when someone is looking for this class, we're going to return this instance with those credentials here. And then if we run the test, it will still not fail and the issue will still be the same. And now the reason is that we also tell now that when someone is looking for Twitter alias,

And then if we run the test, it will still not fail and the issue will still be the same. And now the reason is that we also tell now that when someone is looking for Twitter alias, which we have defined in our facade, that we're going to return this here, or actually we're going to return our real client. So this means we're also going to bind here to the service container to now we're going to use the key Twitter, which we have used in our facade. Now in the callback, we're going to return a new instance of our Twitter client, which we have created now. And I'm also going to use here the app helper method so that the service container can take care of creating this instance.

Debugging Test Failures6:22

And I'm also going to use here the app helper method so that the service container can take care of creating this instance. All right, let's run the test again. We should see now a different error. And we do, but it's not the one that I expected. Compact undefined $variable. Where does this come from? I think from here. Oh yeah, I think you're going to need to provide the string, which is status. Let's try it again.

Oh yeah, I think you're going to need to provide the string, which is status. Let's try it again. All right, type error. Client tweet return value must be of type array. Standard class return. And yeah, I think what we can do is just create an array from this object, which we get back. All right, the test did not perform any assertions. All right, but I'm pretty sure there's also a different error now being thrown. And just to make sure that we also see it, let's also disable now exception handling. The reason that I know that it should fail is because I haven't provided all the keys.

And just to make sure that we also see it, let's also disable now exception handling. The reason that I know that it should fail is because I haven't provided all the keys to my .env file yet. So there should be an issue about this. Let's run it again. Okay, still no errors here. So this doesn't bring us anything, but I think what we can do now in order to see what's going wrong here is dump out what we get back from this call to Twitter. And yeah, now we see that we get back an error, errors, bad authentication data. Okay, so this means most of the process is now working.

Configuring Credentials and Retesting7:51

And yeah, now we see that we get back an error, errors, bad authentication data. Okay, so this means most of the process is now working. We're now calling the real Twitter clients. This is working, this is fine, but we now also need to define the credentials. Okay, I've now provided the real credentials. I'm not going to show them to you because they are from a real account of mine. And just to be sure here, because I'm really now tweeting this out through a live account, I'm going to see testing here. So if anybody sees this, and now we're going to try this out in real. So I've set up the credentials for my LaraStreamers account.

So if anybody sees this, and now we're going to try this out in real. So I've set up the credentials for my LaraStreamers account. So this means if we're going to run the test now one more time, we should see a new tweet here and this will tell us if we correctly implemented this or not. Let's give it a try. No assertions here. Let's refresh this and yeah, we see the tweet here. So this means this has worked, but let's just delete this now so that nobody sees this. I need to switch my accounts here in order to do this because we don't want to confuse any people here.

I need to switch my accounts here in order to do this because we don't want to confuse any people here. All right, done. So this means we have now made sure that also the real Twitter implementation is working. Here I'm just making sure that it also works with the fake method. So let's try this one again. It should fail now because of the string which I changed here. There's testing here. So it's nice to see that this is working as well. And now this test is passing again.

So it's nice to see that this is working as well. And now this test is passing again. And I think now we have a real nice test here for our Twitter comment where we're going to send out a tweet for a new released course. And now let's also regenerate the helper code for the Idea plugin so that we don't see this underlined Twitter classes anymore. And you can see now also this application, PHPStorm knows what we're doing here and where these things come from, like the fake method. All right. So the only thing left here, whenever you're going to add some environment variables to

All right. So the only thing left here, whenever you're going to add some environment variables to your .env file, it's a good idea to also provide them to the example file so that whenever you install this again, where you don't have the environment variables inside here, it's a good idea to also add them here so that you know what are the variables that you're going to need. And in our case, these four are the ones that we need in order to work with Twitter. All right. So that's it. Thanks for watching.

So that's it. Thanks for watching. And I'll see you next time.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟