channel = new Channel(999); $container = $this->getContainer(); $container->shouldReceive('get')->with(Db::class)->andReturn(new Db($container)); $connectionResolverInterface = $container->get(ConnectionResolverInterface::class); Register::setConnectionResolver($connectionResolverInterface); Schema::create('base_models', function (Blueprint $table) { $table->increments('id'); }); Schema::create('related1s', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('base_model_id'); $table->integer('number'); }); Schema::create('related2s', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('base_model_id'); $table->integer('number'); }); BaseModel::create(); Related1::create(['base_model_id' => 1, 'number' => 10]); Related1::create(['base_model_id' => 1, 'number' => 11]); Related2::create(['base_model_id' => 1, 'number' => 12]); Related2::create(['base_model_id' => 1, 'number' => 13]); } protected function tearDown(): void { Mockery::close(); Schema::dropIfExists('base_models'); Schema::dropIfExists('related1s'); Schema::dropIfExists('related2s'); } public function testLoadMaxSingleRelation() { $model = BaseModel::first(); $model->loadMax('related1', 'number'); $this->assertEquals(11, $model->related1_max_number); } public function testLoadMaxMultipleRelations() { $model = BaseModel::first(); $model->loadMax(['related1', 'related2'], 'number'); $this->assertEquals(11, $model->related1_max_number); $this->assertEquals(13, $model->related2_max_number); } protected function getContainer() { $dispatcher = Mockery::mock(EventDispatcherInterface::class); $dispatcher->shouldReceive('dispatch')->with(Mockery::any())->andReturnUsing(function ($event) { $this->channel->push($event); }); $container = ContainerStub::getContainer(function ($conn) use ($dispatcher) { $conn->setEventDispatcher($dispatcher); }); $container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturn($dispatcher); return $container; } }