|
10 | 10 | require_once __DIR__ . '/../../Conversions/HexadecimalToDecimal.php';
|
11 | 11 | require_once __DIR__ . '/../../Conversions/SpeedConversion.php';
|
12 | 12 | require_once __DIR__ . '/../../Conversions/TemperatureConversions.php';
|
| 13 | +require_once __DIR__ . '/../../Conversions/Weightconversions.php'; |
| 14 | +require_once __DIR__ . '/../../Conversions/Lengthconversions.php'; |
13 | 15 |
|
14 | 16 | class ConversionsTest extends TestCase
|
15 | 17 | {
|
@@ -142,4 +144,111 @@ public function testFahrenheitToKelvin()
|
142 | 144 | $this->expectExceptionMessage('Temperature (Fahrenheit) must be a number');
|
143 | 145 | FahrenheitToKelvin("non-numeric");
|
144 | 146 | }
|
| 147 | + /** |
| 148 | + * Helper method to test invalid input handling in conversion methods |
| 149 | + * |
| 150 | + * @param string $method The conversion method to test (e.g. 'WeightConversions::kgToLbs') |
| 151 | + * @param mixed $value The invalid input value |
| 152 | + * @param string $expectedMessage The expected exception message |
| 153 | + */ |
| 154 | + protected function assertInvalidInputConversion($method, $value, $expectedMessage) |
| 155 | + { |
| 156 | + try { |
| 157 | + // Call the method with the invalid input |
| 158 | + $parts = explode('::', $method); |
| 159 | + $className = $parts[0]; |
| 160 | + $methodName = $parts[1]; |
| 161 | + $className::$methodName($value); |
| 162 | + |
| 163 | + // If we get here, no exception was thrown |
| 164 | + $this->fail("Expected exception was not thrown for $method with invalid input"); |
| 165 | + } catch (InvalidArgumentException $e) { |
| 166 | + // Check if the exception message matches the expected message |
| 167 | + $this->assertEquals($expectedMessage, $e->getMessage()); |
| 168 | + } |
| 169 | + } |
| 170 | + public function testKgToLbs() |
| 171 | + { |
| 172 | + $this->assertEquals(220.462, WeightConversions::kgToLbs(100), 0.001); |
| 173 | + $this->assertInvalidInputConversion('WeightConversions::kgToLbs', "invalid string", 'Invalid input for kgToLbs'); |
| 174 | + } |
| 175 | + |
| 176 | + public function testLbsToKg() |
| 177 | + { |
| 178 | + $this->assertEquals(45.3593, WeightConversions::lbsToKg(100), 0.001); |
| 179 | + $this->assertInvalidInputConversion('WeightConversions::lbsToKg', "invalid string", 'Invalid input for lbsToKg'); |
| 180 | + } |
| 181 | + |
| 182 | + public function testGToKg() |
| 183 | + { |
| 184 | + $this->assertEquals(0.5, WeightConversions::gToKg(500), 0.001); |
| 185 | + $this->assertInvalidInputConversion('WeightConversions::gToKg', "invalid string", 'Invalid input for gToKg'); |
| 186 | + } |
| 187 | + |
| 188 | + public function testKgToG() |
| 189 | + { |
| 190 | + $this->assertEquals(1000, WeightConversions::kgToG(1), 0.001); |
| 191 | + $this->assertInvalidInputConversion('WeightConversions::kgToG', "invalid string", 'Invalid input for kgToG'); |
| 192 | + } |
| 193 | + |
| 194 | + public function testOzToLbs() |
| 195 | + { |
| 196 | + $this->assertEquals(3.5, WeightConversions::ozToLbs(56), 0.001); |
| 197 | + $this->assertInvalidInputConversion('WeightConversions::ozToLbs', "invalid string", 'Invalid input for ozToLbs'); |
| 198 | + } |
| 199 | + |
| 200 | + public function testLbsToOz() |
| 201 | + { |
| 202 | + $this->assertEquals(64, WeightConversions::lbsToOz(4), 0.001); |
| 203 | + $this->assertInvalidInputConversion('WeightConversions::lbsToOz', "invalid string", 'Invalid input for lbsToOz'); |
| 204 | + } |
| 205 | + public function testMToKm() |
| 206 | + { |
| 207 | + $this->assertEquals(1, LengthConversions::mToKm(1000), 0.001); |
| 208 | + $this->assertInvalidInputConversion('LengthConversions::mToKm', "invalid string", 'Invalid input for mToKm'); |
| 209 | + } |
| 210 | + |
| 211 | + public function testKmToM() |
| 212 | + { |
| 213 | + $this->assertEquals(5000, LengthConversions::kmToM(5), 0.001); |
| 214 | + $this->assertInvalidInputConversion('LengthConversions::kmToM', "invalid string", 'Invalid input for kmToM'); |
| 215 | + } |
| 216 | + |
| 217 | + public function testMToMiles() |
| 218 | + { |
| 219 | + $this->assertEquals(0.621373, LengthConversions::mToMiles(1000), 0.001); |
| 220 | + $this->assertInvalidInputConversion('LengthConversions::mToMiles', "invalid string", 'Invalid input for mToMiles'); |
| 221 | + } |
| 222 | + |
| 223 | + public function testMilesToM() |
| 224 | + { |
| 225 | + $this->assertEquals(1609.34, LengthConversions::milesToM(1), 0.001); |
| 226 | + $this->assertInvalidInputConversion('LengthConversions::milesToM', "invalid string", 'Invalid input for milesToM'); |
| 227 | + } |
| 228 | + |
| 229 | + public function testInToCm() |
| 230 | + { |
| 231 | + $this->assertEquals(25.4, LengthConversions::inToCm(10), 0.001); |
| 232 | + $this->assertInvalidInputConversion('LengthConversions::inToCm', "invalid string", 'Invalid input for inToCm'); |
| 233 | + } |
| 234 | + |
| 235 | + public function testCmToIn() |
| 236 | + { |
| 237 | + $this->assertEquals(39.37, LengthConversions::cmToIn(100), 0.001); |
| 238 | + $this->assertInvalidInputConversion('LengthConversions::cmToIn', "invalid string", 'Invalid input for cmToIn'); |
| 239 | + } |
| 240 | + |
| 241 | + public function testKmToMiles() |
| 242 | + { |
| 243 | + $this->assertEquals(6.21504, LengthConversions::kmToMiles(10), 0.001); |
| 244 | + $this->assertInvalidInputConversion('LengthConversions::kmToMiles', "invalid string", 'Invalid input for kmToMiles'); |
| 245 | + } |
| 246 | + |
| 247 | + public function testMilesToKm() |
| 248 | + { |
| 249 | + $this->assertEquals(16.09, LengthConversions::milesToKm(10), 0.001); |
| 250 | + $this->assertInvalidInputConversion('LengthConversions::milesToKm', "invalid string", 'Invalid input for milesToKm'); |
| 251 | + } |
| 252 | + |
| 253 | + |
145 | 254 | }
|
0 commit comments