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

Use assertStringEqualsFile0:00

Alright, let's get back to work. Now, before I write my second test, let me clarify this a little more. It loads a .vtt file as a string. And actually, after I finished the previous episode, I remembered that there's actually a assertStringEqualsFile method with PHPUnit. So the file would be this path here, and then the assertString is what we got in response to this call. So if we give that a run, it passes. And now I could even inline this, I think.

So if we give that a run, it passes. And now I could even inline this, I think. Give it a reformat, like that. Give it a run. Still passes. And then maybe we can extract this here to a variable. File is fine. And here's what we get, and that still passes. Can make a big difference. Okay, what's the next test? Well, I think it might be useful to load this file into an array that eventually I could maybe loop over. Maybe for every single line, wrap it in a paragraph tag or an anchor tag.

Add lines() method1:36

Actually, most of the time that's how I feel. Especially at the beginning. I don't quite know, but five minutes from now I will have a better idea, and then I can return and make the method name more clear. So let's do this one more time. I'm going to transcribe load the file and I assume at some point this will move to a setup method for every test. So load the file, and maybe I call a method like lines, and that will give me an array where each item in that array is a single line from the file. So in this case we would have an array of

item in that array is a single line from the file. So in this case we would have an array of nine items or eight items. So let's give that a shot. What if I were to say assertCount eight and compare that. Let's see what we get here. We run it, and I get call to a member function lines on string. Oh, I'm sorry. Run it again. Yeah, now that's better. So we need to add a lines method. Now we know that's going to return an array,

Yeah, now that's better. So we need to add a lines method. Now we know that's going to return an array, and then for now I will slime it, and we give it a run. And of course this fails because an empty array has a size of zero when we really expect eight. So how can we turn the file we have into an array? Well of course we could explode it and use a new line as the separator. That would be one option. So for example if I were to say this file, we give it a run. There's that whole file. But if I were to explode it

file, we give it a run. There's that whole file. But if I were to explode it like so, we should now have an array. Yeah, that would work. Another option though is to use the file function. So the file function is similar to file_get_contents but it organizes it into an array. Now a quick little note here, you've got to be a little bit careful when using these functions because it all gets loaded into memory. So if I'm dealing with something that is a gigabyte large, I very likely need to be more considerate of that. I can't load that whole thing into memory. But in this case, for

a gigabyte large, I very likely need to be more considerate of that. I can't load that whole thing into memory. But in this case, for transcriptions, they're very small, especially for Laracast content. It's not something I'm worried about. But yeah, again, if you do need to process very big files, you would probably need a different path. So anyways, if I change this to file, yeah, now this would be equal to an array of the lines. In which case, this property name is no longer correct. So let's change it to lines. And then, right down here, all I would have to do is return those lines.

So let's change it to lines. And then, right down here, all I would have to do is return those lines. Give it a run. Field asserting an actual size 9. Oh, I'm sorry. I guess I counted wrong. Run it again, and now that passes. So if I were to have a look at this for dump, and we give this a run. Oh, and by the way, notice that it's failing just because of the var_dump. I think that's a configuration. If you want to turn that off, where is it? It's right here. Be strict about output. So let's get rid of this and that. There we go.

Fix string conversion4:48

where is it? It's right here. Be strict about output. So let's get rid of this and that. There we go. So now the test passes, but I still get a var_dump here, which is fine. Okay, so now, let's first run our full suite, because now I think this one's going to fail. And it does. We're expecting a string, but now we have an array. So let's go ahead and fix that. When we convert this object back to a string, let's implode it. Alright, give that a run, and now, yeah, everything is passing. So come back, get rid of the var_dump,

Filter irrelevant VTT lines6:24

lines, or maybe discards. It discards irrelevant lines from the VTT file. Okay, so let's copy this one more time, and then we'll do a setup method. So load this file. Let's extract that. And let's see. When I convert it to a string, this string should not contain what? Well, I don't want it to contain WebVTT, because that should be removed. So let's try this.

where we say instance, discard, let's just be on the nose. Discard irrelevant lines. Like that. And we'll give it the array. Yeah, at least that's a first step. Okay. Down here, this will be protected, discard irrelevant lines. And this will be the array of lines. And that will return an array. Okay, so how do I discard the irrelevant lines? Well, maybe we should go over every single line and check for some kind of criteria. So, for example, I could say array_filter,

every single line and check for some kind of criteria. So, for example, I could say array_filter, and I'll filter over the lines. And for each one, we are going to return a Boolean that determines whether or not this particular line gets to stay, basically. And in this case, it gets to stay if the line does not equal, to start, this one on top. If the line doesn't equal that, then we're good. So let's return that. And give it a shot. Hmm, it fails. It's still being included. Probably because the current line

include? It shouldn't include any line breaks or these heading numbers. So ultimately, when we're done, we will have one, two, three, four lines. Is that right? Maybe I should say this assertCount for transcription lines. When we're done, that's what I expect. Give that a run and it fails. We've gotten rid of one, but we still have the others. Okay. So let's say, let's go back and back to our filtering. So a line is accessible if it's not WebVTT. Actually, real quick, let's say line is trim(line).

and back to our filtering. So a line is accessible if it's not WebVTT. Actually, real quick, let's say line is trim(line). Okay. And it can't be an empty line like this. And it also can't just be a line with a single number on it. So if is_numeric(line). It can't be that. Okay. Let's try that out. Awesome. That works, too. Okay. So if I come back to my test now, let's var_dump(transcriptionLines). And with any luck,

let's var_dump transcription lines. And with any luck, yeah, so now notice I have an array of that, that, that, and that. But also notice I still have these line breaks, so they need to be trimmed at some point. Where should we do that? We could do it, hmm, maybe what I could first do is run everything through array_map where we pass it to trim. So basically, yeah, for every single line in that file, we're going to run trim on it, and then we run array_filter on it.

yeah, for every single line in that file, we're going to run trim on it, and then we run array_filter on it. And actually, you know what I could probably do is switch this to an arrow function. And oh, yeah, it's a little rough, isn't it? Let's see, maybe something like that. Anyways, if I give it a run now, notice every single line is properly trimmed. And actually, notice while we're here, I can inline that as well, can't I? Run it again, and we still get green. Okay, so now if I go back to my test, I think these are going to fail now because we've adjusted the count. So let's just fix that here.

simple like, let's see, let's bring this out. Transcription, and then this assertStringContainsString, and let's just grab some of it. Yeah, here is A, so it should have that. And does that work? Yeah. And then, hmm, I could do all of these. I think as long as those two are present, I think we're fine. Give that a run, yeah. So now, in the next episode, I want to give myself a little more flexibility. Right now, yes, I can load a file,

Rekey and plan objects12:16

yeah. So now, in the next episode, I want to give myself a little more flexibility. Right now, yes, I can load a file, and I can fetch all of the lines that I care about as an array. But it's still, well first, it is not keyed correctly. I'll fix that real quick before the end of the video. But then, each item is still just a segment, when really I'm thinking of this as a pair, and then this as a pair. So whenever I have that term in my head, like these things are connected, they go together, I will usually extract that to an object, or a class. And maybe that class is something as simple as

I will usually extract that to an object, or a class. And maybe that class is something as simple as a line. Because really, each of these are their own lines in the file, but for all intents and purposes, this is a single line, and this is the timestamp that the line occurred. So we will take a look at that, but yeah, I do want to fix the incorrect keys. So because we are filtering over this array, some items are being removed from it. But when they're removed, you don't rekey the array. And sometimes that can be a problem for iteration or looping. So what I usually do is pass it to array_values

And sometimes that can be a problem for iteration or looping. So what I usually do is pass it to array_values that will give you the same set of values, but it will rekey the array. So now notice, it starts at 0, 1, 2, 3, whereas without it, you had whatever order they originally occurred, minus the ones that were removed. So, get rid of the var_dump, run the tests, we are at green, so I will see you in the next episode.

Static Constructorsfile() FunctionMemory Concerns

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