Skip to content

Commit ef0840e

Browse files
committed
added more tests and comments
1 parent 804d6c7 commit ef0840e

4 files changed

Lines changed: 268 additions & 77 deletions

File tree

app/helpers/Mocks/MockHelper.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Helpers\Mocks;
44

5+
use App\Helpers\MetaFormats\FormatCache;
6+
use App\Helpers\MetaFormats\MetaFormatHelper;
57
use App\Helpers\Mocks\MockUserStorage;
68
use App\V1Module\Presenters\BasePresenter;
79
use Nette\Application\Application;
@@ -11,6 +13,7 @@
1113
use Nette\Http\UrlScript;
1214
use Nette\Security\User;
1315
use Nette;
16+
use ReflectionProperty;
1417

1518
class MockHelper
1619
{
@@ -32,4 +35,30 @@ public static function initPresenter(BasePresenter $presenter)
3235

3336
$presenter->injectPrimary($httpRequest, $httpResponse, user: $user, templateFactory: $factory);
3437
}
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+
}
3564
}

0 commit comments

Comments
 (0)