Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the database value of @DiscriminatorField changes to unsupported value, an Exception should be thrown instead of PHP Notice #563

Closed
vmattila opened this issue Apr 19, 2013 · 3 comments
Labels
Milestone

Comments

@vmattila
Copy link

Referring to the second issue in my SO question (http://stackoverflow.com/questions/15056946/doctrine-odm-returns-proxy-object-for-base-class-instead-of-sub-classed-document): if a database value of the discriminator field changes to some unsupported value, a PHP notice is thrown instead of proper exception.

I tried to find a suitable place to test this and throw an exception so I could have submitted a PR with fix & test case, but unfortunately I do not know that much about mongodb-odm codebase to find a correct place for throwing an exception...

However, I drafted a test case for the issue. Don't know whether HydratorException is the correct exception class and where this test function should be added (Tests/Functional/InheritanceTest.php maybe), but here is it anyway.

/**
 * @expectedException \Doctrine\ODM\MongoDB\Hydrator\HydratorException
 */
public function testUnsupportedDatabaseValueForDiscriminatorMapRaisesException()
{
    $project = new \Documents\Project('Project');
    $this->dm->persist($project);
    $this->dm->flush();

    $id = $project->getId();

    $coll = $this->dm->getDocumentCollection('Documents\Project');
    $document = $coll->findOne(array('name' => 'Project'));
    $this->assertEquals('project', $document['type']);

    // Changing database value for DiscriminatorMap field
    $coll->update(array('name' => 'Project'), array('$set' => array('type' => 'unsupported-type')));

    // Ensuring it's properly persisted
    $document = $coll->findOne(array('name' => 'Project'));
    $this->assertEquals('unsupported-type', $document['type']);

    // This query should raise \Doctrine\ODM\MongoDB\Mapping\HydratorException
    // because 'unsupported-type' is not an allowed value in @DiscriminatorMap
    $document = $this->dm->find('Documents\Project', $id);
    $this->assertInstanceOf('Documents\Project', $document);
}
@malarzm
Copy link
Member

malarzm commented Jan 4, 2016

Cross referencing with #867

@manuelbcd
Copy link

Great @malarzm +1

@alcaeus
Copy link
Member

alcaeus commented Nov 26, 2018

Closing in favour of #867 and #1887.

@alcaeus alcaeus closed this as completed Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants