public function testExceptionWhenAttemptToAddEntryWithInvalidValueType() { $msg = 'Invalid value type. Allowed types of data: strings'; $this->setExpectedException('Wookieb\\Map\\Exception\\InvalidValueTypeException', $msg); try { $this->object->add('tip', 1); } catch (InvalidValueTypeException $e) { $this->assertSame(1, $e->getValue()); throw $e; } }
/** * {@inheritDoc} */ public function create($data) { if (!is_array($data) && !$data instanceof \Traversable) { throw new InvalidValueException('Invalid data to create a map. Only traversable structures allowed'); } $map = new StrictMap($this->keyType->getTypeCheck(), $this->valueType->getTypeCheck()); foreach ($data as $entry) { if (is_object($entry)) { $entry = (array) $entry; } else { if (!is_array($entry)) { throw new InvalidValueException('Invalid data to create a map. Each entry must be an array or object'); } } if (!array_key_exists('key', $entry)) { throw new InvalidValueException('Invalid data to create a map. One of entry does not contain "key" key'); } if (!array_key_exists('value', $entry)) { throw new InvalidValueException('Invalid data to create a map. One of entry does not contain "value" key'); } try { $key = $this->keyType->create($entry['key']); } catch (\Exception $e) { throw new InvalidValueException('One of entry of map contains invalid "key"', 0, $e); } try { $value = $this->valueType->create($entry['value']); } catch (\Exception $e) { throw new InvalidValueException('One of entry of map contains invalid "value"', 0, $e); } $map->add($key, $value); } return $map; }