Passed
Push — master ( a02dd5...19f9f2 )
by Aimeos
29:15 queued 26:42
created

TestHelperCustom::context()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2021
6
 */
7
8
9
class TestHelperCustom
10
{
11
	private static $aimeos;
12
	private static $context = [];
13
14
15
	public static function bootstrap()
16
	{
17
		self::getAimeos();
18
		\Aimeos\MShop::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::getTemplatePaths() );
39
40
		$param = ['site' => 'unittest'];
41
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
42
		$view->addHelper( 'param', $helper );
43
44
		$trans = new \Aimeos\MW\Translation\None( 'de_DE' );
45
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $trans );
46
		$view->addHelper( 'translate', $helper );
47
48
		$helper = new \Aimeos\MW\View\Helper\Url\Standard( $view, 'http://baseurl' );
49
		$view->addHelper( 'url', $helper );
50
51
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, '.', '' );
52
		$view->addHelper( 'number', $helper );
53
54
		$helper = new \Aimeos\MW\View\Helper\Date\Standard( $view, 'Y-m-d' );
55
		$view->addHelper( 'date', $helper );
56
57
		$config = new \Aimeos\MW\Config\Decorator\Protect( $config, array( 'admin', 'client/html', 'controller/jsonadm' ) );
58
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
59
		$view->addHelper( 'config', $helper );
60
61
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_csrf_token', '_csrf_value' );
62
		$view->addHelper( 'csrf', $helper );
63
64
		$fcn = function() { return array( 'admin' ); };
65
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
66
		$view->addHelper( 'access', $helper );
67
68
		return $view;
69
	}
70
71
72
	public static function getTemplatePaths()
73
	{
74
		return self::getAimeos()->getTemplatePaths( 'admin/jqadm/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( __FILE__ ) ) ) );
86
			self::$aimeos = new \Aimeos\Bootstrap( array( $extdir ), false );
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
		$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
114
		$ctx->setDatabaseManager( $dbm );
115
116
117
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $conf );
118
		$ctx->setFilesystemManager( $fs );
119
120
121
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
122
		$ctx->setLogger( $logger );
123
124
125
		$cache = new \Aimeos\MW\Cache\None();
126
		$ctx->setCache( $cache );
127
128
129
		$i18n = new \Aimeos\MW\Translation\None( 'de' );
130
		$ctx->setI18n( array( 'de' => $i18n ) );
131
132
133
		$session = new \Aimeos\MW\Session\None();
134
		$ctx->setSession( $session );
135
136
137
		$localeManager = \Aimeos\MShop::create( $ctx, 'locale' );
138
		$locale = $localeManager->bootstrap( $site, '', '', false );
139
		$ctx->setLocale( $locale );
140
141
142
		$ctx->setEditor( 'ai-client-jsonapi:lib/custom' );
143
144
		return $ctx;
145
	}
146
}
147