Skip to content

Commit

Permalink
Add test for DescriptorRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
mvriel authored and jaapio committed Mar 1, 2024
1 parent d6ae76d commit 7e70c1f
Showing 1 changed file with 138 additions and 0 deletions.
138 changes: 138 additions & 0 deletions tests/unit/phpDocumentor/Compiler/DescriptorRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Compiler;

use phpDocumentor\Descriptor\ApiSetDescriptor;
use phpDocumentor\Descriptor\Collection;
use phpDocumentor\Descriptor\VersionDescriptor;
use phpDocumentor\Faker\Faker;
use phpDocumentor\Reflection\Fqsen;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/**
* @coversDefaultClass \phpDocumentor\Compiler\DescriptorRepository
* @covers ::<private>
*/
final class DescriptorRepositoryTest extends TestCase
{
use ProphecyTrait;
use Faker;

private Fqsen $fqsen;
private DescriptorRepository $descriptorRepository;

protected function setUp(): void
{
$this->fqsen = $this->faker()->fqsen();
$this->descriptorRepository = new DescriptorRepository();
}

/**
* @uses VersionDescriptor
* @uses Collection
*
* @covers ::setVersionDescriptor
*/
public function testSetVersionDescriptorMethod(): void
{
$versionDescriptor = $this->faker()->versionDescriptor([]);

$this->descriptorRepository->setVersionDescriptor($versionDescriptor);

$this->assertSame(
$versionDescriptor,
$this->descriptorRepository->getVersionDescriptor(),
'The set and retrieved version descriptors must be the same.',
);
}

/**
* @uses VersionDescriptor
* @uses Collection
*
* @covers ::getVersionDescriptor
*/
public function testGetVersionDescriptorMethod(): void
{
$versionDescriptor = $this->faker()->versionDescriptor([]);

$this->descriptorRepository->setVersionDescriptor($versionDescriptor);

$this->assertSame(
$versionDescriptor,
$this->descriptorRepository->getVersionDescriptor(),
);
}

/**
* @uses VersionDescriptor
* @uses ApiSetDescriptor
*
* @covers ::findDescriptorByFqsen
*/
public function testItCanFindDescriptorByFqsen(): void
{
$classDescriptor = $this->faker()->classDescriptor($this->fqsen);

$apiSetDescriptor = $this->faker()->apiSetDescriptor();
$apiSetDescriptor->getIndex('elements')->set((string) $this->fqsen, $classDescriptor);

$this->descriptorRepository->setVersionDescriptor($this->faker()->versionDescriptor([$apiSetDescriptor]));

$this->assertSame(
$classDescriptor,
$this->descriptorRepository->findDescriptorByFqsen($this->fqsen),
);
}

/**
* @uses VersionDescriptor
* @uses ApiSetDescriptor
*
* @covers ::findDescriptorByFqsen
*/
public function testItReturnsNullWhenNoDescriptorIsFoundByFqsen(): void
{
$this->descriptorRepository->setVersionDescriptor(
$this->faker()->versionDescriptor([$this->faker()->apiSetDescriptor()]),
);

$this->assertNull($this->descriptorRepository->findDescriptorByFqsen($this->fqsen));
}

/**
* @uses VersionDescriptor
* @uses ApiSetDescriptor
*
* @covers ::findDescriptorByTypeAndFqsen
*/
public function testItCanFindDescriptorByTypeAndFqsen(): void
{
$classDescriptor = $this->faker()->classDescriptor($this->fqsen);

$apiSetDescriptor = $this->faker()->apiSetDescriptor();
$apiSetDescriptor
->getIndex('classes')
->set((string) $this->fqsen, $classDescriptor);

$this->descriptorRepository
->setVersionDescriptor($this->faker()->versionDescriptor([$apiSetDescriptor]));

$this->assertSame(
$classDescriptor,
$this->descriptorRepository->findDescriptorByTypeAndFqsen('class', $this->fqsen),
);
}
}

0 comments on commit 7e70c1f

Please sign in to comment.