From 55acf626c2e62144984e83b42db49b298a9e2938 Mon Sep 17 00:00:00 2001 From: Fbenas Date: Mon, 7 May 2018 16:47:13 +0100 Subject: Add unit tests and amend unit test part in readme --- tests/unit/VendorTest.php | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 tests/unit/VendorTest.php (limited to 'tests/unit') 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 @@ + + */ +class VendorTest extends TestCase +{ + /** + * Test the good constructing of vendor + * + * @dataProvider getGoodConstructorData + * @author Phil Burton + * @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 + * @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 + */ + 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 + * @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], + ]; + } +} -- cgit v1.2.3