connection = $this->getMockBuilder(Define\Connection::class) ->disableOriginalConstructor() ->getMock(); $this->comunaRepository = $this->getMockBuilder(Repository\Comuna::class) ->disableOriginalConstructor() ->getMock(); $this->comunaRepository->method('fetchById')->willReturnCallback(function ($id) { $comuna = new Model\Comuna(); $comuna->id = $id; return $comuna; }); } public function testCreate(): void { $direccionRepository = new Repository\Direccion($this->connection, $this->comunaRepository); $faker = Faker\Factory::create(); $data = [ 'calle' => $faker->streetName, 'numero' => $faker->randomNumber(), 'extra' => $faker->streetSuffix, 'comuna' => $faker->randomNumber(), ]; $direccion = $direccionRepository->create($data); $this->assertTrue(!isset($direccion->id)); $this->assertEquals($data['calle'], $direccion->calle); $this->assertEquals($data['numero'], $direccion->numero); $this->assertEquals($data['extra'], $direccion->extra); $this->assertEquals($data['comuna'], $direccion->comuna->id); } public function testSave(): void { $direccionRepository = new Repository\Direccion($this->connection, $this->comunaRepository); $faker = Faker\Factory::create(); $direccion = new Model\Direccion(); $direccion->calle = $faker->streetName; $direccion->numero = $faker->randomNumber(3); $direccion->extra = $faker->streetSuffix; $direccion->comuna = $this->getMockBuilder(Model\Comuna::class)->getMock(); $direccion->comuna->id = $faker->numberBetween(10000, 18000); $direccion = $direccionRepository->save($direccion); $this->assertTrue(isset($direccion->id)); } }