Skip to content

Commit

Permalink
Merge pull request #46 from 5pm-HDH/feature/refactor-code
Browse files Browse the repository at this point in the history
Refactor Code
  • Loading branch information
DumbergerL authored Aug 5, 2021
2 parents c916d1e + 5f97a56 commit a1fbdef
Show file tree
Hide file tree
Showing 42 changed files with 321 additions and 70 deletions.
1 change: 1 addition & 0 deletions .phpunit.cache/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*
1 change: 1 addition & 0 deletions .phpunit.cache/coverage-report/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

### Changed

- Refactoring: [refactor Code, introduce namespace to test-suites, update phpunit-configuration](https://github.com/5pm-HDH/churchtools-api/pull/46)
### Fixed


Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"test-unit": "./vendor/bin/phpunit --testsuite unit",
"test-integration": "./vendor/bin/phpunit --testsuite-integration",
"test-unit-w": "vendor\\bin\\phpunit.bat --testsuite unit",
"test-integration-w": "vendor\\bin\\phpunit.bat --testsuite integration"
"test-integration-w": "vendor\\bin\\phpunit.bat --testsuite integration",
"test-coverage-w": "vendor\\bin\\phpunit.bat --coverage-html .phpunit.cache/coverage-report"
}
}
24 changes: 22 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
<phpunit bootstrap="vendor/autoload.php">
<?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"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
forceCoversAnnotation="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="unit">
<directory>tests/unit</directory>
Expand All @@ -7,4 +19,12 @@
<directory>tests/integration</directory>
</testsuite>
</testsuites>
</phpunit>


<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
5 changes: 5 additions & 0 deletions src/CTClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static function getClient(): CTClient
return self::$client;
}

public static function setClient(CTClient $client)
{
self::$client = $client;
}

public static function createClient(?HandlerStack $handlerStack = null): void
{
if (is_null($handlerStack)) {
Expand Down
9 changes: 5 additions & 4 deletions src/CTConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

class CTConfig
{
private const PATH_LOGIN_TOKEN = 'query.login_token';

private static ?CTConfig $config = null;
private static string $cacheDir = __DIR__ . '/../cache/';

/**
* RequestOptions of Guzzle (https://docs.guzzlephp.org/en/stable/request-options.html)
Expand Down Expand Up @@ -76,17 +77,17 @@ public static function getApiUrl(): ?string
public static function authWithCredentials(string $email, string $password)
{
$auth = AuthRequest::authWithEmailAndPassword($email, $password);
self::setRequestOption('query.login_token', $auth->apiKey);
self::setRequestOption(self::PATH_LOGIN_TOKEN, $auth->apiKey);
}

public static function setApiKey(string $apiKey)
{
self::setRequestOption('query.login_token', $apiKey);
self::setRequestOption(self::PATH_LOGIN_TOKEN, $apiKey);
}

public static function getApiKey(): ?string
{
return self::getRequestOption('query.login_token');
return self::getRequestOption(self::PATH_LOGIN_TOKEN);
}

public static function validateConfig()
Expand Down
6 changes: 3 additions & 3 deletions src/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ public function requestAgenda(): EventAgenda

public function requestEventServiceWithServiceId(int $serviceId): ?EventService
{
$eventServices = array_filter($this->getEventServices(), function ($eventService) use ($serviceId) {
$requestedEventServices = array_filter($this->getEventServices(), function ($eventService) use ($serviceId) {
return $eventService->getServiceId() == $serviceId;
});
if (!empty($eventServices)) {
return $eventServices[array_key_first($eventServices)];
if (!empty($requestedEventServices)) {
return $requestedEventServices[array_key_first($requestedEventServices)];
}
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public function requestFileContent(): bool|string

public function getFileUrlAuthenticated(): ?string
{
$fileUrl = $this->getFileUrl();
$fileUrl2 = $this->getFileUrl();
$apiKey = CTConfig::getApiKey();
if (is_null($fileUrl) || is_null($apiKey)) {
if (is_null($fileUrl2) || is_null($apiKey)) {
return null;
}

return $fileUrl . "&login_token=" . $apiKey;
return $fileUrl2 . "&login_token=" . $apiKey;
}

public function getFileUrlBaseUrl(): ?string
Expand Down
3 changes: 1 addition & 2 deletions src/Models/Song.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ protected function fillNonArrayType(string $key, $value)
$this->setName($value);
break;
case "category":
$category = SongCategory::createModelFromData(["name" => $value]);
$this->setCategory($category);
$this->setCategory(SongCategory::createModelFromData(["name" => $value]));
break;
default:
$this->{$key} = $value;
Expand Down
6 changes: 3 additions & 3 deletions src/Models/SongArrangement.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ protected function fillArrayType(string $key, array $data)
*/
public function requestFirstFile(string $filename, ?string $fileExtension = null): ?File
{
$files = $this->getFiles();
$requestedFiles = $this->getFiles();

if (!is_null($fileExtension)) {
$files = array_filter($files, function ($file) use ($fileExtension) {
$requestedFiles = array_filter($requestedFiles, function ($file) use ($fileExtension) {
return str_ends_with($file->getName(), $fileExtension);
});
}

foreach ($files as $file) {
foreach ($requestedFiles as $file) {
if (str_contains(strtolower($file->getName()), strtolower($filename))) {
return $file;
}
Expand Down
24 changes: 12 additions & 12 deletions src/Models/WikiPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,30 @@ protected function fillNonArrayType(string $key, $value)

public function requestVersions(): ?WikiPageVersionRequestBuilder
{
$wikiCategory = $this->getWikiCategory()?->getId();
$identifier = $this->getIdentifier();
if (!is_null($wikiCategory) && !is_null($identifier)) {
return new WikiPageVersionRequestBuilder($wikiCategory, $identifier);
$requestedWikiCategory = $this->getWikiCategory()?->getId();
$requestedIdentifier = $this->getIdentifier();
if (!is_null($requestedWikiCategory) && !is_null($requestedIdentifier)) {
return new WikiPageVersionRequestBuilder($requestedWikiCategory, $requestedIdentifier);
}
return null;
}

public function requestVersion(int $versionId): ?WikiPage
{
$wikiCategory = $this->getWikiCategory()?->getId();
$identifier = $this->getIdentifier();
if (!is_null($wikiCategory) && !is_null($identifier)) {
return WikiPageVersionRequestBuilder::requestPageVersion($wikiCategory, $identifier, $versionId);
$requestedWikiCategory = $this->getWikiCategory()?->getId();
$requestedIdentifier = $this->getIdentifier();
if (!is_null($requestedWikiCategory) && !is_null($requestedIdentifier)) {
return WikiPageVersionRequestBuilder::requestPageVersion($requestedWikiCategory, $requestedIdentifier, $versionId);
}
return null;
}

public function requestText(): self
{
$wikiCategory = $this->getWikiCategory()?->getId();
$identifier = $this->getIdentifier();
if (!is_null($wikiCategory) && !is_null($identifier)) {
$page = WikiPageRequestBuilder::requestPageFromCategoryAndIdentifier($wikiCategory, $identifier);
$requestedWikiCategory = $this->getWikiCategory()?->getId();
$requestedIdentifier = $this->getIdentifier();
if (!is_null($requestedWikiCategory) && !is_null($requestedIdentifier)) {
$page = WikiPageRequestBuilder::requestPageFromCategoryAndIdentifier($requestedWikiCategory, $requestedIdentifier);
$this->setText($page->getText());
}
return $this;
Expand Down
7 changes: 5 additions & 2 deletions src/Utils/CTUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ class CTUtil
*/
public static function arrayIsDataContainer(array $data): bool
{
if (array() === $data) return false;
return array_keys($data) !== range(0, count($data) - 1);
if (array() === $data) {
return false;
} else {
return array_keys($data) !== range(0, count($data) - 1);
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/CTConfigIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tests\Integration;

use CTApi\CTConfig;
use CTApi\Exceptions\CTAuthException;
use PHPUnit\Framework\TestCase;
Expand Down
1 change: 1 addition & 0 deletions tests/integration/CacheTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

namespace Tests\Integration;

use CTApi\CTConfig;
use CTApi\CTLog;
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/Models/FileTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace Tests\Integration\Models;

use CTApi\CTConfig;
use CTApi\Models\File;
use CTApi\Requests\SongRequest;
use Tests\Integration\TestCaseAuthenticated;

class FileTest extends TestCaseAuthenticated
{
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/Models/MetaTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

namespace Tests\Integration\Models;

use CTApi\Models\Meta;
use CTApi\Models\Person;
use CTApi\Requests\EventAgendaRequest;
use CTApi\Requests\PersonRequest;
use CTApi\Requests\SongRequest;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class MetaTest extends TestCaseAuthenticated
{
Expand Down
24 changes: 0 additions & 24 deletions tests/integration/Models/ModelValidator.php

This file was deleted.

2 changes: 2 additions & 0 deletions tests/integration/Requests/AuthRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

namespace Tests\Integration\Requests;

use CTApi\CTConfig;
use CTApi\Requests\AuthRequest;
use PHPUnit\Framework\TestCase;
use Tests\Integration\TestData;

class AuthRequestTest extends TestCase
{
Expand Down
8 changes: 6 additions & 2 deletions tests/integration/Requests/EventAgendaTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

namespace Tests\Integration\Requests;

use CTApi\Models\EventAgenda;
use CTApi\Models\Song;
use CTApi\Models\SongArrangement;
use CTApi\Requests\EventAgendaRequest;
use CTApi\Requests\EventRequest;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class EventAgendaTest extends TestCaseAuthenticated
{
Expand All @@ -23,7 +26,6 @@ public function testGetAgenda()
$agenda = EventAgendaRequest::fromEvent($eventId)->get();

$this->assertAgendaIsValid($agenda);
ModelValidator::validateModel($agenda);
}

public function testGetAgendaFromEvent()
Expand Down Expand Up @@ -118,7 +120,9 @@ private function assertTestSongIsInSongArray($songArray, $checkForArrangement =
{
$foundSong = false;
foreach ($songArray as $song) {
if (is_null($song)) continue;
if (is_null($song)) {
continue;
}
if (
$song->getName() == TestData::getValue("EVENT_AGENDA_SONG_NAME")
) {
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/Requests/EventRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

namespace Tests\Integration\Requests;

use CTApi\Exceptions\CTModelException;
use CTApi\Models\Event;
use CTApi\Models\Person;
use CTApi\Models\Service;
use CTApi\Requests\EventRequest;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class EventRequestTest extends TestCaseAuthenticated
{
Expand All @@ -24,7 +27,6 @@ public function testGetAllEvents()

$this->assertFalse(empty(sizeof($allEvents)));
$this->assertInstanceOf(Event::class, $allEvents[0]);
ModelValidator::validateModel($allEvents[0]);
}

public function testGetWhereEvents()
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/Requests/PersonRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?php

namespace Tests\Integration\Requests;

use CTApi\Models\Event;
use CTApi\Models\Person;
use CTApi\Requests\AuthRequest;
use CTApi\Requests\PersonRequest;
use Exception;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class PersonRequestTest extends TestCaseAuthenticated
{
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/Requests/ServiceRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

namespace Tests\Integration\Requests;

use CTApi\Exceptions\CTRequestException;
use CTApi\Models\Service;
use CTApi\Models\ServiceGroup;
use CTApi\Requests\ServiceGroupRequest;
use CTApi\Requests\ServiceRequest;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class ServiceRequestTest extends TestCaseAuthenticated
{
Expand Down
Loading

0 comments on commit a1fbdef

Please sign in to comment.