در حال بارگذاری ...

Defining Package Scope2:18

The caption is what displays on the video. The transcription will be in our slide-out modal here. That makes perfect sense. Okay, and things like that are important, because they will determine what the class names are, what the package name is, what your method calls might be. It's very important. Okay, so let's get started. I'm going to begin by thinking of this as a Composer package, even if I never release it to the world. Even if it's only meant to be used by me, that's fine.

it to the world. Even if it's only meant to be used by me, that's fine. When you build these things, always start off by scratching your own itch. Don't try to plan for every potential use case. I know there's all these different transcription file types that you might need to work with, but I don't need to worry about that. It's irrelevant to my needs, and I only want to scratch my own itch right now. So I will call this LayerCast. I'll put it under the LayerCast namespace, and we'll do Transcriptions.

Composer and PHPUnit Setup3:05

So I will call this LayerCast. I'll put it under the LayerCast namespace, and we'll do Transcriptions. And I'll leave the others blank. Yeah. Okay, cool. So that'll set up my Composure.json file. Next, I want to require phpunit, phpunit for development. We're going to use test-driven development to drive this out, and I think that'll be a great way to go. And then finally, I want a quick phpunit configuration file.

a great way to go. And then finally, I want a quick phpunit configuration file. So let me remember. What is that called? Generate configuration. Bootstrap script. Again, usually the defaults are correct. So notice tests go in the tests directory, source files go in the src directory. That's fine. Close that out.

Creating Project Structure3:55

That's fine. Close that out. And I'll go ahead and build up my directory structure. src. And then another one for tests. Okay. And then for this example here, I'm going to put this into a stubs directory. Because maybe I'll work that into one of my tests, or maybe even do another one here. Like let's duplicate this, and we'll call it basic_example. And then I'm going to condense this down drastically.

Writing First PHPUnit Test4:18

Like let's duplicate this, and we'll call it basicExample. And then I'm going to condense this down drastically. We'll just make it something like that. Here is an example of a VTT file. And that's it. Okay. Let's get started. I'm going to build my first class. And we're working with transcription, so why don't we call it TranscriptionTest. And the namespace, I guess, will be Tests.

And we're working with transcription, so why don't we call it transcription test. And the namespace, I guess, will be tests. Okay. So this will extend phpunit. And we'll do our first test. What is the simplest thing I can do here? How about something as simple as it loads a VTT file? All right. Let's write the code I wish I had. Maybe we'll have a Transcription class, and I will give it the path to a file.

Let's write the code I wish I had. Maybe we'll have a transcription class, and I will give it the path to a file. So this would be something like stubs/basic/example.vtt. And what do I expect? Well, I expect to load this full string. Now I could assert that it just contains a portion, or I could do something like $expected equals file_get_contents(...). So I'm going to do it again. And then I can say this->assertEquals($expected, $transcription); So yeah, at this point, $transcription->load() basically just does file_get_contents(...).

And then I can say this assertEquals expected, and then compare that against the transcription. So yeah, at this point, transcription load basically just does file_get_contents. So in this case, it looks a little suspicious whenever your test is just reproducing the class you're testing. It's suspicious. But in this case, I don't think it's really a problem. Okay, so we know this is going to fail, but let's give it a try. And yeah, transcription not found in line 12. So it has no idea what this is. Okay, let's go ahead and create our Transcription class.

Configuring PSR-4 Autoloading6:08

So it has no idea what this is. Okay, let's go ahead and create our Transcription class. There we go. And then I'm going to use that. So use Transcription. Give it another run. Okay, so now class LayerCast Transcription slash Transcription is still not found. That's probably because we need to configure the autoloader. So we're using PSR-4 autoloading. So I will say autoload, and this will be PSR-4.

So we're using PSR-4 autoloading. So I will say autoload, and this will be PSR-4. And let's say our namespace was Laracasts\Transcriptions. And where's that located? In the src directory. Okay, let's run it again. And it still can't find it, but that's just because we have to run composer dump-autoload. I wish we didn't have to do that. But it's required. Okay, so we run it again.

But it's required. Okay, so we run it again. Aha, we've changed the error. So we're making progress. Now we're trying to call a load method that doesn't exist. Okay, let's click through. And this is a static initializer probably. And this will expect a path to the file. All right, we give it a run. file_get_contents test.

All right, we give it a run. file_get_contents test. Oh, wait, test, test, that's not right. I bet you saw this and you've been waiting for me to figure it out. Okay. Okay, let's give it another run. Here we go. Failed asserting that null matches the contents of that file. Okay. Again, we're changing the error.

Okay. Again, we're changing the error. And whenever you do that, you're making little bits of progress, usually. So what's going on here? void method. Yeah, okay. That's going to return a string. So how do we make this work? Well, again, this is where I said it's a bit suspicious because right now, load, hmm, will just start with file_get_contents.

Well, again, this is where I said it's a bit suspicious because right now, load, hmm, will just start with file_get_contents. But my guess is we'll probably change this because this isn't overly useful right now. The test does not have a covers annotation, but it's expected. I don't want to do that. Let's turn that off. Run it again. And we get green. But we don't have colors. So let's turn on colors here.

Refactoring Load API8:13

But we don't have colors. So let's turn on colors here. All right, one more time. There we go. So now that I'm at green, I can refactor this a little bit. Really, I think load should return to me an instance of transcription. So why don't we start by saving this to file, and that will be a string. And then I want to set it. But of course, I can't do this because we're in a static method. So this is where we could, within here, initialize our class, and then set it, and then return

But of course, I can't do this because we're in a static method. So this is where we could, within here, initialize our class, and then set it, and then return the instance. Okay, but now it's not going to work, right? We run the test, and now load is returning an object rather than the string. But still, I kind of like being able to call load, and then if I ever need to use the response there, it immediately gives me the string. If you want that to work, what we could do is set toString to declare what should happen when this object is used as a string. And if that's the case, maybe we just return file, and that will return a string, of course.

when this object is used as a string. And if that's the case, maybe we just return file, and that will return a string, of course. So we give that another run, and it's passing. But now we've changed it up just a little bit. So I think this is a good stopping point for lesson one. We figured out the terminology, we set up our initial package configuration, and then we wrote our first basic test. In the next episode, we'll keep going.

Create a Composer PackageInstall PHPUnitPSR-4 Autoloading

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