Skip to content

Commit

Permalink
Adds unit test for getDigitalForm method on the service.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryguyk committed Jan 22, 2025
1 parent 7bbc90a commit 132c2eb
Showing 1 changed file with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ protected function setUp(): void {
}

/**
* Helper function to DRY up expectation setup.
* Helper function to DRY up expectation setup for getDigitalForms.
*/
private function setUpMockQuery($publishedOnly, $hasResults = TRUE) {
private function setUpMockQueryGetDigitalForms($publishedOnly, $hasResults = TRUE) {
// Mock the entity storage.
$entityStorage = $this->createMock(EntityStorageInterface::class);

Expand Down Expand Up @@ -127,7 +127,7 @@ private function setUpMockQuery($publishedOnly, $hasResults = TRUE) {
public function testGetDigitalFormsPublishedOnlyTrue() {
// We will call the method with $publishedOnly = TRUE,
// so we set up expectations accordingly.
$this->setUpMockQuery(TRUE);
$this->setUpMockQueryGetDigitalForms(TRUE);

// Call the method, which asserts expectations set in setup.
$this->digitalFormsService->getDigitalForms(TRUE);
Expand All @@ -143,7 +143,7 @@ public function testGetDigitalFormsPublishedOnlyTrue() {
public function testGetDigitalFormsPublishedOnlyFalse() {
// We will call the method with $publishedOnly = FALSE,
// so we set up expectations accordingly.
$this->setUpMockQuery(FALSE);
$this->setUpMockQueryGetDigitalForms(FALSE);

// Call the method, which asserts expectations set in setup.
$this->digitalFormsService->getDigitalForms(FALSE);
Expand All @@ -159,7 +159,7 @@ public function testGetDigitalFormsPublishedOnlyDefault() {
// and we want to ensure that the expectations are set up
// as though the value were set to TRUE, which is the
// expected default.
$this->setUpMockQuery(TRUE);
$this->setUpMockQueryGetDigitalForms(TRUE);

// Call the method, which asserts expectations set in setup.
$this->digitalFormsService->getDigitalForms();
Expand All @@ -183,7 +183,7 @@ public function testGetDigitalFormsNoResults() {
// 2. That `$this->entityTypeManager->getStorage('node')`
// will be called only once, rather than twice,
// since there will be no need to call `loadMultiple`.
$this->setUpMockQuery(TRUE, FALSE);
$this->setUpMockQueryGetDigitalForms(TRUE, FALSE);

// Call the method, which asserts expectations set in setup.
$result = $this->digitalFormsService->getDigitalForms(TRUE);
Expand All @@ -192,4 +192,45 @@ public function testGetDigitalFormsNoResults() {
$this->assertCount(0, $result);
}

/**
* Helper function to DRY up expectation setup for getDigitalForm.
*/
private function setUpMockQueryGetDigitalForm() {
// Mock the entity storage.
$entityStorage = $this->createMock(EntityStorageInterface::class);
$entityStorage->expects($this->once())
->method('load')
->willReturnMap([
['1', $this->createMock('Drupal\node\NodeInterface')],
]);

// Mock the entity type manager.
$this->entityTypeManager->expects($this->once())
->method('getStorage')
->with('node')
->willReturn($entityStorage);
}

/**
* Tests getDigitalForm() with $nid of existing node.
*/
public function testGetDigitalFormReturnsNode() {
$this->setUpMockQueryGetDigitalForm();

// Call the method with a nid that exists.
$node = $this->digitalFormsService->getDigitalForm('1');
$this->assertNotEmpty($node);
}

/**
* Tests getDigitalForm() with $nid of non-existent node.
*/
public function testGetDigitalFormReturnsNull() {
$this->setUpMockQueryGetDigitalForm();

// Call the method with a nid that does not exist.
$node = $this->digitalFormsService->getDigitalForm('99999');
$this->assertEmpty($node);
}

}

0 comments on commit 132c2eb

Please sign in to comment.