Skip to content

Commit

Permalink
fix: fix application open cases (dvsa/olcs-backend#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaLicense authored May 30, 2024
1 parent 1b5c87a commit 8e23405
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,16 @@ private function canWithdraw(Entity\Application\Application $application)
$underConsideration = $this->getRepo()->getRefdataReference($application::APPLICATION_STATUS_UNDER_CONSIDERATION)->getId();
$isUnderConsideration = ($status === $underConsideration);

try {
$openCases = $this->getRepo('Cases')->fetchOpenCasesForApplication($application->getId());
/**
* @var Repository\Cases $caseRepository
*/
$caseRepository = $this->getRepo('Cases');

if (count($openCases) > 0) {
return false;
}
return $isUnderConsideration;
} catch (\Exception) {
return $isUnderConsideration;
$openCases = $caseRepository->fetchOpenCasesForApplication($application->getId());

if (count($openCases) > 0) {
return false;
}
return $isUnderConsideration;
}
}
10 changes: 3 additions & 7 deletions app/api/module/Api/src/Domain/Repository/Cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ public function fetchOpenCasesForSurrender(
}

/**
* @return Entity\Cases\Cases[]
*
* @return mixed
* @throws Exception\NotFoundException
*/
public function fetchOpenCasesForApplication(int $id)
public function fetchOpenCasesForApplication(int $id): array
{
$qb = $this->createQueryBuilder();

Expand All @@ -237,10 +237,6 @@ public function fetchOpenCasesForApplication(int $id)
$expr->isNull($this->alias . '.closedDate')
);

$result = $qb->getQuery()->getResult();
if (!$result) {
throw new Exception\NotFoundException('Resource not found');
}
return $result[0];
return $qb->getQuery()->getResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function testHandleQuery()

$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -160,6 +161,7 @@ public function testHandleQueryDigitallySigned()
->andReturn('apsts_consideration');
$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -234,6 +236,7 @@ public function testHandleQueryWithDocs()
->once()
->andReturn('apsts_consideration');
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn([])->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -305,6 +308,7 @@ public function testHandleQueryWithPsv()
->andReturn('apsts_consideration');

$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn([])->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -368,6 +372,7 @@ public function testHandleQueryWithDocs2()

$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -435,6 +440,7 @@ public function testHandleQueryNoNeedToSign()

$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -504,6 +510,7 @@ public function testHandleQueryVariation()
->andReturn('apsts_consideration');
$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -565,6 +572,7 @@ public function testHandleQueryVariationUnchangedFinancialEvidence()

$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([$this->createMock(Entity\Cases\Cases::class), $this->createMock(Entity\Cases\Cases::class)]);

$this->mockAppRepo->shouldReceive('fetchUsingId')
->once()
Expand Down Expand Up @@ -645,6 +653,7 @@ public function testHandleQueryVariationUpdatedOcWithIncrease()
->andReturn('apsts_consideration');
$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -719,6 +728,7 @@ public function testHandleQueryVariationUpdatedOcWithoutIncrease()
->andReturn('apsts_consideration');
$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -775,6 +785,7 @@ public function testHandleQueryVariationWithNoApplicationData()

$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -832,6 +843,7 @@ public function testHandleQueryWisthSpecialRestrictedLicence()

$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([]);

$result = $this->sut->handleQuery($query);

Expand Down Expand Up @@ -888,7 +900,7 @@ public function testHandleQueryOpenCases()
$this->mockAppRepo->shouldReceive('getRefdataReference->getId')
->once()
->andReturn('apsts_consideration');
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn(["case", "case2"]);
$this->mockCaseRepo->shouldReceive('fetchOpenCasesForApplication')->with(111)->andReturn([$this->createMock(Entity\Cases\Cases::class), $this->createMock(Entity\Cases\Cases::class)]);
$mockFee = m::mock()->shouldReceive('getLatestPaymentRef')->andReturn('ref')->once()->getMock();
$this->mockFeeRepo->shouldReceive('fetchLatestPaidFeeByApplicationId')->with(111)->andReturn($mockFee)->once();

Expand Down
24 changes: 2 additions & 22 deletions app/api/test/module/Api/src/Domain/Repository/CasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,32 +340,12 @@ public function testFetchOpenCasesForApplication(): void
$qb->shouldReceive('getQuery')->andReturn(
m::mock()->shouldReceive('execute')
->shouldReceive('getResult')
->andReturn(['RESULTS'])
->andReturn([$this->createMock(CasesEntity::class), $this->createMock(CasesEntity::class)])
->getMock()
);
$result = $this->sut->fetchOpenCasesForApplication(1);
$expectedQuery = 'BLAH AND a.id = [[1]] AND m.closedDate IS NULL';
$this->assertEquals($expectedQuery, $this->query);
$this->assertEquals('RESULTS', $result);
}

public function testFetchOpenCasesForApplicationThrowsException(): void
{
$qb = $this->createMockQb('BLAH');

$this->mockCreateQueryBuilder($qb);

$this->queryBuilder
->shouldReceive('modifyQuery')->with($qb)->times(1)->andReturnSelf()
->shouldReceive('with')->with('application', 'a')->andReturnSelf();

$qb->shouldReceive('getQuery')->andReturn(
m::mock()->shouldReceive('execute')
->shouldReceive('getResult')
->andReturn([])
->getMock()
);
$this->expectException(NotFoundException::class);
$this->sut->fetchOpenCasesForApplication(1);
$this->assertCount(2, $result);
}
}

0 comments on commit 8e23405

Please sign in to comment.