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

Commit

Permalink
[TEST] Increased test converage
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Mar 31, 2019
1 parent bdc58a3 commit d22a37b
Show file tree
Hide file tree
Showing 12 changed files with 1,058 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Action/SitemapXMLActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\SitemapBundle\Tests\Action;

use Core23\SitemapBundle\Action\SitemapXMLAction;
use Core23\SitemapBundle\Generator\SitemapGeneratorInterface;
use PHPUnit\Framework\TestCase;

class SitemapXMLActionTest extends TestCase
{
public function testExecute(): void
{
$generator = $this->prophesize(SitemapGeneratorInterface::class);
$generator->toXML()
->willReturn('<xml></xml>')
;

$action = new SitemapXMLAction($generator->reveal());

$response = $action();

$this->assertSame('text/xml', $response->headers->get('Content-Type'));
$this->assertSame('<xml></xml>', $response->getContent());
}
}
41 changes: 41 additions & 0 deletions tests/Definition/DefinitionManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\SitemapBundle\Tests\Definition;

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

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

$this->assertInstanceOf(DefintionManagerInterface::class, $definition);
}

public function testAddDefintion(): void
{
$definition = new DefintionManager();
$definition->addDefinition('foo.definition', [
'foo' => 'bar',
]);

foreach ($definition->getAll() as $id => $item) {
$this->assertInstanceOf(SitemapDefinition::class, $item);
$this->assertSame('foo.definition', $id);
$this->assertSame([
'foo' => 'bar',
], $item->getSettings());
}
}
}
61 changes: 61 additions & 0 deletions tests/Definition/SitemapDefintionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\SitemapBundle\Tests\Definition;

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

class SitemapDefintionTest extends TestCase
{
public function testItIsInstantiable(): void
{
$definition = new SitemapDefinition('acme.sitemap');

$this->assertInstanceOf(SitemapDefinitionInterface::class, $definition);
$this->assertSame('acme.sitemap', $definition->getType());
$this->assertSame('acme.sitemap', $definition->toString());
$this->assertSame([], $definition->getSettings());
}

public function testSetting(): void
{
$definition = new SitemapDefinition('acme.sitemap');
$definition->setSettings([
'foo'=> 'bar',
]);

$this->assertSame('bar', $definition->getSetting('foo'));
}

public function testSettingWithDefault(): void
{
$definition = new SitemapDefinition('acme.sitemap');

$this->assertSame('baz', $definition->getSetting('foo', 'baz'));
}

public function testTtl(): void
{
$definition = new SitemapDefinition('acme.sitemap', [
'use_cache' => true,
'ttl' => 90,
]);

$this->assertSame(90, $definition->getTtl());
}

public function testTtlDefault(): void
{
$definition = new SitemapDefinition('acme.sitemap');

$this->assertSame(0, $definition->getTtl());
}
}
124 changes: 124 additions & 0 deletions tests/DependencyInjection/Compiler/SitemapCompilerPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\SitemapBundle\Tests\DependencyInjection\Compiler;

use Core23\SitemapBundle\Definition\DefintionManagerInterface;
use Core23\SitemapBundle\DependencyInjection\Compiler\SitemapCompilerPass;
use Core23\SitemapBundle\Sitemap\SitemapServiceManagerInterface;
use Core23\SitemapBundle\Sitemap\StaticSitemapService;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class SitemapCompilerPassTest extends TestCase
{
private $serviceManager;

private $definitionManager;

/**
* @var ContainerBuilder
*/
private $container;

protected function setUp()
{
$this->serviceManager = $this->prophesize(Definition::class);
$this->serviceManager->hasTag('core23.sitemap')
->willReturn(false)
;
$this->definitionManager = $this->prophesize(Definition::class);
$this->definitionManager->hasTag('core23.sitemap')
->willReturn(false)
;

$this->container = new ContainerBuilder();
$this->container->setDefinition(SitemapServiceManagerInterface::class, $this->serviceManager->reveal());
$this->container->setDefinition(DefintionManagerInterface::class, $this->definitionManager->reveal());
}

public function testProcess(): void
{
$this->serviceManager->addMethodCall('addSitemap', Argument::that(function ($args) {
return 'acme.sitemap' === $args[0] && $args[1] instanceof Reference;
}))
->shouldBeCalled()
;

$this->definitionManager->addMethodCall('addDefinition', [
'acme.sitemap',
])
->shouldBeCalled()
;

$sitemapDefinition = new Definition();
$sitemapDefinition->addTag('core23.sitemap');

$this->container->setParameter('core23_sitemap.static_urls', []);
$this->container->setDefinition('acme.sitemap', $sitemapDefinition);

$compiler = new SitemapCompilerPass();
$compiler->process($this->container);

$this->assertTrue($sitemapDefinition->isPublic());
}

public function testProcessWithNoServices(): void
{
$this->container->setParameter('core23_sitemap.static_urls', []);

$compiler = new SitemapCompilerPass();
$compiler->process($this->container);

$this->assertTrue(true);
}

public function testProcessWithStaticUrls(): void
{
$this->definitionManager->addMethodCall('addDefinition', [
StaticSitemapService::class,
[
[
'url' => 'http://example.com',
'priority' => 100,
'changefreq' => 'daily',
],
],
])
->shouldBeCalled()
;

$this->container->setParameter('core23_sitemap.static_urls', [
'static' => [
[
'url' => 'http://example.com',
'priority' => 100,
'changefreq' => 'daily',
],
],
]);

$compiler = new SitemapCompilerPass();
$compiler->process($this->container);
}

public function testProcessWithEmptyGroups(): void
{
$this->container->setParameter('core23_sitemap.static_urls', []);

$compiler = new SitemapCompilerPass();
$compiler->process($this->container);

$this->serviceManager->addMethodCall(Argument::any())->shouldNotBeCalled();
$this->definitionManager->addMethodCall(Argument::any())->shouldNotBeCalled();
}
}
68 changes: 68 additions & 0 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\SitemapBundle\Tests\DependencyInjection;

use Core23\SitemapBundle\DependencyInjection\Configuration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;

class ConfigurationTest extends TestCase
{
public function testOptions(): void
{
$processor = new Processor();

$config = $processor->processConfiguration(new Configuration(), [[
]]);

$expected = [
'static' => [
],
'cache' => [
'service' => null,
],
];

$this->assertSame($expected, $config);
}

public function testCronOptions(): void
{
$processor = new Processor();

$config = $processor->processConfiguration(new Configuration(), [[
'cache' => [
'service' => 'acme.foo.service',
],
'static' => [
[
'url' => 'http://example.com',
'priority' => 100,
'changefreq' => 'daily',
],
],
]]);

$expected = [
'cache' => [
'service' => 'acme.foo.service',
],
'static' => [
[
'url' => 'http://example.com',
'priority' => 100,
'changefreq' => 'daily',
],
],
];

$this->assertArraySubset($expected, $config);
}
}
Loading

0 comments on commit d22a37b

Please sign in to comment.