TDD with the Liskov Substitution Principle
The LSP was introduced by Barbara Liskov. The way that I use it is that an implementation of an interface should be replaceable with another implementation of that interface without changing the behavior. If you are extending a superclass, the child class must be able to substitute the superclass without breaking the behavior.
In this example, let’s try adding a business rule to reject toy car models that were built on or before 1950.
As usual, let’s start with a test:
- Open the
YearValidatorTest.phpclass we created earlier and modify the test class with the following:
codebase/symfony/tests/Unit/Validator/YearValidatorTest.php
<?php
namespace App\Tests\Unit\Validator;
use App\Validator\ToyCarTooOldException;
use PHPUnit\Framework\TestCase;
use App\Validator\YearValidator;
class YearValidatorTest extends TestCase
{
/**
* @param $data
* @param $expected
* @dataProvider provideYear...