/** * @throws \PhpJsonMarshaller\Exception\ClassNotFoundException */ public function testClassSuccessfulDecode() { $result = $this->decoder->decodeClass('\\PhpJsonMarshallerTests\\ExampleClass\\ClassCannotIgnoreUnknown'); $this->assertEquals(3, count($result->getProperties())); $this->assertEquals(false, $result->canIgnoreUnknown()); $this->assertEquals(true, $result->hasProperty('id')); $this->assertEquals(true, $result->getProperty('id')->hasAnnotationName()); $this->assertEquals('id', $result->getProperty('id')->getAnnotationName()); $this->assertEquals(true, $result->getProperty('id')->hasAnnotationType()); $this->assertEquals('int', $result->getProperty('id')->getAnnotationType()); $this->assertEquals('id', $result->getProperty('id')->getDirect()); $this->assertEquals(true, $result->getProperty('id')->hasDirect()); $this->assertEquals(false, $result->getProperty('id')->hasGetter()); $this->assertEquals(false, $result->getProperty('id')->hasSetter()); $this->assertEquals(true, $result->hasProperty('active')); $this->assertEquals(true, $result->getProperty('active')->hasAnnotationName()); $this->assertEquals('active', $result->getProperty('active')->getAnnotationName()); $this->assertEquals(true, $result->getProperty('active')->hasAnnotationType()); $this->assertEquals('boolean', $result->getProperty('active')->getAnnotationType()); $this->assertEquals(false, $result->getProperty('active')->hasDirect()); $this->assertEquals(true, $result->getProperty('active')->hasGetter()); $this->assertEquals(true, $result->getProperty('active')->hasSetter()); $this->assertEquals('setActive', $result->getProperty('active')->getSetter()); $this->assertEquals('isActive', $result->getProperty('active')->getGetter()); $this->assertEquals(true, $result->hasProperty('name')); $this->assertEquals(true, $result->getProperty('name')->hasAnnotationName()); $this->assertEquals('name', $result->getProperty('name')->getAnnotationName()); $this->assertEquals(true, $result->getProperty('name')->hasAnnotationType()); $this->assertEquals('string', $result->getProperty('name')->getAnnotationType()); $this->assertEquals(false, $result->getProperty('name')->hasDirect()); $this->assertEquals(true, $result->getProperty('name')->hasGetter()); $this->assertEquals(true, $result->getProperty('name')->hasSetter()); $this->assertEquals('getName', $result->getProperty('name')->getGetter()); $this->assertEquals('setName', $result->getProperty('name')->getSetter()); }
/** * Sets the values of an associative array into a <$classString> class * @param array $assocArray the associative array containing all of our data * @param string $classString the fully qualified namespaced class which will receive the data * @return mixed A fully populated <$classString> class, containing values from the assoc array * @throws InvalidTypeException * @throws UnknownPropertyException * @throws \PhpJsonMarshaller\Exception\ClassNotFoundException */ protected function unmarshallClass($assocArray, $classString) { // Decode the class and it's properties $decodedClass = $this->classDecoder->decodeClass($classString); if (count($decodedClass->getProperties()) == 0 && count($decodedClass->getConstructorParams()) === 0) { throw new \InvalidArgumentException("Class {$classString} doesn't have any @MarshallProperty annotations defined"); } // Create a new class $newClass = $this->createClass($classString, $decodedClass, $assocArray); foreach ($assocArray as $key => $value) { if ($decodedClass->hasProperty($key)) { $property = $decodedClass->getProperty($key); $propertyType = $property->getPropertyType(); // Decode the result $result = $this->decodeValue($value, $propertyType); // Set our result into the class if ($property->hasDirect()) { $newClass->{$property->getDirect()} = $result; } elseif ($property->hasSetter()) { $newClass->{$property->getSetter()}($result); } } else { if ($decodedClass->canIgnoreUnknown() === false) { throw new UnknownPropertyException("Unknown property '{$key}' in class '{$classString}' and cannot ignore unknown properties.\n (You can add a MarshallConfig annotation on the class to change this)"); } } } return $newClass; }
/** * @expectedException \PhpJsonMarshaller\Exception\CacheCollisionException */ public function testExceptionOnCacheCollision() { $classObject = $this->decoder->decodeClass('\\PhpJsonMarshallerTests\\ExampleClass\\User'); $this->cache->setClass('\\PhpJsonMarshallerTests\\ExampleClass\\User', $classObject); $this->cache->setClass('\\PhpJsonMarshallerTests\\ExampleClass\\User', $classObject); }