# How to UnitTest a ShellTask

**URL:** <https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738>\
**Category:** Need Help\
**Created:** [December 15, 2016, 2:46pm UTC](https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738 "2016-12-15T14:46:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HeHof](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@HeHof](https://discourse.cakephp.org/u/HeHof)\
**Post date:** [December 15, 2016, 2:46pm UTC](https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738/1 "2016-12-15T14:46:50Z")

</div>

Hello!

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.

Any suggestions?  
Thx!!

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [January 21, 2017, 5:17am UTC](https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738/3 "2017-01-21T05:17:14Z")

</div>

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

```
    $this->PrepareCatalog = $this->getMockBuilder('Mercateo\Shell\Task\PrepareCatalogTask')
        ->setConstructorArgs([$this->io])
        ->setMethods(['_stop'])
        ->getMock();
```

---

<div class="post-metadata">

**Author:** ![bhumi](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@bhumi](https://discourse.cakephp.org/u/bhumi)\
**Post date:** [July 6, 2021, 3:27pm UTC](https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738/4 "2021-07-06T15:27:22Z")

</div>

How to do unit test of datatrait? Or how we can use datatrait method in unit test file CAKEPHP3

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [July 6, 2021, 10:16pm UTC](https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738/5 "2021-07-06T22:16:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bhumi](https://avatars.discourse-cdn.com/v4/letter/b/edb3f5/32.png) [@bhumi](https://discourse.cakephp.org/u/bhumi)\
**Post date:** [July 7, 2021, 6:24am UTC](https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738/6 "2021-07-07T06:24:13Z")

</div>

In the Traits folder, we are creating trait files, So I want to use One of the Trait methods in my UNIT test file, but I was not able to use it.

```auto
$obj = $this->getObjectForTrait(DataTrait::class);
$mock = $this->getMockForTrait('/src/Controller/Traits/DataTrait.php');

```

I have tried like above but it’s loading Test trait, not the actual trait that I want to use.

---

<div class="post-metadata">

**Author:** ![markstory](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/markstory/32/182_2.png) [@markstory](https://discourse.cakephp.org/u/markstory)\
**Post date:** [July 8, 2021, 4:45am UTC](https://discourse.cakephp.org/t/how-to-unittest-a-shelltask/1738/7 "2021-07-08T04:45:18Z")

</div>

What do you mean that a ‘test trait’ is being loaded? I’ve not seen that behavior before in PHPUnit.
