Laravel по-русски

Русское сообщество разработки на PHP-фреймворке Laravel.

Ты не вошёл. Вход тут.

#1 Laravel 6/7/8/9/10 » Проблема с connection() в phpunit » 23.01.2023 18:26:42

cylbps
Ответов: 0

Всех приветствую! Laravel 8.82.0. Столкнулся с такой проблемой: в тесте использую @dataProvider, в котором пытаюсь вернуть коллекцию заполненных моделей. Получаю такую ошибку: The data provider specified for Services\RevenueServiceTest::testGetRevenues is invalid.
Error: Call to a member function connection() on null

<?php

namespace Services;

use App\Models\Account;
use App\Models\Revenue;
use App\Models\User;
use App\Repositories\RevenuesRepository;
use App\Services\RevenueService;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Tests\CreatesApplication;
use Tests\TestCase;

use Mockery;

class RevenueServiceTest extends TestCase
{
    use CreatesApplication;

    private RevenueService $service;

    protected function setUp(): void
    {
        parent::setUp();
        $this->createApplication();
        $this->mockService();
        //$user = $this->mockUser();
        //Auth::shouldReceive('user')->andReturn($user);
    }

    /**
     * @dataProvider getData
     */
    public function testGetRevenues(array $result)
    {
        $revenues = $this->service->getRevenues();

        $this->assertEquals($result, $revenues);
    }

    public function getData()
    {
        return [
            [
                new Collection([
                    0 => (new Revenue())->forceFill([
                        'id' => 43,
                        'user_id' => 1,
                        'operation_id' => 43,
                        'account_id' => 1,
                        'article_id' => 2,
                        'total_sum' => 15000000,
                        'description' => NULL,
                        'created_at' => '2023-01-19T20:48:44.000000Z',
                        'updated_at' => '2023-01-19T20:48:44.000000Z',
                    ])
                        ->setRelation('account', (new Account())->forceFill([
                            'id' => 1,
                            'user_id' => 1,
                            'name' => 'Карта безнал',
                            'balance' => 15000000,
                            'currency_id' => 1,
                            'description' => NULL,
                            'created_at' => NULL,
                            'updated_at' => '2023-01-19T20:48:44.000000Z',
                        ]))
                ])
            ]
        ];
    }

    private function mockService(): void
    {
        $this->service = resolve(RevenueService::class, [
            'revenueRepository' => $this->mockRevenueRepository(),
        ]);
    }

    private function mockUser(): User
    {
        $mock = Mockery::mock(User::class);
        $mock->shouldReceive('getAttribute')
            ->with('id')
            ->andReturn(1);
        return $mock;
    }

    private function mockRevenueRepository(): RevenuesRepository
    {
        $mock = Mockery::mock(RevenuesRepository::class);
        $mock->shouldReceive('getRevenues')
            ->with(1)
            ->andReturn(new Collection([
                0 => (new Revenue())->forceFill([
                    'id' => 43,
                    'user_id' => 1,
                    'operation_id' => 43,
                    'account_id' => 1,
                    'article_id' => 2,
                    'total_sum' => 15000000,
                    'description' => NULL,
                    'created_at' => '2023-01-19T20:48:44.000000Z',
                    'updated_at' => '2023-01-19T20:48:44.000000Z',
                ])
                    ->setRelation('account', (new Account())->forceFill([
                        'id' => 1,
                        'user_id' => 1,
                        'name' => 'Карта безнал',
                        'balance' => 15000000,
                        'currency_id' => 1,
                        'description' => NULL,
                        'created_at' => NULL,
                        'updated_at' => '2023-01-19T20:48:44.000000Z',
                    ]))
            ]));

        return $mock;
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        Mockery::close();
    }
}

Подскажите, из за чего может быть такая ошибка?

Подвал раздела