Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Antonella's avatar

MOCK API test:Error: Call to undefined method App\Services\TrelloCardService::_downloadCardsFromBoard()

I wrote the following service:

namespace App\Services;

use App\Services\Api\TrelloCardAPIService;

class TrelloCardService
{
    protected $trelloCardApiService;

    public function __construct(TrelloCardAPIService $trelloCardApiService)
    {
        $this->trelloCardApiService = $trelloCardApiService;
    }
    
}

then a service that calls the API:

<?php


namespace App\Services\Api;

use App\Traits\CardTrait;
use Unirest\Request;

class TrelloCardAPIService
{
    public function call(string $url) {
        $headers = array('Accept' => 'application/json');
        $query = array('key' => env('TRELLO_KEY'), 'token' => env('TRELLO_TOKEN'));
        $r = Request::get($url, $headers, $query);
        return $r->body;
    }

    public function _downloadCardsFromBoard() {
        echo "API downloadCards!\n";
        $url = TRELLO_API_BASE_URL . "/boards/".TRELLO_BOARDS_SPRINT."/cards";
        $res = $this->call($url);
        return $res;
    }

}

then I wrote the test feature:

namespace Tests\Feature;

use App\Services\Api\TrelloCardAPIService;
use App\Services\TrelloCardService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\File;

use Tests\TestCase;

class trelloCardTest extends TestCase
{
    public function test_mock_card()
    {
        $cards = json_decode(File::get("tests/test_data/cards.json"),FALSE);

        $mock = $this->mock(TrelloCardAPIService::class, function ($mock) use ($cards) {
            $mock->shouldReceive('_downloadCardsFromBoard')
                ->once()
                ->andReturn($cards);
        });
        
        //here I print the var mock if I do the DD
        $mockedTrelloCardService = new TrelloCardService($mock);

        $data = $mockedTrelloCardService->_downloadCardsFromBoard();//fail this
        dd($data);//I would like to print $cards
    }
}

give me following error:

Error: Call to undefined method App\Services\TrelloCardService::_downloadCardsFromBoard()
0 likes
1 reply
automica's avatar
automica
Best Answer
Level 54

@gianmarx isnt '_downloadCardsFromBoard' a method within TrelloCardAPIService not TrelloCardService

so would be:

$data = $mockedTrelloCardService->trelloCardApiService->_downloadCardsFromBoard();
1 like

Please or to participate in this conversation.