Skip to content
This repository has been archived by the owner on Sep 18, 2022. It is now read-only.

Commit

Permalink
[TEST] Fixed phpstan findings
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jul 31, 2019
1 parent bdfdda2 commit 5b5c396
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 63 deletions.
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ parameters:
# Symfony DI
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\).#'
- '#Cannot call method end\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null.#'
- "/Call to function method_exists.. with 'Symfony.+' and 'getRootNode' will always evaluate to false./"

# PHPUnit
-
message: '#Property .*::\$.* has no typehint specified.#'
path: tests/

2 changes: 2 additions & 0 deletions src/Definition/SitemapDefinitionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function getSettings(): array;
*
* @param string $name Key name
* @param mixed|null $default Default value
*
* @return mixed|null
*/
public function getSetting(string $name, $default = null);
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('core23_sitemap');

// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
if (!method_exists(TreeBuilder::class, 'getRootNode')) {
$rootNode = $treeBuilder->root('core23_sitemap');
} else {
$rootNode = $treeBuilder->getRootNode();
Expand Down
6 changes: 5 additions & 1 deletion src/Test/AbstractSitemapServiceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
use Core23\SitemapBundle\Sitemap\SitemapServiceInterface;
use DateTime;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\RouterInterface;

abstract class AbstractSitemapServiceTestCase extends TestCase
{
/**
* @var MockObject&RouterInterface
*/
protected $router;

/**
Expand Down Expand Up @@ -105,7 +109,7 @@ private function assertLastmod(?array $data, UrlInterface $url): void

\assert($data['lastmod'] instanceof \DateTime);

if ($url->getLastMod() <=> $data['lastmod']) {
if (null === $url->getLastMod() || $url->getLastMod() > $data['lastmod'] || $url->getLastMod() < $data['lastmod']) {
throw new AssertionFailedError(
sprintf("The url '%s' was expected with a different lastmod.", $url->getLoc())
);
Expand Down
5 changes: 3 additions & 2 deletions tests/Core23SitemapBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@

use Core23\SitemapBundle\Core23SitemapBundle;
use Core23\SitemapBundle\DependencyInjection\Compiler\SitemapCompilerPass;
use Core23\SitemapBundle\DependencyInjection\Core23SitemapExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;

final class Core23SitemapBundleTest extends TestCase
{
public function testItIsInstantiable(): void
public function testGetContainerExtension(): void
{
$bundle = new Core23SitemapBundle();

static::assertInstanceOf(Core23SitemapBundle::class, $bundle);
static::assertInstanceOf(Core23SitemapExtension::class, $bundle->getContainerExtension());
}

public function testBuild(): void
Expand Down
8 changes: 0 additions & 8 deletions tests/Definition/DefinitionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,11 @@
namespace Core23\SitemapBundle\Tests\Definition;

use Core23\SitemapBundle\Definition\DefintionManager;
use Core23\SitemapBundle\Definition\DefintionManagerInterface;
use Core23\SitemapBundle\Definition\SitemapDefinition;
use PHPUnit\Framework\TestCase;

final class DefinitionManagerTest extends TestCase
{
public function testItIsInstantiable(): void
{
$definition = new DefintionManager();

static::assertInstanceOf(DefintionManagerInterface::class, $definition);
}

public function testAddDefintion(): void
{
$definition = new DefintionManager();
Expand Down
2 changes: 0 additions & 2 deletions tests/Definition/SitemapDefintionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Core23\SitemapBundle\Tests\Definition;

use Core23\SitemapBundle\Definition\SitemapDefinition;
use Core23\SitemapBundle\Definition\SitemapDefinitionInterface;
use PHPUnit\Framework\TestCase;

final class SitemapDefintionTest extends TestCase
Expand All @@ -19,7 +18,6 @@ public function testItIsInstantiable(): void
{
$definition = new SitemapDefinition('acme.sitemap');

static::assertInstanceOf(SitemapDefinitionInterface::class, $definition);
static::assertSame('acme.sitemap', $definition->getType());
static::assertSame('acme.sitemap', $definition->toString());
static::assertSame('acme.sitemap', $definition->__toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public function testProcess(): void
$compiler->process($this->container);

static::assertTrue($sitemapDefinition->isPublic());

$this->definitionManager->addMethodCall('addDefintion', Argument::any())->shouldNotHaveBeenCalled();
}

public function testProcessWithNoServices(): void
Expand All @@ -79,7 +81,9 @@ public function testProcessWithNoServices(): void
$compiler = new SitemapCompilerPass();
$compiler->process($this->container);

static::assertTrue(true);
static::assertSame([], $this->container->getParameter('core23_sitemap.static_urls'));

$this->definitionManager->addMethodCall('addDefintion', Argument::any())->shouldNotHaveBeenCalled();
}

public function testProcessWithStaticUrls(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/DependencyInjection/Core23SitemapExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private function assertServices(): void
$this->assertContainerBuilderHasService(SitemapGenerator::class);
}

private function assertSitemap()
private function assertSitemap(): void
{
$this->assertContainerBuilderHasService(StaticSitemapService::class);
}
Expand Down
24 changes: 0 additions & 24 deletions tests/Exception/SitemapNotFoundExceptionTest.php

This file was deleted.

11 changes: 0 additions & 11 deletions tests/Generator/SitemapGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Core23\SitemapBundle\Definition\DefintionManagerInterface;
use Core23\SitemapBundle\Definition\SitemapDefinitionInterface;
use Core23\SitemapBundle\Generator\SitemapGenerator;
use Core23\SitemapBundle\Generator\SitemapGeneratorInterface;
use Core23\SitemapBundle\Model\Url;
use Core23\SitemapBundle\Model\UrlInterface;
use Core23\SitemapBundle\Sitemap\SitemapServiceInterface;
Expand Down Expand Up @@ -41,16 +40,6 @@ protected function setUp(): void
$this->defintionManager = $this->prophesize(DefintionManagerInterface::class);
}

public function testItIsInstantiable(): void
{
$generator = new SitemapGenerator(
$this->sitemapServiceManager->reveal(),
$this->defintionManager->reveal()
);

static::assertInstanceOf(SitemapGeneratorInterface::class, $generator);
}

public function testToXMLWithInvalidDefinition(): void
{
$expected = '<?xml version="1.0" encoding="UTF-8"?>';
Expand Down
23 changes: 11 additions & 12 deletions tests/Sitemap/SitemapServiceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,18 @@
use Core23\SitemapBundle\Exception\SitemapNotFoundException;
use Core23\SitemapBundle\Sitemap\SitemapServiceInterface;
use Core23\SitemapBundle\Sitemap\SitemapServiceManager;
use Core23\SitemapBundle\Sitemap\SitemapServiceManagerInterface;
use Core23\SitemapBundle\Tests\Fixtures\SitemapService;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use TypeError;

final class SitemapServiceManagerTest extends TestCase
{
public function testItIsInstantiable(): void
{
$manager = new SitemapServiceManager();

static::assertInstanceOf(SitemapServiceManagerInterface::class, $manager);
}

public function testCreationWithInvalidServices(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "stdClass" service is not a valid SitemapServiceInterface');
$this->expectException(TypeError::class);

new SitemapServiceManager([
'invalid' => new stdClass(),
Expand Down Expand Up @@ -126,6 +118,13 @@ public function testAddSitemap(): void
$manager = new SitemapServiceManager();
$manager->addSitemap('my-type', $service->reveal());

static::assertTrue(true);
$reflection = new ReflectionClass($manager);

$servicesProperty = $reflection->getProperty('services');
$servicesProperty->setAccessible(true);

static::assertSame([
'my-type' => $service->reveal(),
], $servicesProperty->getValue($manager));
}
}

0 comments on commit 5b5c396

Please sign in to comment.