This article is from medium—-; Welcome to our php&Laravel learning group: 109256050

You need to start using tests

That’s what I tell myself every day. Like many people, I don’t test my code the way TDD suggests. I’m using testing now, and have been doing so for the past six months, and there’s a long way to go.

I decided to learn testing while working on a complex legacy project. The code is so brittle and rigid that we break it as soon as we add it. New features? Achieve and destroy something! Fix the bug? Create another one.

This was a big problem and got me started trying to test it.

The first tool is PHPUnit. As stated on the official website

PHPUnit is PHP’s test framework for programmers.

This is an example of the Unit testing framework xUnit architecture.

So, PHPUnit is a testing framework to help you create your projects, unit tests. It provides several functions to test the results of your code and produce the same good output as those test results.

Since I started thinking about testing, reading, and talking to people about testing, I’ve found another great tool that complements those previous units of testing: Behat, a PHP framework for BDD.

BDD (Behavior Driven Development) is a development process derived from TDD (Test Driven Development). These acronyms are not important now, but you can explain your tests in more natural language that non-technical people can understand.

This language, called Gherkin, is used to describe the expected behavior being tested. Using Gherkin looks like this

Behind this description, whenever a specified method described in phpDoc has a regular pattern matching the corresponding PHP code, the code implements these steps through an SDK, application, or Web system, simulating what a real user would do

Working with Behat is so smooth. After everything is configured correctly, you start writing all possible scenarios to test a feature. Once you run Behat, it gives you all the method templates you should add to your PHP environment classes in order to implement each step of the scenario

After that, you start writing the actual code for each step and continue repeating the cycle.

  • Implement a step of PHP code
  • Run the test
  • If it works, proceed to the next step of the PHP code
  • If there is an exception, fix it

After an hour and a half of configuring and reading the documentation, you can start using Behat, and by the end all you see is PHP code and you already know how to write it, right

Continuous integration

Continuous integration (CI) is a process — a way of doing something, and for us software engineers, that means creating software.

Simply put, it is the act of constantly integrating small blocks of code (perhaps several times a day) into a code base. The code will be tested without exceptions. CI helps you automate the build, test, and deployment of your applications.

With a few clicks, you can integrate your GitHub project with Travis CI, and each push to the repository will run your PHPUnit and Behat tests that tell you if the final implementation features are ready and should be merged. In addition, you can use Travis CI to deploy code to production and staging.

Implementing a good workflow through a well-regulated program is great, and Travis CI can help you do this. Following this good start, discover how interesting it is to think about the software development process, not just the code itself.

Follow PSR-1 and PSR-2

If you didn’t know what PSR was before, you should now. PSR actually stands for PHP Standard Recommendation, and PHP-FIG suggests using them. PHP — FIG is an organization of members from the largest PHP projects, frameworks, and CMS systems dedicated to thinking about the future of the language, the ecosystem, and the PHP specification that should be followed

PHP has not had an coding style for a long time. I’m not that old, but every time I look at someone else’s project or library, they come in a different style. Sometimes putting the parentheses in one place, sometimes putting them on the next line, different ways to handle long lines of code, and other combinations of styles and preferences. What a mess.

PHP — FIG does a lot of other things, but comes up with a unified code, and they say “let’s stop worrying about code style, let’s all follow the same standard and start thinking about creating great software”. Now, whenever you look at someone’s code, you worry about how it works, not the format, the structure.

At the end of the article, there are nine accepted PSRs recommended solutions to common problems. But if you don’t know the criteria, use THE PSR-1 and PSR-2 as a starting point.

These standards set out the modern PHP coding style. Be sure to read them before you start. Don’t assume that you’ll remember everything when you write code, it’s a process, but in order for you to determine which specifications you’re using, there are tools that can help you do it. PHP CodeSniffer is a tool that you can find on Packagist and install using Composer. I don’t think this library name is the best choice because it contains two different tools, PHPCBF PHPCs.

Phpcs code sniffer, which scans your entire code for parts that don’t match the configured coding standards.

You can use multiple coding standards with PHPCs and you can even create your own. At the end of the code scan, PHPCs lists the code snippets that do not conform to the standard. It works so well.

Now, how do I fix all the bad code snippets? You can open all the files, change the code, run PHPCs until you see the error doesn’t show up, and repeat the process. It would be boring.

To solve this problem, a tool called PHPCBF of PHPcodesniffer came into play, or became a PHPcode beautification tool. It tries to fix all the bugs and make them conform to the same code specification without breaking your code.

Get in the habit of using PHPCS and PHPCBF to check your code before pushing it into your repository. This will ensure that everything you write is compliant, and once someone likes your project and wants to contribute, they’ll have no problem reading it.

The framework

I don’t want to spend too much time talking about frameworks, there’s a good one, each with its own pros and cons, and personally, I don’t like using these big frameworks that encapsulate everything. I like to use what I need.

If you need an HTTP client, such as Guzzle. If you need a template engine like Twig. If you need a router. Find the components that work for you, use them, and put them together to build your own application.

Symfony goes a long way towards this concept, you can use the entire framework as a project, or use any of the components you need as described above.

However, whenever I need to use a framework to write an application, I usually opt for a miniature framework. They are really small, provide the most basic components, and are very easy to customize.

My micro framework of choice is Slimframework, which I think everyone should try out.

By the way, for anyone new to programming, I really recommend that you try to build your own framework before adopting it and using it. This will give you an overview of how the whole thing works. And make it easier to understand when you adopt larger frameworks later.

Modern PHP tools

Let’s end this article with a list of linked components and tools and libraries that, to me, represent the great ideas of modern PHP:

  • Slimframework: A nice and cool mini-framework
  • Symfony: A larger framework with great and reusable components
  • Guzzle: A simple and easy-to-use HTTP client
  • Behat: A behavior-driven development framework
  • PHPCS/CBF: Code review and beautification
  • Faker: Virtual data generator
  • Psysh: Real-time development console full of great features
  • Composer: Dependency management and other useful features
  • Packagist: third-party repository
  • Twig: Template engine

I know the title is pompous, but what I really want to say here is that PHP is improving, and so is its ecosystem (probably faster).