php - Try catch private method phpunit symfony -
i have following code:
public function addsomething($paramdto) { try { $this->privatemethod($param); } catch(\exception $e) { return ['error' => true, 'messages' => [$e->getmessage()]]; } return ['error' => false, 'messages' => 'success']; } private function privatemethod($param) { if(!$param) { throw new \exception('errormessage'); } }
i'm trying test addsomething method, catch block returns, don't want test private method.
public function testaddsomethingthrowerror($paramdto) { $param = \mockery::mock('myentity'); $method = new \reflectionmethod( 'myservice', 'privatemethod' ); $method->setaccessible(true); $this->expectexception(\exception::class); $this->getmyservice() ->shouldreceive($method->invoke($param) ->withanyargs() ->andthrow(\exception::class); $this->getmyservice()->addsomething($paramdto); }
the thing if run test, coverages private method in if statement , returns exception, catch method in addsomething method not covered, not cover addsomething method @ all.
i using sebastian bergmann phpunit framework.
what doing wrong?
the correct answer should jakub matczak's answer:
"you want "assert if public method returning message indeed returning". there's no sense in doing that. consider tested class blackbox without possibility check source. make tests according how want work using public interface. "
Comments
Post a Comment