3
votes

I'm trying to test my Category class. I'm using Mockery::mock() method, with 'overload:' prefix and makePartial() method.

When running test I have this error:

Mockery\Exception\BadMethodCallException : Method App\Models\Category::getDynamicFieldsForDocument() does not exist on this mock object

Here is my code:

namespace App\Models;
class Category extends DictionaryBase{
    //some methods
    public function getDynamicFieldsForDocument()
    {
        $data = [];
        $parents = [];
        $allParents = $this->getParents($this->id, $parents);
        foreach ($allParents as $parentId) {

            $parent = Category::find($parentId);
            $fields = $parent->dynamicFields();
            foreach ($fields as $field) {
                $data[$field['name']] = $field;
            }
        }

        return $data;
    }
}

TestCase:

namespace Tests\Unit;

use App\Models\Category;
use Tests\TestCase;
class CategoryModelTest extends TestCase{
    //some methods
    /**
     * @runInSeparateProcess
     * @preserveGlobalState disabled
     */
    public function testGetDynamicFieldsForDocument()
    {
        $mockCategory = \Mockery::mock('overload:'.Category::class)->makePartial();
        $preparedDynamicFields = $this->prepareDynamicFields();
        $preparedCategories = $this->prepareCategories();
        $mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]);
        $mockCategory->shouldReceive('getParents')->andReturn(['1a2b', '3c4d', '5e6f']);
        $mockCategory->shouldReceive('dynamicFields')->andReturn(null, $preparedDynamicFields[0], $preparedDynamicFields[1]);

        $response = $mockCategory->getDynamicFieldsForDocument();
        dd($response);
    }
}

I have no idea why i still have error. I think when ->makePartial() method is called it should mock only methods, which are called by ->shouldReceive()


EDIT:

Now I'm making mock instance without :overload, and mocking 'find' method in this way:

    `$mockCategory->shouldReceive('find')->andReturn($preparedCategories[0], $preparedCategories[1], $preparedCategories[2]);`

My find method looks like this:

public static function find($id) {
        return $id ? self::list(config(static::IDENT.'.fields'), (new Filter('and'))->add('id', $id, '')->toArray(),[],1,1)[0] ?? null : null;
    }

And this is my error:

Error : Wrong parameters for App\Exceptions\ApiException([string $message [, long $code [, Throwable $previous = NULL]]])

It's because list method call API so it looks like this method is called without mock. I know that i can't mock static method, but earlier when I used :overload it was possible. What's now?

1
Can you post the namespaces of your Category model and the CategoryModelTest class ? Also check the use declarations on your test file.namelivia
I updated my post, you can see it nowMarshall

1 Answers

1
votes

Delete :overload and just define your mock as:

$mockCategory = \Mockery::mock(Category::class)->makePartial()

Example

Model:

namespace App\Models;

class Foobar extends BaseModel
{
  public function foonction()
  {   
      Foobar::find();                                                                                                                                                                                          
      return '1';
  }   
}

Test:

namespace Tests;

use Evalua\Heva\Models\Foobar;

class FoobarTest extends TestCase 
{
  public function testFoobar()
  {   
    $fooMock = \Mockery::mock('overload:'.Foobar::class)->makePartial();
    $fooMock->shouldReceive('find')->once();
    $fooMock->foonction();
  }   
}

Fails with:

Mockery\Exception\BadMethodCallException: Method Evalua\Heva\Models\Foobar::foonction() does not exist on this mock object

Without the :overload the test pass.

The explanation should be based on what's written in the documentation about overload:

Prefixing the valid name of a class (which is NOT currently loaded) with “overload:” will generate an alias mock (as with “alias:”) except that created new instances of that class will import any expectations set on the origin mock ($mock). The origin mock is never verified since it’s used an expectation store for new instances. For this purpose we use the term “instance mock”