/** * what PHP type is $item? * * @param mixed $item * the data to examine * @param int $flags * options to change what we put in the return value * @return string * the data type of $item */ function get_printable_type($item, $flags = GetPrintableType::FLAG_DEFAULTS) { return GetPrintableType::of($item, $flags); }
/** * create the format string and exception data * * @param string $builder * name of the ParameterBuilder class to use * @param string $formatString * the format string to customise * @param array $backtrace * a debug_backtrace() * @param mixed $fieldOrVar * the value that you're throwing an exception about * @param string $fieldOrVarName * the name of the value in your code * @param array $extraData * extra data that you want to include in your exception * @param int|null $typeFlags * do we want any extra type information in the final exception message? * @param array $callStackFilter * are there any namespaces we want to filter out of the call stack? * @return ParameterisedException * an fully-built exception for you to throw */ protected static function buildFormatAndData($builder, $formatString, $backtrace, $fieldOrVar, $fieldOrVarName, array $extraData = [], $typeFlags = null, array $callStackFilter = []) { // build the basic message and data list($message, $data) = $builder::from($formatString, $backtrace, $callStackFilter); // merge in any extra data we've been given $data = array_merge_keys($data, $extraData); // merge in the remainder of our parameters $data['dataType'] = GetPrintableType::of($fieldOrVar, $typeFlags); $data['fieldOrVarName'] = $fieldOrVarName; $data['fieldOrVar'] = $fieldOrVar; // all done return [$message, $data]; }
/** * @covers ::of * @dataProvider provideBadFlags */ public function testWillUseDefaultFlagsIfInvalidFlagsProvided($flags) { // ---------------------------------------------------------------- // setup your test $data = 100; $expectedType = "integer<100>"; // ---------------------------------------------------------------- // perform the change $actualType = GetPrintableType::of($data, $flags); // ---------------------------------------------------------------- // test the results $this->assertEquals($expectedType, $actualType); }
/** * throw an exception if the object is not in read-write mode * * @return void * @throws ReadOnlyException */ protected function requireReadWrite() { if (!$this->readOnly) { return; } // if we get here, we are in read-only mode, and cannot be edited $type = GetPrintableType::of($this); throw new ReadOnlyException("{$type} is read-only; cannot edit"); }
/** * @covers ::offsetUnset * @covers ::requireReadWrite */ public function testCannotForgetFactoryWhenReadOnly() { // ---------------------------------------------------------------- // setup your test $factories = ['dummy' => new FactoryListContainerTest_Builder()]; $unit = new FactoryListContainer($factories); $expectedType = GetPrintableType::of($unit); $expectedMessage = __CLASS__ . '->' . __FUNCTION__ . '()@' . (__LINE__ + 15) . ": " . FactoryListContainer::class . "->offsetUnset()@209 says attempt to edit read-only container '{$expectedType}'"; $expectedData = ['thrownBy' => new CodeCaller(FactoryListContainer::class, 'offsetUnset', '->', FactoryListContainer::file, 209), 'thrownByName' => FactoryListContainer::class . '->offsetUnset()@209', 'calledBy' => new CodeCaller(__CLASS__, __FUNCTION__, '->', __FILE__, __LINE__ + 11), 'calledByName' => __CLASS__ . '->' . __FUNCTION__ . '()@' . (__LINE__ + 10), 'fieldOrVarName' => '$this', 'fieldOrVar' => $unit, 'dataType' => $expectedType]; // ---------------------------------------------------------------- // perform the change try { unset($unit['trout']); } catch (ContainerIsReadOnly $e) { // fall through } // ---------------------------------------------------------------- // test the results // make sure we have an exception $this->assertInstanceOf(ContainerIsReadOnly::class, $e); $actualMessage = $e->getMessage(); $actualData = $e->getMessageData(); $this->assertEquals($expectedMessage, $actualMessage); $this->assertEquals($expectedData, $actualData); }