Skip to content

Commit

Permalink
Merge pull request #113 from exonet/tsi/utest
Browse files Browse the repository at this point in the history
Add unit test for getShortName
  • Loading branch information
robbinjanssen authored Jun 20, 2022
2 parents 3342521 + c128397 commit bb65efc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Resources/ResourceRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,22 +243,22 @@ public function getName(): string
* Get the name of the resource record without the domain name.
* "@" when empty.
*
* @throws PowerdnsException
* @throws PowerdnsException If it is not possible to shorten the name.
*
* @return string The short name
*/
public function getShortName(): string
{
if ($this->zone !== null) {
$name = substr($this->name, 0, -(strlen($this->zone->getCanonicalName()) + 1));

if (strlen($name) == 0) {
$name = '@';
}
} else {
if ($this->zone === null) {
throw new PowerdnsException('No zone set for this ResourceRecord. Unable to shorten name');
}

$name = substr($this->name, 0, -(strlen($this->zone->getCanonicalName()) + 1));

if ($name === '' || $name === false) {
return '@';
}

return $name;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Resources/ResourceRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,24 @@ public function testSetTypeWithInvalidTypeThrowsException()

$resourceRecord->setType('test');
}

public function testGetShortName(): void
{
$zone = Mockery::mock(Zone::class);
$zone->shouldReceive('getCanonicalName')->once()->andReturn('test-zone.dev');

$resourceRecord = new ResourceRecord();
$resourceRecord->setName('unit.test-zone.dev');

$resourceRecord->setZone($zone);
$this->assertSame('unit.test-zone.dev', $resourceRecord->getName());
$this->assertSame('unit', $resourceRecord->getShortName());

$resourceRecord->setName('test-zone.dev');
$this->assertSame('test-zone.dev', $resourceRecord->getName());
$this->assertSame('@', $resourceRecord->getShortName());

$this->expectExceptionMessage('No zone set for this ResourceRecord. Unable to shorten name');
(new ResourceRecord())->getShortName();
}
}

0 comments on commit bb65efc

Please sign in to comment.