44
55namespace Yiisoft \View \Tests ;
66
7- use Yiisoft \Composer \Config \Builder ;
87use PHPUnit \Framework \TestCase as BaseTestCase ;
98use Psr \Container \ContainerInterface ;
109use Psr \EventDispatcher \EventDispatcherInterface ;
1110use Psr \EventDispatcher \ListenerProviderInterface ;
1211use Psr \Log \LoggerInterface ;
12+ use Psr \Log \NullLogger ;
1313use Yiisoft \Aliases \Aliases ;
1414use Yiisoft \Di \Container ;
15+ use Yiisoft \Factory \Definitions \Reference ;
16+ use Yiisoft \EventDispatcher \Dispatcher \Dispatcher ;
17+ use Yiisoft \EventDispatcher \Provider \Provider ;
1518use Yiisoft \Files \FileHelper ;
1619use Yiisoft \View \Theme ;
1720use Yiisoft \View \View ;
1821use Yiisoft \View \WebView ;
22+ use Yiisoft \View \Tests \Mocks \WebViewPlaceholderMock ;
23+
24+ use function str_replace ;
1925
2026abstract class TestCase extends BaseTestCase
2127{
22- /**
23- * @var Aliases $aliases
24- */
25- protected $ aliases ;
26-
27- /**
28- * @var ContainerInterface $container
29- */
30- private $ container ;
31-
32- /**
33- * @var EventDispatcherInterface $eventDispatcher
34- */
35- protected $ eventDispatcher ;
36-
37- /**
38- * @var LoggerInterface $logger
39- */
40- protected $ logger ;
41-
42- /**
43- * @var Theme $theme
44- */
45- protected $ theme ;
28+ private ContainerInterface $ container ;
29+ private EventDispatcherInterface $ eventDispatcher ;
30+ private LoggerInterface $ logger ;
31+ protected Aliases $ aliases ;
32+ protected WebView $ webView ;
33+ protected WebViewPlaceholderMock $ webViewPlaceholderMock ;
4634
47- /**
48- * @var WebView $webView
49- */
50- protected $ webView ;
51-
52- /**
53- * @var ListenerProviderInterface
54- */
55- protected $ listenerProvider ;
56-
57- /**
58- * setUp
59- *
60- * @return void
61- */
6235 protected function setUp (): void
6336 {
6437 parent ::setUp ();
6538
66- $ config = require Builder::path ('tests ' );
67-
68- $ this ->container = new Container ($ config );
39+ $ this ->container = new Container ($ this ->config ());
6940
7041 $ this ->aliases = $ this ->container ->get (Aliases::class);
7142 $ this ->eventDispatcher = $ this ->container ->get (EventDispatcherInterface::class);
72- $ this ->listenerProvider = $ this ->container ->get (ListenerProviderInterface::class);
7343 $ this ->logger = $ this ->container ->get (LoggerInterface::class);
7444 $ this ->webView = $ this ->container ->get (WebView::class);
45+ $ this ->webViewPlaceholderMock = $ this ->container ->get (WebViewPlaceholderMock::class);
7546 }
7647
7748 protected function getContainer (): ContainerInterface
@@ -86,7 +57,7 @@ protected function getContainer(): ContainerInterface
8657 */
8758 protected function tearDown (): void
8859 {
89- $ this ->container = null ;
60+ unset( $ this ->container , ) ;
9061 parent ::tearDown ();
9162 }
9263
@@ -125,11 +96,11 @@ protected function assertSameIgnoringSlash(string $expected, string $actual): vo
12596 * Create view tests.
12697 *
12798 * @param string $basePath
128- * @param Theme $theme
99+ * @param Theme|null $theme
129100 *
130101 * @return View
131102 */
132- protected function createView ($ basePath , Theme $ theme = null ): View
103+ protected function createView (string $ basePath , ? Theme $ theme = null ): View
133104 {
134105 return new View ($ basePath , $ theme ?: new Theme (), $ this ->eventDispatcher , $ this ->logger );
135106 }
@@ -140,4 +111,55 @@ protected function touch(string $path): void
140111
141112 touch ($ path );
142113 }
114+
115+ private function config (): array
116+ {
117+ return [
118+ Aliases::class => [
119+ '__class ' => Aliases::class,
120+ '__construct() ' => [
121+ [
122+ '@root ' => __DIR__ ,
123+ '@baseUrl ' => '/baseUrl '
124+ ]
125+ ]
126+ ],
127+
128+ LoggerInterface::class => NullLogger::class,
129+
130+ ListenerProviderInterface::class => Provider::class,
131+
132+ EventDispatcherInterface::class => Dispatcher::class,
133+
134+ View::class => [
135+ '__class ' => View::class,
136+ '__construct() ' => [
137+ __DIR__ . '/public/view ' ,
138+ Reference::to (Theme::class),
139+ Reference::to (EventDispatcherInterface::class),
140+ Reference::to (LoggerInterface::class)
141+ ]
142+ ],
143+
144+ WebView::class => [
145+ '__class ' => WebView::class,
146+ '__construct() ' => [
147+ __DIR__ . '/public/view ' ,
148+ Reference::to (Theme::class),
149+ Reference::to (EventDispatcherInterface::class),
150+ Reference::to (LoggerInterface::class)
151+ ]
152+ ],
153+
154+ WebViewPlaceholderMock::class => [
155+ '__class ' => WebViewPlaceholderMock::class,
156+ '__construct() ' => [
157+ __DIR__ . '/public/view ' ,
158+ Reference::to (Theme::class),
159+ Reference::to (EventDispatcherInterface::class),
160+ Reference::to (LoggerInterface::class)
161+ ]
162+ ]
163+ ];
164+ }
143165}
0 commit comments