Full series and write up - https://codereviewvideos.com/course/let-s-build-a-wallpaper-website-in-symfony-3/video/tested-image-preview-sort-of
One of the nice bonuses of our implementation is in showing a user their existing, uploaded wallpaper image file when editing. Let's test this, too.
Show More Show Less View Video Transcript
0:00
Hi I'm Chris from CodeReviewVideos.com and in this video we're continuing on with our
0:09
test driven approach to wallpaper updates and this time we're looking at adding in the
0:13
image preview feature which is when we go to edit or update an existing wallpaper as
0:19
long as it's got an image associated with it we would like to see that inside the Easy
0:23
Admin Bundle backend. Now there's a bunch of ways that we could go about testing this and the only way that
0:28
we're going to cover in this video is to do so using PHP spec but because this is a visual feature
0:33
you may wish to use a tool like Selenium and a library such as Codeception or BHAT to actually
0:38
test that that behavior the behavior of seeing the image come on the screen is happening as expected
0:44
because we're only focusing on the PHP spec side of things we're not going to be able to test
0:49
every single part of this process specifically we're not going to be able to test the template
0:53
if we look under app, resources, views. And then under our form
0:58
we've got our image underscore upload HTML twig template and our PHP spec test is not going to be able to touch on this
1:05
Instead, we're going to focus on testing our uploaded file type. And to begin with, we'll test the existing implementation
1:11
which is all the stuff that you see on the screen currently. And then we'll copy in parts of the implementation
1:16
from our untested approach, which predominantly will be this piece around build view
1:21
where we're wanting to add in a file URI, depending on whether we're in a create or update screen depends on whether we want to have access
1:28
to a path on that file uri or not so as it stands currently we don't yet have a test for our uploaded
1:34
file type so let's go ahead and create one and i'm going to run this straight away to make sure that
1:41
php spec is passing and that looks good so i've gone ahead and i've opened up the uploaded file
1:46
type and the uploaded file type spec and we're going to get started by adding in a new test
1:51
And in this first test we gonna check what happens when we call build form So if we mock the builder then after we called build form we would like to assert that the add method was called with the arguments of file
2:04
file type class, and the options of multiple being false. So we'll call our function, it can build a form
2:12
And to get ourselves started, we'll call this build form. And we know that build form expects to be given an instance of form builder interface
2:19
and an array of options. So we'll mock the form builder interface and even though we don't own it
2:25
because it's an interface we should have no problems here. We don't need to mock the array
2:29
we'll just pass in an empty array anyway. And so our assertion is that after build form has been
2:35
called our builder mocks add method should have been called with the arguments as they are in our
2:41
existing function so we'll say should have been called let's make sure that we import this file type class and let's see where we get with that
2:54
next up we need to test the build view method and straight away we can see that our form view
3:01
which we will be using here is not an interface and even worse we're using a public property
3:07
which is this array of vars but even so let's give it a shot and see where we get so create a
3:13
new function which says that it can build a view and we'll know that we need to call this build
3:19
view and build view takes three arguments the view the form and the options so say we'll have a view
3:24
variable a form variable and an empty array of options now we have neither the view nor the form
3:30
So I'll just try and get PHP spec to mock these. And our assertion here is that after we've called build view
3:38
whatever we have set as the full underscore name on our view vars
3:42
should be concatenated with the string open brackets file close brackets So on our view we start off by setting on the vars this full underscore name key to have the value and we just have QWERTY as the value
3:58
So our assertion will be that after we've called build view, our view vars full name should return QWERTY open brackets file
4:08
Okay, let's give this a shot. And unfortunately that doesn't work. We can see here an indirect modification of overloaded property
4:16
vars has no effect so that's a bit of a scary looking notice that we're getting there
4:21
so let's see what we can do so we already know that we shouldn't be mocking what we don't own
4:26
so let's try refactoring this we'll start off by creating a view which will be a new instance
4:31
of the form view and is that good enough to get us to where we need to be well let's try our tests
4:38
okay slightly different problem now we're getting an exception call to a member function should
4:44
return on a string so here we are view vise full name should return and then this string so the
4:50
problem is that should return is being called on whatever is set into view vise full name which is
4:56
a string so that's where the error is coming from we've set a string here and then we're trying to
5:00
call a method on that string so that's never going to work so we're in a bit of a problem here we
5:05
can't mock it and because we don't own it as in we don't own the form view there's not a lot we can
5:10
do there to change it. I don't want to get into this situation where we're trying to wrap that as
5:14
well. So I'm going to cheat a little bit here and I'm going to use a different library to get myself
5:20
around this problem. So we're going to use the assert library from web mozart and if you aren't
5:26
aware web mozart or bernard schussick hopefully I'm pronouncing that right was the main creator of
5:32
symphony's form component. So there's a little bit of irony there that we're having to use a different
5:36
one of his libraries to get around the problem with another of his libraries, but hey ho
5:41
So that all looks like it's gone through okay. So I going to get rid of this should return statement down the bottom here and instead I going to do an assert the same so this is checking that two values are the same and what we want to do is check that after build view has been called the outcome of view bar full name is equal to qwerty
6:00
open brackets file close brackets let's try that and that looks good now just to be certain that
6:08
we've not just cheated completely here let's try adding another assert and we'll say assert true
6:13
on false so we should expect this to fail and it does so that's quite nice we know that our
6:19
assertion is actually working as we intend so all we have left to test now is the configure options
6:27
method and again we can see that that is not an interface but we're going to try our look all the
6:32
same so we'll say it can correctly configure options and again we'll say this configure options
6:38
which in this case is going to take a resolver so start off by trying to mock that
6:43
and a little bit of truth from me here even though we shouldn't mock what we don't own
6:49
typically i start off by trying to and only really adapt if i can't now this may be going against
6:56
like best practices and whatnot but honestly it works in my projects and as i say i adapt when it
7:01
doesn't so for me it's good enough your opinion may differ and that's okay too and all we want to
7:07
do here is check that this set defaults method was called on our resolver so we'll say should
7:13
have been called and we will need to make sure that we've got the symphony uploaded file as a
7:18
use statement inside our spec let's give this a shot too and that's looking good so this brings
7:25
us to a point where we have a working test spec for our existing implementation and now with that
7:30
test spec in place we can confidently make changes knowing that if all our tests are passing at the
7:35
end we didn't break anything and we're going to get on to changing this uploaded file type in the
7:39
very next video
