I wrote an Shell Task performing the creation of some XML Document.
Now I’d like to Unit Test this Task.
My Question is how. And my Problem is, that the MockBuilder is not really creating a Mock.
Here’s the Code of my setUp() Function.
public function setUp()
{
parent::setUp();
$this->io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
$this->PrepareCatalog = $this->getMockBuilder('Mercateo\Shell\Task\PrepareCatalogTask')
->setConstructorArgs([$this->io])
->getMock();
}
The “PrepareCatalogTask” is (of Course) invoked by a Shell.
I’ve written a simple math Function in the PrepareCatalogTask:
public function mathAdd($a,$b)
{
$c = $a + $b;
return $c;
}
Here is the Error Message i’m getting:
2) Mercateo\Test\TestCase\Shell\Task\PrepareCatalogTaskTest::testMathAdd
Failed asserting that null matches expected 9.
So, the PrepareCatalogTask Object has not been instantiated respectively the Mock Builder is not doing what I hope he’s doing.
Because you’re not calling setMethods() on the MockBuilder for your shell task, all of the classes methods are mocked. Instead either don’t use a mock, or only mock _stop with
Not sure what DataTrait. In general you can use PHPUnit to generate classes for a trait and then test the trait methods with that object. If your trait requires methods to be implemented by the class that uses the trait I would create a simple class that uses your trait and then test that.