Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
Here, an example is used as a TDD (Test Driven Development) approach.
Only test file is called from here.
-
There are some products with code, name & price. See src/Modal/Products file
-
There are some offers for specific product, such as
- Buy 1 get 1 offer for Mango
- Bulk discount offer for Strawberry: buy item >=3, price will be reduced to a specific amount, in this example, 4.5
See src/Modal/Offers file
-
Now, calculate total price when checking out
To run test file through CLI using phpunit, try like this:
./vendor/bin/phpunit tests/CheckoutTest.php