Passed
Push — master ( 6a01fa...835962 )
by Aimeos
27:25 queued 23:33
created

TestHelperHtml::getContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
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 context( $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::context( $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
		$paths = ['client', 'common', 'resource/fs/baseurl', 'resource/fs-media/baseurl'];
54
		$config = new \Aimeos\MW\Config\Decorator\Protect( $config, $paths );
55
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
56
		$view->addHelper( 'config', $helper );
57
58
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_csrf_token', '_csrf_value' );
59
		$view->addHelper( 'csrf', $helper );
60
61
		$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
62
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $view, $psr17Factory->createServerRequest( 'POST', 'https://aimeos.org' ) );
63
		$view->addHelper( 'request', $helper );
64
65
		$helper = new \Aimeos\MW\View\Helper\Response\Standard( $view, $psr17Factory->createResponse() );
66
		$view->addHelper( 'response', $helper );
67
68
		return $view;
69
	}
70
71
72
	public static function getHtmlTemplatePaths()
73
	{
74
		return self::getAimeos()->getTemplatePaths( 'client/html/templates' );
75
	}
76
77
78
	private static function getAimeos()
79
	{
80
		if( !isset( self::$aimeos ) )
81
		{
82
			require_once 'Bootstrap.php';
83
			spl_autoload_register( 'Aimeos\\Bootstrap::autoload' );
84
85
			$extdir = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
86
			self::$aimeos = new \Aimeos\Bootstrap( array( $extdir ), true );
87
		}
88
89
		return self::$aimeos;
90
	}
91
92
93
	/**
94
	 * @param string $site
95
	 */
96
	private static function createContext( $site )
97
	{
98
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
99
		$aimeos = self::getAimeos();
100
101
102
		$paths = $aimeos->getConfigPaths();
103
		$paths[] = __DIR__ . DIRECTORY_SEPARATOR . 'config';
104
		$file = __DIR__ . DIRECTORY_SEPARATOR . 'confdoc.ser';
105
		$local = array( 'resource' => array( 'fs' => array( 'adapter' => 'Standard', 'basedir' => __DIR__ . '/tmp' ) ) );
106
107
		$conf = new \Aimeos\MW\Config\PHPArray( $local, $paths );
108
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
109
		$conf = new \Aimeos\MW\Config\Decorator\Documentor( $conf, $file );
110
		$ctx->setConfig( $conf );
111
112
113
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
114
		$ctx->setLogger( $logger );
115
116
117
		$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
118
		$ctx->setDatabaseManager( $dbm );
119
120
121
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $conf );
122
		$ctx->setFilesystemManager( $fs );
123
124
125
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $conf );
126
		$ctx->setMessageQueueManager( $mq );
127
128
129
		$cache = new \Aimeos\MW\Cache\None();
130
		$ctx->setCache( $cache );
131
132
133
		$i18n = new \Aimeos\MW\Translation\None( 'de' );
134
		$ctx->setI18n( array( 'de' => $i18n ) );
135
136
137
		$session = new \Aimeos\MW\Session\None();
138
		$ctx->setSession( $session );
139
140
141
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::create( $ctx );
142
		$locale = $localeManager->bootstrap( $site, '', '', false );
143
		$ctx->setLocale( $locale );
144
145
146
		$ctx->setEditor( 'ai-client-html:client/html' );
147
148
		return $ctx;
149
	}
150
}
151