Passed
Push — master ( 694c78...f1dcbc )
by Aimeos
06:27 queued 02:01
created

TestHelperHtml::view()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 23
nc 2
nop 2
dl 0
loc 36
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 */
8
class TestHelperHtml
9
{
10
	private static $aimeos;
11
	private static $context = [];
12
13
14
	public static function bootstrap()
15
	{
16
		self::getAimeos();
17
		\Aimeos\MShop::cache( false );
18
		\Aimeos\Controller\Frontend::cache( false );
19
	}
20
21
22
	public static function getContext( $site = 'unittest' )
23
	{
24
		if( !isset( self::$context[$site] ) ) {
25
			self::$context[$site] = self::createContext( $site );
26
		}
27
28
		return clone self::$context[$site];
29
	}
30
31
32
	public static function view( $site = 'unittest', \Aimeos\MW\Config\Iface $config = null )
33
	{
34
		if( $config === null ) {
35
			$config = self::getContext( $site )->getConfig();
36
		}
37
38
		$view = new \Aimeos\MW\View\Standard( self::getHtmlTemplatePaths() );
39
40
		$trans = new \Aimeos\MW\Translation\None( 'de_DE' );
41
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $trans );
42
		$view->addHelper( 'translate', $helper );
43
44
		$helper = new \Aimeos\MW\View\Helper\Url\Standard( $view, 'http://baseurl' );
45
		$view->addHelper( 'url', $helper );
46
47
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, '.', '' );
48
		$view->addHelper( 'number', $helper );
49
50
		$helper = new \Aimeos\MW\View\Helper\Date\Standard( $view, 'Y-m-d' );
51
		$view->addHelper( 'date', $helper );
52
53
		$config = new \Aimeos\MW\Config\Decorator\Protect( $config, ['client', 'common', 'resource/fs/baseurl'] );
54
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
55
		$view->addHelper( 'config', $helper );
56
57
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_csrf_token', '_csrf_value' );
58
		$view->addHelper( 'csrf', $helper );
59
60
		$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
61
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $view, $psr17Factory->createServerRequest( 'POST', 'https://aimeos.org' ) );
62
		$view->addHelper( 'request', $helper );
63
64
		$helper = new \Aimeos\MW\View\Helper\Response\Standard( $view, $psr17Factory->createResponse() );
65
		$view->addHelper( 'response', $helper );
66
67
		return $view;
68
	}
69
70
71
	public static function getHtmlTemplatePaths()
72
	{
73
		return self::getAimeos()->getTemplatePaths( 'client/html/templates' );
74
	}
75
76
77
	private static function getAimeos()
78
	{
79
		if( !isset( self::$aimeos ) )
80
		{
81
			require_once 'Bootstrap.php';
82
			spl_autoload_register( 'Aimeos\\Bootstrap::autoload' );
83
84
			$extdir = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
85
			self::$aimeos = new \Aimeos\Bootstrap( array( $extdir ), true );
86
		}
87
88
		return self::$aimeos;
89
	}
90
91
92
	/**
93
	 * @param string $site
94
	 */
95
	private static function createContext( $site )
96
	{
97
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
98
		$aimeos = self::getAimeos();
99
100
101
		$paths = $aimeos->getConfigPaths();
102
		$paths[] = __DIR__ . DIRECTORY_SEPARATOR . 'config';
103
		$file = __DIR__ . DIRECTORY_SEPARATOR . 'confdoc.ser';
104
		$local = array( 'resource' => array( 'fs' => array( 'adapter' => 'Standard', 'basedir' => __DIR__ . '/tmp' ) ) );
105
106
		$conf = new \Aimeos\MW\Config\PHPArray( $local, $paths );
107
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
108
		$conf = new \Aimeos\MW\Config\Decorator\Documentor( $conf, $file );
109
		$ctx->setConfig( $conf );
110
111
112
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
113
		$ctx->setLogger( $logger );
114
115
116
		$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
117
		$ctx->setDatabaseManager( $dbm );
118
119
120
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $conf );
121
		$ctx->setFilesystemManager( $fs );
122
123
124
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $conf );
125
		$ctx->setMessageQueueManager( $mq );
126
127
128
		$cache = new \Aimeos\MW\Cache\None();
129
		$ctx->setCache( $cache );
130
131
132
		$i18n = new \Aimeos\MW\Translation\None( 'de' );
133
		$ctx->setI18n( array( 'de' => $i18n ) );
134
135
136
		$session = new \Aimeos\MW\Session\None();
137
		$ctx->setSession( $session );
138
139
140
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::create( $ctx );
141
		$locale = $localeManager->bootstrap( $site, '', '', false );
142
		$ctx->setLocale( $locale );
143
144
145
		$ctx->setEditor( 'ai-client-html:client/html' );
146
147
		return $ctx;
148
	}
149
}
150