diff options
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | src/Model/Vendor.php | 3 | ||||
-rw-r--r-- | tests/unit/VendorTest.php | 110 |
3 files changed, 118 insertions, 2 deletions
@@ -24,6 +24,12 @@ Example: ``` php scripts/run.php -fdata/input.txt -c150 -lEC123 -t12:00 -d09/05/18 ``` + +To run unit tests: +``` +php ./vendor/bin/phpunit tests +``` + # Design Decisions ## Coding style and standards @@ -63,3 +69,4 @@ The filtering is tightly coupled with both the `App\Model\Collection` and the `A # Testing I have not attempted to get full coverage of the code, and I have not attempted to cover all the possible use cases for input. I have added these unit tests, using PHPUnit to show that I am capable of writing testable code and writing unit tests to cover that code. +Because of time constraints I have left this part a little light. Usually I would prefer to make a base test class, or many base test classes for similar objects and have many more tests for each component. I happy to discuss more about how I would enhance these unit tests and the main project code in-order to get better test coverage. diff --git a/src/Model/Vendor.php b/src/Model/Vendor.php index dfed157..927c52c 100644 --- a/src/Model/Vendor.php +++ b/src/Model/Vendor.php @@ -60,7 +60,6 @@ class Vendor extends Model return static::parseVendors($fileContents); } - /** * Parse file and return array * @@ -112,7 +111,7 @@ class Vendor extends Model * @param $postcode * @param $maxCovers */ - public function __construct($name, $postcode, $maxCovers) + public function __construct(string $name, string $postcode, int $maxCovers) { $this->name = $name; $this->postcode = $postcode; diff --git a/tests/unit/VendorTest.php b/tests/unit/VendorTest.php new file mode 100644 index 0000000..de58110 --- /dev/null +++ b/tests/unit/VendorTest.php @@ -0,0 +1,110 @@ +<?php + +namespace App\Unit; + +use PHPUnit\Framework\TestCase; +use ReflectionClass; +use App\Model\Vendor; + +/** + * Tet the vedor class + * + * @author Phil Burton <phil@pgburton.com> + */ +class VendorTest extends TestCase +{ + /** + * Test the good constructing of vendor + * + * @dataProvider getGoodConstructorData + * @author Phil Burton <phil@pgburton.com> + * @param string $name + * @param string $postcode + * @param int maxCovers + */ + public function testGoodConstructor($name, $postcode, $maxCovers) + { + // create new vendor + $vendor = new Vendor($name, $postcode, $maxCovers); + + // use reflection to get values of protected properties + $reflectionClass = new ReflectionClass(Vendor::class); + + // Check name has been set correctly + $nameProperty = $reflectionClass->getProperty('name'); + $nameProperty->setAccessible(true); + $nameValue = $nameProperty->getValue($vendor); + + $this->assertEquals($nameValue, $name); + + // Check postcode has been set correctly + $postcodeProperty = $reflectionClass->getProperty('postcode'); + $postcodeProperty->setAccessible(true); + $postcodeValue = $postcodeProperty->getValue($vendor); + + $this->assertEquals($postcodeValue, $postcode); + + // Check maxCovers has been set correctly + $maxCoversProperty = $reflectionClass->getProperty('maxCovers'); + $maxCoversProperty->setAccessible(true); + $maxCoversValue = $maxCoversProperty->getValue($vendor); + + $this->assertEquals($maxCoversValue, $maxCovers); + + // Check the menus array is initalised to an empty array + $mensuProperty = $reflectionClass->getProperty('menus'); + $mensuProperty->setAccessible(true); + $menusValue = $mensuProperty->getValue($vendor); + $this->assertEquals($menusValue, []); + } + + /** + * Data for testing good constcutor + * + * @author Phil Burton <phil@pgburton.com> + * @return mixed[] + */ + public function getGoodConstructorData() + { + return [ + ['a', 'b', 1], + ['1', '2', 3], + ['A very long name with spaces in it', 'SW111TH', 10000000], + ]; + } + + /** + * Test the check location + * + * @dataProvider getLocationData + * @author Phil Burton <phil@pgburton.com> + */ + public function testCheckLocation($postcode, $postcodePart, $expectedResult) + { + $vendor = new Vendor('name', $postcode, 1); + + $this->assertEquals($vendor->checkLocation($postcodePart), $expectedResult); + } + + /** + * Return the location data for test + * + * @author Phil Burton <phil@pgburton.com> + * @return mixed[] + */ + public function getLocationData() + { + return [ + ['SW111TH', 'SW', true], + ['SW11 1TH', 'SW', true], + ['sw111th', 'SW', true], + ['sW111TH', 'SW', true], + ['SW11 1TH', 'sw', true], + ['sw11 1th', 'sw', true], + ['SW', 'SW', true], + ['SE111TH', 'SW', false], + ['SW111TH', 'se', false], + ['0000SW', 'SW', false], + ]; + } +} |