TYPO3ContentElements/Tests/Unit/SettingsFactoryTest.php

171 lines
6.3 KiB
PHP
Raw Permalink Normal View History

2020-09-23 08:42:34 +02:00
<?php
2021-01-23 15:52:16 +01:00
declare(strict_types=1);
namespace SkillDisplay\SkilldisplayContent\Tests\Unit;
2020-09-23 08:42:34 +02:00
/*
* Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use Prophecy\PhpUnit\ProphecyTrait;
2020-09-23 08:42:34 +02:00
use SkillDisplay\PHPToolKit\Configuration\Settings;
use SkillDisplay\SkilldisplayContent\SettingsFactory;
2020-09-23 08:42:34 +02:00
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase as TestCase;
2020-09-23 08:42:34 +02:00
/**
* @covers SkillDisplay\SkilldisplayContent\SettingsFactory
2020-09-23 08:42:34 +02:00
*/
class SettingsFactoryTest extends TestCase
{
use ProphecyTrait;
2020-09-23 08:42:34 +02:00
/**
* @test
*/
public function instanceCanBeCreated(): void
{
$siteFinder = $this->prophesize(SiteFinder::class);
$subject = new SettingsFactory($siteFinder->reveal());
2020-09-23 08:42:34 +02:00
static::assertInstanceOf(SettingsFactory::class, $subject);
}
/**
* @test
*/
public function returnsDefaultSettingsIfNothingIsConfiguredInCurrentSite(): void
2020-09-23 08:42:34 +02:00
{
$siteFinder = $this->prophesize(SiteFinder::class);
2020-09-23 08:42:34 +02:00
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([]);
$request = $this->prophesize(ServerRequest::class);
$request->getAttribute('site')->willReturn($site->reveal());
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$subject = new SettingsFactory($siteFinder->reveal());
2020-09-23 08:42:34 +02:00
$settings = $subject->createFromCurrentSiteConfiguration();
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(0, $settings->getVerifierID());
static::assertSame('', $settings->getUserSecret());
static::assertSame('', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
/**
* @test
*/
public function returnsSettingsFromCurrentSite(): void
{
$siteFinder = $this->prophesize(SiteFinder::class);
2020-09-23 08:42:34 +02:00
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([
'skilldisplay_api_key' => '---YOUR-API-KEY---',
'skilldisplay_verifier_id' => 10,
'skilldisplay_user_secret' => '---USER-SECRET---',
]);
$request = $this->prophesize(ServerRequest::class);
$request->getAttribute('site')->willReturn($site->reveal());
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$subject = new SettingsFactory($siteFinder->reveal());
2020-09-23 08:42:34 +02:00
$settings = $subject->createFromCurrentSiteConfiguration();
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(10, $settings->getVerifierID());
static::assertSame('---USER-SECRET---', $settings->getUserSecret());
static::assertSame('---YOUR-API-KEY---', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
/**
* @test
*/
public function throwsExceptionIfCurrentSiteCanNotBeFetched(): void
{
$siteFinder = $this->prophesize(SiteFinder::class);
2020-09-23 08:42:34 +02:00
$request = $this->prophesize(ServerRequest::class);
$request->getAttribute('site')->willReturn(null);
$GLOBALS['TYPO3_REQUEST'] = $request->reveal();
$subject = new SettingsFactory($siteFinder->reveal());
2020-09-23 08:42:34 +02:00
$this->expectExceptionMessage('Could not determine current site.');
$this->expectExceptionCode(1599721652);
$subject->createFromCurrentSiteConfiguration();
}
/**
* @test
*/
public function returnsDefaultSettingsIfNothingIsConfiguredForPageUidBasesSite(): void
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([]);
$siteFinder = $this->prophesize(SiteFinder::class);
$siteFinder->getSiteByPageId(10)->willReturn($site->reveal());
$subject = new SettingsFactory($siteFinder->reveal());
$settings = $subject->createFromPageUid(10);
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(0, $settings->getVerifierID());
static::assertSame('', $settings->getUserSecret());
static::assertSame('', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
/**
* @test
*/
public function returnsSettingsFromPageUidBasedSite(): void
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->willReturn([
'skilldisplay_api_key' => '---YOUR-API-KEY---',
'skilldisplay_verifier_id' => 10,
'skilldisplay_user_secret' => '---USER-SECRET---',
]);
$siteFinder = $this->prophesize(SiteFinder::class);
$siteFinder->getSiteByPageId(10)->willReturn($site->reveal());
$subject = new SettingsFactory($siteFinder->reveal());
$settings = $subject->createFromPageUid(10);
static::assertInstanceOf(Settings::class, $settings);
static::assertSame(10, $settings->getVerifierID());
static::assertSame('---USER-SECRET---', $settings->getUserSecret());
static::assertSame('---YOUR-API-KEY---', $settings->getApiKey());
static::assertSame('https://www.skilldisplay.eu', $settings->getAPIUrl());
static::assertSame('https://my.skilldisplay.eu', $settings->getMySkillDisplayUrl());
}
2020-09-23 08:42:34 +02:00
}