Laravel Package Development – e02 – Getting started with PHPUnit, Composer and PSR4 Autoloading

Laravel Package Development – e02 – Getting started with PHPUnit, Composer and PSR4 Autoloading


Alright, let’s get started, so we know
that our project name is gonna be Press so let’s make a directory and name it
Press. Let’s change into that directory now you may be familiar with the ‘git init’
command but you may not know that composer also has an init command and
when you run this command it will actually walk you through creating a
very basic composer.json config file. Now, my vendor is vicgonvt and
our package name is press. In description, let’s write a description. Perfect! Author
is correct so I’m just gonna hit enter minimum stability, let’s leave that blank.
In package type, leave that blank. For license, enter your appropriate license
here. In my case, I’m just gonna put MIT. ‘composer init’ also allows you to
require dependencies interactively. Now, we’re not gonna do it in this way but
you can. So, finally it’ll give you a preview of what we’ve done and hit enter
to confirm. Now, in PHPStorm let’s take a look at this composer.json file so we have a name, description, license and authors and at this time we
have no required packages just yet so let’s get started with that. Back in the
terminal, we’re gonna say ‘composer require – -dev orchestra/testbench’ now, test bench will actually bring in the same environment that you’re used
to in all of your Laravel projects and it’s gonna allow us to develop our
package in the same manner as you would a regular Laravel application. Now I do
want to go back here and I want to show you some of the packages that it pulls
in. First and foremost, the Laravel framework, another package you may be
familiar are some of these symfony components as well as PHPUnit. so, now
that our installation is done, let’s go ahead and initialize our git repository.
‘git init’ and we have an empty repository ‘git status’ and we have a couple of
directories in here. Now, you may not be used to seeing the vendor directory and
something like a ‘.idea’ directory and this is because Laravel actually ships
with a very nice ‘.gitignore’. Now, ‘git ignore’ file will actually instruct git
to ignore certain directories or files and it does this because they’re not
supposed to be in version control. Things like your vendor directory, that gets
created when you run composer and a ‘.idea’ directory, in my case, that’s a
PHPStorm file so that doesn’t need to be inside your source control. Let’s visit
the Laravel GitHub page and take a look at that git ignore file. now, when we look at this
file you see we have that ‘.idea’ and vendor and a couple of other directories
here so what I actually want to do is bring in this entire file into our
project. I’m gonna select everything I’m gonna copy it switch back to PHPStorm
and go ahead and make a new file. New file. I’m gonna name this file ‘.gitignore’ and I’m simply gonna paste that in. Save, ‘git status’ and there we go, those
files are done so we’re ready for our first commit ‘git add .’ ‘git commit -m
initial commit’ perfect we’re making great progress, the first
thing we’re going to talk about its directory structure. This is one of those
topics that becomes a little bit complicated when you start package
development because you’re not sure where to put your files but rest assured,
there’s an easy approach for this and the approach that we’re gonna take, is to
simply use the exact same directory structure that we are used to in our
Laravel packages. Let’s go back to the Laravel GitHub page one more time and
take a look at the directory structure. so we have an app directory, bootstrap,
config, routes, storage, tests, and so on and so forth. We’re gonna follow this
exact same directory structure with one exception, instead of an app directory,
we’re actually gonna call it ‘src’ which stands for source. In the package
community, everybody seems to agree that the main directory should be ‘src’ so
we’re gonna go with that convention. With that being said, let’s head back to our
terminal and make a couple of directories. make directory ‘src’ and let’s
also make a couple of directories for our tests. so start with the test file
and inside that, we’re gonna have a tests feature test and we’re gonna have
a unit test. So, back in PHPStorm, we can take a look
at that test directory and our source which at this point, of course, is empty.
Now, one thing that we are not leveraging just yet is auto-loading so let’s go
back to our composer.json file and add a couple of entries. The first one is
‘autoload’ We’re gonna do this in psr-4 and the first thing we’ve got to tell it
is what our namespace is going to be, in my case ‘vicgonvt\press\’ and we also
have to tell it where the main entry point is for that namespace in our case
the ‘src’ directory, we’re also going to autoload our tests, so, to do that, we’re
going to add another entry ‘autoload-dev’ also in psr-4 and a slightly different namespace. ‘vicgonvt\press\tests\’ so, where is the
entry point for that? Well, that’s gonna be in our test directory and that’s it!
At this point, we are auto-loading, we have testing in place and we’re ready to
start running PHPUnit, let’s try it. ‘phpunit’ and oh boy!
That didn’t go as planned! Right? We’re used to running ‘phpunit’ and everything
kind of just working but PHPUnit actually has a lot of options, now the
reason why we never see these options is because Laravel actually ships with a
little file that tells PHPUnit what to do. Let’s take a look at that file. Back
in the GitHub repo, there’s a file in here called ‘phpunit.xml’ and if we open
that, we actually see all of the options up here, as well as a unit test suite and a
features test suite. Same as the ‘.gitignore’ let’s actually bring in this
entire file into our project. I’m gonna make a new file and I’m gonna call it
‘phpunit.xml’ PHPUnit is actually gonna look for this file, when you run
‘phpunit’ so, let’s go ahead and paste this in and we can go through this file
but basically all of these are the options, then we have our test suite
section and then we have a filter for which files are actually going to be
tested and finally we set up some of the environment files. We don’t need these
right now so I’m actually gonna erase them and the only other thing you have
to change is instead of an app directory it’s gonna be our ‘src’ directory. I’m
gonna hit save, switch back to iTerm and run ‘phpunit’ one more time and there you
go, the tests have been executed. One last thing I do want to do, is just write a
very simple test that will allow us to really test something. So, I’m gonna make
a new PHP class and we’re gonna call it initial test, my namespace ‘vicgonvtPressTests’ You may be used to simply running
‘php artisan make:tests’ so, you’re not used to setting up your tests from
scratch but one thing you may know, is that every test extends a testcase, now
in the case of Laravel, it typically extends the testcase for the
Laravel framework but because we’re developing a package, we actually have to
extend the testcase that ships with the orchestra test bench, so just look for
that now and here it is, The full namespace is
‘orchestratestbenchtestcase’ so we see that got imported up here and let’s
write our very first simple test, Now, one thing I want you to notice here is how
we have all the assertions that you’re used to seeing already, so we’re simply going
to assert true and pass in true and of course, that’s going to pass. Let’s go
back to iTerm, we run ‘phpunit’ one more time and there we go, we have a green
test.

3 comments

Add a Comment

Your email address will not be published. Required fields are marked *