Add Unittests

This commit is contained in:
Daniel Siepmann 2020-02-25 21:19:18 +01:00
parent d7c07aee79
commit 9452e66a51
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
8 changed files with 569 additions and 1 deletions

View file

@ -14,3 +14,6 @@ jobs:
- name: Test CGL
run: ./vendor/bin/phpcs
- name: Execute PHPUnit Tests
run: ./vendor/bin/phpunit

View file

@ -0,0 +1,169 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Domain\Model;
/*
* 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 DanielSiepmann\Tracking\Domain\Model\Pageview;
use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
/**
* @covers DanielSiepmann\Tracking\Domain\Model\Pageview
*/
class PageviewTest extends TestCase
{
/**
* @test
*/
public function canBeCreated()
{
$language = $this->prophesize(SiteLanguage::class);
$subject = new Pageview(
0,
$language->reveal(),
new \DateTimeImmutable(),
0,
'',
''
);
static::assertInstanceOf(Pageview::class, $subject);
}
/**
* @test
*/
public function returnsPageUid()
{
$language = $this->prophesize(SiteLanguage::class);
$subject = new Pageview(
500,
$language->reveal(),
new \DateTimeImmutable(),
0,
'',
''
);
static::assertSame(500, $subject->getPageUid());
}
/**
* @test
*/
public function returnsLanguage()
{
$language = $this->prophesize(SiteLanguage::class);
$subject = new Pageview(
0,
$language->reveal(),
new \DateTimeImmutable(),
0,
'',
''
);
static::assertSame($language->reveal(), $subject->getLanguage());
}
/**
* @test
*/
public function returnsCrdate()
{
$language = $this->prophesize(SiteLanguage::class);
$crdate = new \DateTimeImmutable();
$subject = new Pageview(
0,
$language->reveal(),
$crdate,
0,
'',
''
);
static::assertSame($crdate, $subject->getCrdate());
}
/**
* @test
*/
public function returnsPageType()
{
$language = $this->prophesize(SiteLanguage::class);
$subject = new Pageview(
0,
$language->reveal(),
new \DateTimeImmutable(),
999,
'',
''
);
static::assertSame(999, $subject->getPageType());
}
/**
* @test
*/
public function returnsUrl()
{
$language = $this->prophesize(SiteLanguage::class);
$subject = new Pageview(
0,
$language->reveal(),
new \DateTimeImmutable(),
0,
'https://example.com/path.html',
''
);
static::assertSame('https://example.com/path.html', $subject->getUrl());
}
/**
* @test
*/
public function returnsUserAgent()
{
$language = $this->prophesize(SiteLanguage::class);
$subject = new Pageview(
0,
$language->reveal(),
new \DateTimeImmutable(),
0,
'',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
);
static::assertSame(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0',
$subject->getUserAgent()
);
}
}

View file

@ -0,0 +1,196 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Domain\Pageview;
/*
* 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 DanielSiepmann\Tracking\Domain\Model\Pageview;
use DanielSiepmann\Tracking\Domain\Pageview\Factory;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
/**
* @covers DanielSiepmann\Tracking\Domain\Pageview\Factory
*/
class FactoryTest extends TestCase
{
/**
* @test
*/
public function returnsPageview()
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(0);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal());
static::assertInstanceOf(Pageview::class, $result);
}
/**
* @test
*/
public function returnedPageviewContainsUserAgent()
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(0);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
]);
$result = Factory::fromRequest($request->reveal());
static::assertSame(
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0',
$result->getUserAgent()
);
}
/**
* @test
*/
public function returnedPageviewContainsUri()
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(0);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('https://example.com/path?query=params&some=more#anchor');
$request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal());
static::assertSame(
'https://example.com/path?query=params&some=more#anchor',
$result->getUrl()
);
}
/**
* @test
*/
public function returnedPageviewContainsPageType()
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(50);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal());
static::assertSame(
50,
$result->getPageType()
);
}
/**
* @test
*/
public function returnedPageviewContainsDateTime()
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(0);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal());
static::assertInstanceOf(\DateTimeImmutable::class, $result->getCrdate());
}
/**
* @test
*/
public function returnedPageviewContainsLanguage()
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(0);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal());
static::assertInstanceOf(SiteLanguage::class, $result->getLanguage());
}
/**
* @test
*/
public function returnedPageviewContainsPageId()
{
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(0);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]);
$result = Factory::fromRequest($request->reveal());
static::assertSame(
10,
$result->getPageUid()
);
}
}

View file

@ -0,0 +1,72 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Domain\Repository;
/*
* 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 DanielSiepmann\Tracking\Domain\Model\Pageview as Model;
use DanielSiepmann\Tracking\Domain\Repository\Pageview;
use PHPUnit\Framework\TestCase;
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
/**
* @covers DanielSiepmann\Tracking\Domain\Repository\Pageview
*/
class PageviewTest extends TestCase
{
/**
* @test
*/
public function modelCanBeAdded()
{
$connection = $this->prophesize(Connection::class);
$dateTime = $this->prophesize(\DateTimeImmutable::class);
$dateTime->format('U')->willReturn(1582660189);
$language = $this->prophesize(SiteLanguage::class);
$language->getLanguageId()->willReturn(2);
$model = $this->prophesize(Model::class);
$model->getPageUid()->willReturn(10);
$model->getCrdate()->willReturn($dateTime->reveal());
$model->getPageType()->willReturn(999);
$model->getLanguage()->willReturn($language->reveal());
$model->getUrl()->willReturn('https://example.com/path.html');
$model->getUserAgent()->willReturn('Mozilla/5.0 (Windows NT 10.0) Gecko/20100101 Firefox/74.0');
$connection->insert(
'tx_tracking_pageview',
[
'pid' => 10,
'crdate' => 1582660189,
'tstamp' => 1582660189,
'type' => 999,
'sys_language_uid' => 2,
'url' => 'https://example.com/path.html',
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0) Gecko/20100101 Firefox/74.0',
]
)->willReturn(1)->shouldBeCalledTimes(1);
$subject = new Pageview($connection->reveal());
$subject->add($model->reveal());
}
}

View file

@ -0,0 +1,95 @@
<?php
namespace DanielSiepmann\Tracking\Unit\Middleware;
/*
* 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 DanielSiepmann\Tracking\Domain\Model\Pageview as Model;
use DanielSiepmann\Tracking\Domain\Repository\Pageview as Repository;
use DanielSiepmann\Tracking\Middleware\Pageview;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
/**
* @covers DanielSiepmann\Tracking\Middleware\Pageview
*/
class PageviewTest extends TestCase
{
/**
* @test
*/
public function doesNotAddBlacklistedRequest()
{
$repository = $this->prophesize(Repository::class);
$context = $this->prophesize(Context::class);
$rule = 'false';
$request = $this->prophesize(ServerRequestInterface::class);
$response = $this->prophesize(ResponseInterface::class);
$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle($request->reveal())->willReturn($response->reveal());
$repository->add()->shouldNotBeCalled();
$subject = new Pageview($repository->reveal(), $context->reveal(), $rule);
$result = $subject->process($request->reveal(), $handler->reveal());
static::assertInstanceOf(ResponseInterface::class, $result);
}
/**
* @test
*/
public function addsPageviewToRepository()
{
$repository = $this->prophesize(Repository::class);
$context = $this->prophesize(Context::class);
$rule = 'true';
$routing = $this->prophesize(PageArguments::class);
$routing->getPageId()->willReturn(10);
$routing->getPageType()->willReturn(0);
$language = $this->prophesize(SiteLanguage::class);
$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('routing')->willReturn($routing->reveal());
$request->getAttribute('language')->willReturn($language->reveal());
$request->getUri()->willReturn('');
$request->getHeader('User-Agent')->willReturn([]);
$response = $this->prophesize(ResponseInterface::class);
$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle($request->reveal())->willReturn($response->reveal());
$repository->add(Argument::type(Model::class))->shouldBeCalledtimes(1);
$subject = new Pageview($repository->reveal(), $context->reveal(), $rule);
$result = $subject->process($request->reveal(), $handler->reveal());
static::assertInstanceOf(ResponseInterface::class, $result);
}
}

View file

@ -14,6 +14,11 @@
"DanielSiepmann\\Tracking\\": "Classes/"
}
},
"autoload-dev": {
"psr-4": {
"DanielSiepmann\\Tracking\\Tests\\": "Tests/"
}
},
"require": {
"doctrine/dbal": "^2.10",
"php": "^7.3.0",
@ -43,6 +48,7 @@
]
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.5",
"phpunit/phpunit": "^9.0"
}
}

View file

@ -3,6 +3,7 @@
<description>This project coding standard</description>
<file>Classes/</file>
<file>Tests/</file>
<!-- Set default settings -->
<arg value="sp"/>

26
phpunit.xml.dist Normal file
View file

@ -0,0 +1,26 @@
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="false"
convertWarningsToExceptions="false"
forceCoversAnnotation="false"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false">
<testsuites>
<testsuite name="unit-tests">
<directory>Tests/Unit/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">Classes</directory>
</whitelist>
</filter>
</phpunit>