Skip to content

Commit 3c75d27

Browse files
committed
Merge branch 'validation-bugfixes' into format-performance-testing
2 parents 66b23fd + d067cd0 commit 3c75d27

6 files changed

Lines changed: 124 additions & 4 deletions

File tree

tests/Mocks/MockHelper.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../../vendor/autoload.php';
4+
require_once __DIR__ . "/MockTemplate.php";
5+
require_once __DIR__ . "/MockTemplateFactory.php";
6+
require_once __DIR__ . "/MockUserStorage.php";
7+
8+
use App\Helpers\MetaFormats\FormatCache;
9+
use App\Helpers\MetaFormats\MetaFormatHelper;
10+
use App\V1Module\Presenters\BasePresenter;
11+
use Nette\Application\Application;
12+
use Nette\Application\PresenterFactory;
13+
use Nette\Application\Routers\RouteList;
14+
use Nette\Http\Response;
15+
use Nette\Http\UrlScript;
16+
use Nette\Security\User;
17+
18+
class MockHelper
19+
{
20+
/**
21+
* Initializes a presenter object with empty http request, response, and user objects.
22+
* This is intended to be called right after presenter instantiation and before calling the Presenter::run method.
23+
* @param BasePresenter $presenter The presenter to be initialized.
24+
*/
25+
public static function initPresenter(BasePresenter $presenter)
26+
{
27+
$httpRequest = new \Nette\Http\Request(new UrlScript());
28+
$httpResponse = new Response();
29+
$user = new User(new MockUserStorage());
30+
31+
$application = new Application(new PresenterFactory(), new RouteList("V1"), $httpRequest, $httpResponse);
32+
$presenter->application = $application;
33+
34+
$factory = new MockTemplateFactory();
35+
36+
$presenter->injectPrimary($httpRequest, $httpResponse, user: $user, templateFactory: $factory);
37+
}
38+
39+
/**
40+
* Injects a Format class to the FormatCache.
41+
* This method must not be used outside of testing, normal Format classes are discovered automatically.
42+
* @param string $format The Format class name.
43+
*/
44+
public static function injectFormat(string $format)
45+
{
46+
// make sure the cache is initialized (it uses lazy loading)
47+
FormatCache::getFormatToFieldDefinitionsMap();
48+
FormatCache::getFormatNamesHashSet();
49+
50+
// inject the format name
51+
$hashSetReflector = new ReflectionProperty(FormatCache::class, "formatNamesHashSet");
52+
$hashSetReflector->setAccessible(true);
53+
$formatNamesHashSet = $hashSetReflector->getValue();
54+
$formatNamesHashSet[$format] = true;
55+
$hashSetReflector->setValue(null, $formatNamesHashSet);
56+
57+
// inject the format definitions
58+
$formatMapReflector = new ReflectionProperty(FormatCache::class, "formatToFieldFormatsMap");
59+
$formatMapReflector->setAccessible(true);
60+
$formatToFieldFormatsMap = $formatMapReflector->getValue();
61+
$formatToFieldFormatsMap[$format] = MetaFormatHelper::createNameToFieldDefinitionsMap($format);
62+
$formatMapReflector->setValue(null, $formatToFieldFormatsMap);
63+
}
64+
}

tests/Mocks/MockTemplate.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
use Nette\Application\UI\Template;
4+
5+
class MockTemplate implements Template
6+
{
7+
public function render(): void
8+
{
9+
}
10+
11+
public function setFile(string $file): static
12+
{
13+
return $this;
14+
}
15+
16+
public function getFile(): ?string
17+
{
18+
return "test";
19+
}
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Nette\Application\UI\Control;
4+
use Nette\Application\UI\Template;
5+
use Nette\Application\UI\TemplateFactory;
6+
7+
class MockTemplateFactory implements TemplateFactory
8+
{
9+
public function createTemplate(?Control $control = null): Template
10+
{
11+
return new MockTemplate();
12+
}
13+
}

tests/Mocks/MockUserStorage.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Nette\Security\IIdentity;
4+
5+
class MockUserStorage implements Nette\Security\UserStorage
6+
{
7+
public function setExpiration(?string $expire, bool $clearIdentity): void
8+
{
9+
}
10+
11+
public function saveAuthentication(IIdentity $identity): void
12+
{
13+
}
14+
15+
public function clearAuthentication(bool $clearIdentity): void
16+
{
17+
}
18+
19+
public function getState(): array
20+
{
21+
return [false, null, 0];
22+
}
23+
}

tests/Validation/BasePresenter.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
require_once __DIR__ . "/../Mocks/MockHelper.php";
4+
35
use App\Exceptions\BadRequestException;
46
use App\Exceptions\InvalidApiArgumentException;
57
use App\Helpers\MetaFormats\Attributes\Format;
@@ -11,9 +13,7 @@ use App\Helpers\MetaFormats\Attributes\Post;
1113
use App\Helpers\MetaFormats\Attributes\Query;
1214
use App\Helpers\MetaFormats\FormatCache;
1315
use App\Helpers\MetaFormats\MetaFormat;
14-
use App\Helpers\MetaFormats\MetaFormatHelper;
1516
use App\Helpers\MetaFormats\Validators\VInt;
16-
use App\Helpers\Mocks\MockHelper;
1717
use App\V1Module\Presenters\BasePresenter;
1818
use Nette\Application\Request;
1919
use Tester\Assert;

tests/Validation/Formats.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
require_once __DIR__ . "/../Mocks/MockHelper.php";
4+
35
use App\Exceptions\BadRequestException;
46
use App\Exceptions\InternalServerException;
57
use App\Exceptions\InvalidApiArgumentException;
@@ -9,10 +11,8 @@ use App\Helpers\MetaFormats\Attributes\FPost;
911
use App\Helpers\MetaFormats\Attributes\FQuery;
1012
use App\Helpers\MetaFormats\FormatCache;
1113
use App\Helpers\MetaFormats\MetaFormat;
12-
use App\Helpers\MetaFormats\MetaFormatHelper;
1314
use App\Helpers\MetaFormats\Validators\VInt;
1415
use App\Helpers\MetaFormats\Validators\VObject;
15-
use App\Helpers\Mocks\MockHelper;
1616
use Tester\Assert;
1717

1818
$container = require_once __DIR__ . "/../bootstrap.php";

0 commit comments

Comments
 (0)