11<?php namespace com \handlebarsjs \unittest ;
22
3+ use com \github \mustache \{FilesIn , InMemory };
34use com \handlebarsjs \{HandlebarsEngine , HandlebarsParser };
45use io \streams \MemoryOutputStream ;
5- use lang \IllegalArgumentException ;
6- use test \{Assert , Expect , Test };
6+ use io \{Path , File , Files , Folder };
7+ use lang \{IllegalArgumentException , Environment };
8+ use test \{Assert , Expect , Test , Values };
79use util \log \LogCategory ;
810
911class EngineTest {
1012
13+ /** @return iterable */
14+ private function files () {
15+ yield [function ($ temp ) { return $ temp ->getURI (); }, 'string ' ];
16+ yield [function ($ temp ) { return new Folder ($ temp ->getURI ()); }, 'io.Folder ' ];
17+ yield [function ($ temp ) { return new Path ($ temp ->getURI ()); }, 'io.Path ' ];
18+ yield [function ($ temp ) { return new FilesIn ($ temp , ['.handlebars ' ]); }, 'com.github.mustache.FilesIn ' ];
19+ }
20+
1121 #[Test]
1222 public function can_create () {
1323 new HandlebarsEngine ();
@@ -67,4 +77,38 @@ public function version() { return '1.0.0'; }
6777 });
6878 Assert::equals ('1.0.0 ' , $ engine ->parser ()->version ());
6979 }
80+
81+ #[Test]
82+ public function templates_initially_empty () {
83+ $ engine = new HandlebarsEngine ();
84+ Assert::equals ([], $ engine ->templates ()->listing ()->templates ());
85+ }
86+
87+ #[Test]
88+ public function templates_from_loader () {
89+ $ engine = (new HandlebarsEngine ())->withTemplates (new InMemory (['test ' => 'Hello {{name}} ' ]));
90+ Assert::equals (['test ' ], $ engine ->templates ()->listing ()->templates ());
91+ }
92+
93+ #[Test]
94+ public function templates_from_map () {
95+ $ engine = (new HandlebarsEngine ())->withTemplates (['test ' => 'Hello {{name}} ' ]);
96+ Assert::equals (['test ' ], $ engine ->templates ()->listing ()->templates ());
97+ }
98+
99+ #[Test, Values(from: 'files ' )]
100+ public function templates_from_files ($ arg ) {
101+ $ temp = new Folder (Environment::tempDir (), md5 (self ::class));
102+ $ temp ->create ();
103+
104+ try {
105+ Files::write (new File ($ temp , 'test.handlebars ' ), 'Hello {{name}} ' );
106+ Files::write (new File ($ temp , 'translations.csv ' ), 'Not included in listing ' );
107+
108+ $ engine = (new HandlebarsEngine ())->withTemplates ($ arg ($ temp ));
109+ Assert::equals (['test ' ], $ engine ->templates ()->listing ()->templates ());
110+ } finally {
111+ $ temp ->unlink ();
112+ }
113+ }
70114}
0 commit comments