lundi 24 août 2015

how to have class return mock on new Class()?

In phpunit, I want to test a controller. As part of this I need to return a mock when new Class() is called.

I have tried:

$mockEntity = $this->getMock( 'Vet', array( '__construct', 'setDoctrine', 'setContainer' ) );

$mockForm = $this->getMock( 'VetType', array( 'handleRequest', 'isValid', 'createView' ) );

$mockEntity->expects( $this->once() )->method( '__construct')->willReturn( $this->returnValue( $mockEntity ) );

and also simply:

$mockEntity = $this->getMock( 'Vet', array( 'setDoctrine', 'setContainer' ) );

$mockForm = $this->getMock( 'VetType', array( 'handleRequest', 'isValid', 'createView' ) );

Both return a "Vet" object, not the mock. Is this possible with phpUnit 4.0.20, or do I just need to add a getNewVet() function?

This is a Symfony2 controller

Aucun commentaire:

Enregistrer un commentaire