createStub(Query::class); $query->method('getOneOrNullResult')->willReturn($lastOrder); $qb = $this->createStub(QueryBuilder::class); $qb->method('where')->willReturnSelf(); $qb->method('setParameter')->willReturnSelf(); $qb->method('orderBy')->willReturnSelf(); $qb->method('setMaxResults')->willReturnSelf(); $qb->method('getQuery')->willReturn($query); return $qb; } public function testGenerateFirstOfMonth(): void { $repo = $this->createStub(OrderNumberRepository::class); $repo->method('createQueryBuilder')->willReturn($this->createQueryBuilder(null)); $em = $this->createStub(EntityManagerInterface::class); $service = new OrderNumberService($repo, $em); $result = $service->generate(); $now = new \DateTimeImmutable(); $expected = $now->format('m/Y').'-00001'; $this->assertSame($expected, $result->getNumOrder()); } public function testGenerateIncrementsCounter(): void { $now = new \DateTimeImmutable(); $lastOrder = new OrderNumber($now->format('m/Y').'-00042'); $repo = $this->createStub(OrderNumberRepository::class); $repo->method('createQueryBuilder')->willReturn($this->createQueryBuilder($lastOrder)); $em = $this->createStub(EntityManagerInterface::class); $service = new OrderNumberService($repo, $em); $result = $service->generate(); $expected = $now->format('m/Y').'-00042'; $this->assertSame($expected, $result->getNumOrder()); } public function testGenerateAndUse(): void { $repo = $this->createStub(OrderNumberRepository::class); $repo->method('createQueryBuilder')->willReturn($this->createQueryBuilder(null)); $em = $this->createStub(EntityManagerInterface::class); $service = new OrderNumberService($repo, $em); $result = $service->generateAndUse(); $this->assertTrue($result->isUsed()); } public function testPreviewFirstOfMonth(): void { $repo = $this->createStub(OrderNumberRepository::class); $repo->method('createQueryBuilder')->willReturn($this->createQueryBuilder(null)); $em = $this->createStub(EntityManagerInterface::class); $service = new OrderNumberService($repo, $em); $result = $service->preview(); $now = new \DateTimeImmutable(); $this->assertSame($now->format('m/Y').'-00001', $result); } public function testPreviewIncrementsCounter(): void { $now = new \DateTimeImmutable(); $lastOrder = new OrderNumber($now->format('m/Y').'-00010'); $repo = $this->createStub(OrderNumberRepository::class); $repo->method('createQueryBuilder')->willReturn($this->createQueryBuilder($lastOrder)); $em = $this->createStub(EntityManagerInterface::class); $service = new OrderNumberService($repo, $em); $result = $service->preview(); $this->assertSame($now->format('m/Y').'-00010', $result); } public function testPreviewReturnsUnusedOrderNumber(): void { $now = new \DateTimeImmutable(); // An existing unused order number $unused = new OrderNumber($now->format('m/Y').'-00005'); // unused query returns it; lastOrder query also returns it (only first call matters) $repo = $this->createStub(OrderNumberRepository::class); $repo->method('createQueryBuilder')->willReturn($this->createQueryBuilder($unused)); $em = $this->createStub(EntityManagerInterface::class); $service = new OrderNumberService($repo, $em); $result = $service->preview(); // When unused order exists, preview returns its numOrder $this->assertSame($now->format('m/Y').'-00005', $result); } public function testGenerateReturnsUnusedOrderNumberWhenExists(): void { $now = new \DateTimeImmutable(); $unused = new OrderNumber($now->format('m/Y').'-00003'); $repo = $this->createStub(OrderNumberRepository::class); $repo->method('createQueryBuilder')->willReturn($this->createQueryBuilder($unused)); $em = $this->createStub(EntityManagerInterface::class); $service = new OrderNumberService($repo, $em); $result = $service->generate(); $this->assertSame($unused, $result); } }