Skip to content

Format Unit Tests #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions app/helpers/MetaFormats/MetaFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Helpers\MetaFormats;

use App\Exceptions\BadRequestException;
use App\Exceptions\InternalServerException;
use App\Exceptions\InvalidApiArgumentException;

Expand Down Expand Up @@ -42,29 +43,26 @@ public function checkedAssign(string $fieldName, mixed $value)

/**
* Validates the given format.
* @return bool Returns whether the format and all nested formats are valid.
* @throws InvalidApiArgumentException Thrown when a value is not assignable.
* @throws BadRequestException Thrown when the structural constraints were not met.
*/
public function validate()
{
// check whether all higher level contracts hold
if (!$this->validateStructure()) {
return false;
throw new BadRequestException("The structural constraints of the format were not met.");
}

// go through all fields and check whether they were assigned properly
$fieldFormats = FormatCache::getFieldDefinitions(get_class($this));
foreach ($fieldFormats as $fieldName => $fieldFormat) {
if (!$this->checkIfAssignable($fieldName, $this->$fieldName)) {
return false;
}
$this->checkIfAssignable($fieldName, $this->$fieldName);

// check nested formats recursively
if ($this->$fieldName instanceof MetaFormat && !$this->$fieldName->validate()) {
return false;
if ($this->$fieldName instanceof MetaFormat) {
$this->$fieldName->validate();
}
}

return true;
}

/**
Expand Down
64 changes: 64 additions & 0 deletions tests/Mocks/MockHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . "/MockTemplate.php";
require_once __DIR__ . "/MockTemplateFactory.php";
require_once __DIR__ . "/MockUserStorage.php";

use App\Helpers\MetaFormats\FormatCache;
use App\Helpers\MetaFormats\MetaFormatHelper;
use App\V1Module\Presenters\BasePresenter;
use Nette\Application\Application;
use Nette\Application\PresenterFactory;
use Nette\Application\Routers\RouteList;
use Nette\Http\Response;
use Nette\Http\UrlScript;
use Nette\Security\User;

class MockHelper
{
/**
* Initializes a presenter object with empty http request, response, and user objects.
* This is intended to be called right after presenter instantiation and before calling the Presenter::run method.
* @param BasePresenter $presenter The presenter to be initialized.
*/
public static function initPresenter(BasePresenter $presenter)
{
$httpRequest = new \Nette\Http\Request(new UrlScript());
$httpResponse = new Response();
$user = new User(new MockUserStorage());

$application = new Application(new PresenterFactory(), new RouteList("V1"), $httpRequest, $httpResponse);
$presenter->application = $application;

$factory = new MockTemplateFactory();

$presenter->injectPrimary($httpRequest, $httpResponse, user: $user, templateFactory: $factory);
}

/**
* Injects a Format class to the FormatCache.
* This method must not be used outside of testing, normal Format classes are discovered automatically.
* @param string $format The Format class name.
*/
public static function injectFormat(string $format)
{
// make sure the cache is initialized (it uses lazy loading)
FormatCache::getFormatToFieldDefinitionsMap();
FormatCache::getFormatNamesHashSet();

// inject the format name
$hashSetReflector = new ReflectionProperty(FormatCache::class, "formatNamesHashSet");
$hashSetReflector->setAccessible(true);
$formatNamesHashSet = $hashSetReflector->getValue();
$formatNamesHashSet[$format] = true;
$hashSetReflector->setValue(null, $formatNamesHashSet);

// inject the format definitions
$formatMapReflector = new ReflectionProperty(FormatCache::class, "formatToFieldFormatsMap");
$formatMapReflector->setAccessible(true);
$formatToFieldFormatsMap = $formatMapReflector->getValue();
$formatToFieldFormatsMap[$format] = MetaFormatHelper::createNameToFieldDefinitionsMap($format);
$formatMapReflector->setValue(null, $formatToFieldFormatsMap);
}
}
20 changes: 20 additions & 0 deletions tests/Mocks/MockTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Nette\Application\UI\Template;

class MockTemplate implements Template
{
public function render(): void
{
}

public function setFile(string $file): static
{
return $this;
}

public function getFile(): ?string
{
return "test";
}
}
13 changes: 13 additions & 0 deletions tests/Mocks/MockTemplateFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Nette\Application\UI\Control;
use Nette\Application\UI\Template;
use Nette\Application\UI\TemplateFactory;

class MockTemplateFactory implements TemplateFactory
{
public function createTemplate(?Control $control = null): Template
{
return new MockTemplate();
}
}
23 changes: 23 additions & 0 deletions tests/Mocks/MockUserStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Nette\Security\IIdentity;

class MockUserStorage implements Nette\Security\UserStorage
{
public function setExpiration(?string $expire, bool $clearIdentity): void
{
}

public function saveAuthentication(IIdentity $identity): void
{
}

public function clearAuthentication(bool $clearIdentity): void
{
}

public function getState(): array
{
return [false, null, 0];
}
}
Loading