Permalink
Browse files

Merge pull request #150 from jaugustin/fix-param-converter-optional

fix param converter to handle correctly isOptional parameter
  • Loading branch information...
2 parents 2e7bb13 + 6237a2a commit c02e7ceb6c475beb8babefbe52fefde5fb698c39 @willdurand willdurand committed May 7, 2012
@@ -30,7 +30,12 @@ public function apply(Request $request, ConfigurationInterface $configuration)
if (in_array('id', $exclude) || false === $object = $this->findPk($classQuery, $request)) {
// find by criteria
if (false === $object = $this->findOneBy($classQuery, $request, $exclude)) {
- throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
+ if ($configuration->isOptional()) {
+ //we find nothing but the object is optional
+ $object = null;
+ } else {
+ throw new \LogicException('Unable to guess how to get a Propel object from the request information.');
+ }
}
}
@@ -128,4 +128,17 @@ public function testParamConverterFindLogicError()
$configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book'));
$paramConverter->apply($request, $configuration);
}
+
+
+ public function testParamConverterFindWithOptionalParam()
+ {
+ $paramConverter = new PropelParamConverter();
+ $request = new Request(array(), array(), array('book' => null));
+ $configuration = new ParamConverter(array('class' => 'Propel\PropelBundle\Tests\Fixtures\Model\Book', 'name' => 'book'));
+ $configuration->setIsOptional(true);
+ $paramConverter->apply($request, $configuration);
+
+ $this->assertNull($request->attributes->get('book'),
+ 'param "book" should be null if book is not found and the parameter is optional');
+ }
}

0 comments on commit c02e7ce

Please sign in to comment.