/** * Test AddClass::add() with data that should throw an exception. * * This method is similar to testAddWithDataProvider(), but the data * provider gives us data that should throw an exception. * * This test uses the '@expectedException' annotation to tell PHPUnit that * a thrown exception should pass the test. You specify a * fully-qualified exception class name. If you specify \Exception, PHPUnit * will pass any exception, whereas a more specific subclass of \Exception * will require that exception type to be thrown. * * Alternately, you can use try and catch blocks with assertions in order * to test exceptions. We won't demonstrate that here; it's a much better * idea to test your exceptions with @expectedException. * * @dataProvider addBadDataProvider * @expectedException \InvalidArgumentException * * @see AddClassTest::addBadDataProvider() */ public function testAddWithBadDataProvider($a, $b) { $sut = new AddClass(); $sut->add($a, $b); }
/** * A simple addition method with validity checking. * * @param numeric $a * A number to add. * @param numeric $b * Another number to add. * * @return numeric * The sum of $a and $b. * * @throws \InvalidArgumentException * If either $a or $b is non-numeric, we can't add, so we throw. */ private function privateAdd($a, $b) { $adder = new AddClass(); return $adder->add($a, $b); }