Practical examples of immutability in PHP

A mutable object can be modified after its creation, an immutable cannot.

Image for post
Image for post

An immutable object will remain in the same state as it was created. Design and implementation will be much easier and consistent. In the case of problems, locate a potential bug is faster due that it won’t have side-effects.

Although, creating immutable objects sometimes require more code, and it doesn’t fit in all scenarios (entities need to be mutable for example).

Mutability examples and their impacts


Being up-to-date and avoiding conflicts

Image for post
Image for post

Have you ever wondered how you could collaborate with open-source projects but you didn’t know how to start? It couldn’t be easier. Take a look:

  • Fork the repository into your account.


Dummy, Stub, Spy, Mock or Fake

Image for post
Image for post

Test Doubles

A Test Double is an object that can stand-in for a real object in a test, similar to how a stunt double stands in for an actor in a movie.

As I wrote in “The importance of the Tests in our Software”, there are several types of tests. They are also known as Test Doubles instead of “Mocks”.

The five types of Test Doubles are:


Image for post
Image for post

TL;DR: No, and let me explain you why.

It causes bugs when refactoring due to the highly coupling

When we use reflection our tests get too fragile, we are allowing our tests to know so much information about the real implementation.
We need to hard-code the method name and we are coupling our test method to the production code.
Furthermore, we need to write a lot about boilerplate to test a simple method.

Q: I need to get at least an 80% of code coverage, how can I get it without Reflection class?
A: You should test ONLY your public methods and depend on the variables we pass, we should reach all the possible paths. …


1.- What is a test?

A test is an empirical assertion which demonstrates the behaviour of an expected functionality from something.

Image for post
Image for post

The tests are classified by what they verify, the most important ones are the following:

Functional tests

  • Unit test
  • Component test
  • Integration test
  • System test
  • Smoke test

Non-functional test

  • Compatibility test
  • Security test
  • Stress test
  • Usability test

A software needs all of them, but the most important concerning developers are unit, integration and functional tests.

2.- Differences between test types

Unit test

About

Jesus Valera Reales

Competitive, entrepreneur and autodidact. Hard worker, lover of technology and free software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store