/** * @covers ::apply * @covers ::to */ public function testFailedAssertProvidesRejectedValue() { // ---------------------------------------------------------------- // setup your test // // we care about: // - fieldOrVar // - dataType $value = 5; $reason = "must be less than 4"; $e = null; $expectedMessage = __CLASS__ . '->' . __FUNCTION__ . '()@' . (__LINE__ + 14) . ': contract failed; ' . $reason; $expectedData = ['thrownByName' => __CLASS__ . '->' . __FUNCTION__ . '()@' . (__LINE__ + 12), 'thrownBy' => new CodeCaller(__CLASS__, __FUNCTION__, '->', __FILE__, __LINE__ + 11), 'fieldOrVar' => $value, 'fieldOrVarName' => 'value', 'reason' => $reason, 'dataType' => 'integer<' . $value . '>']; // ---------------------------------------------------------------- // perform the change try { AssertValue::apply($value < 4, $reason)->to($value); } catch (ContractFailed $e) { // do nothing } // ---------------------------------------------------------------- // test the results // make sure we caught an exception $this->assertInstanceOf(ContractFailed::class, $e); // make sure it has the details we expect $actualMessage = $e->getMessage(); $actualData = $e->getMessageData(); $this->assertEquals($expectedMessage, $actualMessage); $this->assertEquals($expectedData, $actualData); }
/** * check that an expression is true ... and if it is not, throw an * exception * * @param mixed $value * the value check we are checking * @param boolean $expr * the expression we use to check $value * @param string|null $reason * the reason why $expr must be true * @return boolean * TRUE on success */ public static function assertValue($value, $expr, $reason = null) { AssertValue::apply($expr, $reason)->to($value); }