22
33namespace App \Helpers \Mocks ;
44
5+ use App \Helpers \MetaFormats \FormatCache ;
6+ use App \Helpers \MetaFormats \MetaFormatHelper ;
57use App \Helpers \Mocks \MockUserStorage ;
68use App \V1Module \Presenters \BasePresenter ;
79use Nette \Application \Application ;
1113use Nette \Http \UrlScript ;
1214use Nette \Security \User ;
1315use Nette ;
16+ use ReflectionProperty ;
1417
1518class 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