logger = $this->getMockBuilder(LoggerInterface::class) ->disableOriginalConstructor() ->getMock(); $this->precioRepository = $this->getMockBuilder(Repository\Venta\Precio::class) ->disableOriginalConstructor() ->getMock(); $this->estadoPrecioRepository = $this->getMockBuilder(Repository\Venta\EstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $this->tipoEstadoPrecioRepository = $this->getMockBuilder(Repository\Venta\TipoEstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); } public function testUpdatePrice(): void { $uniqueElements = 1000; $faker = Faker\Factory::create(); $tipoEstadoPrecio = $this->getMockBuilder(Model\Venta\TipoEstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $tipoEstadoPrecio->id = $faker->unique()->numberBetween(1, $uniqueElements); $tipoEstadoPrecio->descripcion = 'vigente'; $tipoEstadoPrecioRepository = $this->getMockBuilder(Repository\Venta\TipoEstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $tipoEstadoPrecioRepository->method('fetchByDescripcion')->with('vigente')->willReturn($tipoEstadoPrecio); $unidad = $this->getMockBuilder(Model\Venta\Unidad::class) ->disableOriginalConstructor() ->getMock(); $unidad->id = $faker->unique()->numberBetween(1, $uniqueElements); $precio = $this->getMockBuilder(Model\Venta\Precio::class) ->disableOriginalConstructor() ->getMock(); $precio->id = $faker->unique()->numberBetween(1, $uniqueElements); $precio->unidad = $unidad; $estadoPrecio = $this->getMockBuilder(Model\Venta\EstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $estadoPrecio->id = $faker->unique()->numberBetween(1, $uniqueElements); $estadoPrecio->precio = $precio; $estadoPrecio->tipoEstadoPrecio = $tipoEstadoPrecio; $estadoPrecioRepository = $this->getMockBuilder(Repository\Venta\EstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $estadoPrecioRepository->method('create')->willReturn($estadoPrecio); $estadoPrecioRepository->method('save')->willReturnArgument(0); $date = $faker->dateTimeBetween('-6 months'); $estadoPrecioService = new Service\Venta\Precio\Estado($this->logger, $this->precioRepository, $estadoPrecioRepository, $tipoEstadoPrecioRepository); $estadoPrecioService->updatePrice($precio, $date); $this->assertTrue(true); } public function testReplacePrices(): void { $uniqueElements = 1000; $faker = Faker\Factory::create(); $tipoEstadoPrecio1 = $this->getMockBuilder(Model\Venta\TipoEstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $tipoEstadoPrecio1->id = $faker->unique()->numberBetween(1, $uniqueElements); $tipoEstadoPrecio1->descripcion = 'vigente'; $tipoEstadoPrecio = $this->getMockBuilder(Model\Venta\TipoEstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $tipoEstadoPrecio->id = $faker->unique()->numberBetween(1, $uniqueElements); $tipoEstadoPrecio->descripcion = 'reemplazado'; $this->tipoEstadoPrecioRepository->method('fetchByDescripcion') ->willReturnCallback(function($descripcion) use ($tipoEstadoPrecio, $tipoEstadoPrecio1) { return match ($descripcion) { 'vigente' => $tipoEstadoPrecio1, 'reemplazado' => $tipoEstadoPrecio, }; }); $unidad = $this->getMockBuilder(Model\Venta\Unidad::class) ->disableOriginalConstructor() ->getMock(); $unidad->id = $faker->unique()->numberBetween(1, $uniqueElements); $preciosCount = $faker->numberBetween(1, 10); $precios = []; $estados = []; for ($i = 0; $i < $preciosCount; $i++) { $precio = $this->getMockBuilder(Model\Venta\Precio::class) ->disableOriginalConstructor() ->getMock(); $precio->id = $faker->unique()->numberBetween(1, $uniqueElements); $precios []= $precio; $estado = $this->getMockBuilder(Model\Venta\EstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $estado->id = $faker->unique()->numberBetween(1, $uniqueElements); $estado->precio = $precio; $estado->tipoEstadoPrecio = $tipoEstadoPrecio1; $estados []= $estado; } $this->precioRepository->method('fetchByUnidad')->with($unidad->id)->willReturn($precios); $this->estadoPrecioRepository->method('fetchCurrentByPrecio')->willReturnCallback(function($precio_id) use ($estados) { $idx = array_search($precio_id, array_map(fn($estado) => $estado->precio->id, $estados)); return $estados[$idx]; }); $newEstados = []; foreach ($precios as $precio) { $estado = $this->getMockBuilder(Model\Venta\EstadoPrecio::class) ->disableOriginalConstructor() ->getMock(); $estado->id = $faker->unique()->numberBetween(1, $uniqueElements); $estado->precio = $precio; $estado->tipoEstadoPrecio = $tipoEstadoPrecio; $newEstados []= $estado; } $this->estadoPrecioRepository->method('create')->willReturnCallback(function($data) use ($newEstados) { $idx = array_search($data['precio'], array_map(fn($estado) => $estado->precio->id, $newEstados)); return $newEstados[$idx]; }); $this->estadoPrecioRepository->method('save')->willReturnArgument(0); $precio = $this->getMockBuilder(Model\Venta\Precio::class) ->disableOriginalConstructor() ->getMock(); $precio->id = $faker->unique()->numberBetween(1, $uniqueElements); $precio->unidad = $unidad; $date = $faker->dateTimeBetween('-6 months'); $estadoPrecioService = new Service\Venta\Precio\Estado($this->logger, $this->precioRepository, $this->estadoPrecioRepository, $this->tipoEstadoPrecioRepository); $estadoPrecioService->replacePrices($unidad, $date, $precio); $this->assertTrue(true); } }