Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
- Plugin
- Cron
- Sitemap
  • Loading branch information
zoglo committed Jun 1, 2023
1 parent 0b4105a commit 73d6ff1
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 1 deletion.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Composer
/composer.lock
/vendor/

# PhpUnit
/.phpunit.result.cache
/phpunit.xml

# IDE
/.idea
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
"contao/core-bundle": "^4.13 || ^5.1"
},
"require-dev": {
"contao/manager-plugin": "^2.0"
"contao/manager-plugin": "^2.3.1",
"contao/test-case": "^5.1",
"phpunit/phpunit": "^9.5",
"symfony/http-client": "^5.4 || ^6.0",
"symfony/phpunit-bridge": "^5.4 || ^6.0"
},
"conflict": {
"contao/core": "*",
Expand Down Expand Up @@ -60,5 +64,11 @@
"dev-main": "1.3.x-dev"
},
"contao-manager-plugin": "Oveleon\\ContaoRecommendationBundle\\ContaoManager\\Plugin"
},
"config": {
"allow-plugins": {
"php-http/discovery": true,
"contao/manager-plugin": true
}
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" colors="true" bootstrap="vendor/autoload.php">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
<env name="SYMFONY_PATCH_TYPE_DECLARATIONS" value="deprecations=0"/>
</php>
<testsuites>
<testsuite name="contao-recommendation-bundle">
<directory>./tests</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
28 changes: 28 additions & 0 deletions tests/ContaoManager/PluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace ContaoManager;

use Contao\CoreBundle\ContaoCoreBundle;
use Contao\ManagerPlugin\Bundle\Config\BundleConfig;
use Contao\ManagerPlugin\Bundle\Parser\ParserInterface;
use Oveleon\ContaoRecommendationBundle\ContaoManager\Plugin;
use Oveleon\ContaoRecommendationBundle\ContaoRecommendationBundle;
use PHPUnit\Framework\TestCase;

class PluginTest extends TestCase
{
public function testReturnsTheBundles(): void
{
$parser = $this->createMock(ParserInterface::class);

/** @var BundleConfig $config */
$config = (new Plugin())->getBundles($parser)[0];

$this->assertInstanceOf(BundleConfig::class, $config);
$this->assertSame(ContaoRecommendationBundle::class, $config->getName());
$this->assertSame([ContaoCoreBundle::class], $config->getLoadAfter());
$this->assertSame(['recommendation'], $config->getReplace());
}
}
33 changes: 33 additions & 0 deletions tests/Cron/PurgeRecommendationsCronTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Cron;

use Contao\Model\Collection;
use Contao\TestCase\ContaoTestCase;
use Oveleon\ContaoRecommendationBundle\Cron\PurgeRecommendationsCron;
use Oveleon\ContaoRecommendationBundle\Model\RecommendationModel;

class PurgeRecommendationsCronTest extends ContaoTestCase
{
public function testDeletesUnactivatedRecommendations(): void
{
$recommendationModel = $this->createMock(RecommendationModel::class);
$recommendationModel
->expects($this->once())
->method('delete')
;

$recommendationModelAdapter = $this->mockAdapter(['findExpiredRecommendations']);
$recommendationModelAdapter
->expects($this->once())
->method('findExpiredRecommendations')
->willReturn(new Collection([$recommendationModel], RecommendationModel::getTable()))
;

$framework = $this->mockContaoFramework([RecommendationModel::class => $recommendationModelAdapter]);

(new PurgeRecommendationsCron($framework, null))();
}
}
102 changes: 102 additions & 0 deletions tests/EventListener/SitemapListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace EventListener;

use Contao\CoreBundle\Event\SitemapEvent;
use Contao\Database;
use Oveleon\ContaoRecommendationBundle\EventListener\SitemapListener;
use Contao\PageModel;
use Contao\TestCase\ContaoTestCase;
use Oveleon\ContaoRecommendationBundle\Model\RecommendationArchiveModel;
use Oveleon\ContaoRecommendationBundle\Model\RecommendationModel;
use Symfony\Component\HttpFoundation\Request;

class SitemapListenerTest extends ContaoTestCase
{
protected function tearDown(): void
{
unset($GLOBALS['TL_CONFIG']);

parent::tearDown();
}

public function testNothingIsAddedIfNoPublishedArchive(): void
{
$adapters = [
RecommendationArchiveModel::class => $this->mockConfiguredAdapter(['findByProtected' => null]),
];

$sitemapEvent = $this->createSitemapEvent([]);
$listener = $this->createListener([], $adapters);
$listener($sitemapEvent);

$this->assertStringNotContainsString('<url><loc>', (string) $sitemapEvent->getDocument()->saveXML());
}

public function testRecommendationIsAdded(): void
{
$jumpToPage = $this->mockClassWithProperties(PageModel::class, [
'published' => 1,
'protected' => 0,
]);

$jumpToPage
->method('getAbsoluteUrl')
->willReturn('https://www.oveleon.de')
;

$adapters = [
RecommendationArchiveModel::class => $this->mockConfiguredAdapter([
'findByProtected' => [
$this->mockClassWithProperties(RecommendationArchiveModel::class, [
'jumpTo' => 21,
]),
],
]),
PageModel::class => $this->mockConfiguredAdapter([
'findWithDetails' => $jumpToPage,
]),
RecommendationModel::class => $this->mockConfiguredAdapter([
'findPublishedByPid' => [
$this->mockClassWithProperties(RecommendationModel::class, [
'jumpTo' => 21,
]),
],
]),
];

$sitemapEvent = $this->createSitemapEvent([1]);
$listener = $this->createListener([1, 21], $adapters);
$listener($sitemapEvent);

$this->assertStringContainsString('<url><loc>https://www.oveleon.de</loc></url>', (string) $sitemapEvent->getDocument()->saveXML());
}

private function createListener(array $allPages, array $adapters): SitemapListener
{
$database = $this->createMock(Database::class);
$database
->method('getChildRecords')
->willReturn($allPages)
;

$instances = [
Database::class => $database,
];

$framework = $this->mockContaoFramework($adapters, $instances);

return new SitemapListener($framework);
}

private function createSitemapEvent(array $rootPages): SitemapEvent
{
$sitemap = new \DOMDocument('1.0', 'UTF-8');
$urlSet = $sitemap->createElementNS('https://www.sitemaps.org/schemas/sitemap/0.9', 'urlset');
$sitemap->appendChild($urlSet);

return new SitemapEvent($sitemap, new Request(), $rootPages);
}
}

0 comments on commit 73d6ff1

Please sign in to comment.