Passed
Push — master ( 4aa199...91fbcb )
by Aimeos
37:45 queued 28:45
created

TestHelperCustom::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 Aimeos (aimeos.org), 2015-2021
6
 */
7
class TestHelperCustom
8
{
9
	private static $aimeos;
10
	private static $context = [];
11
12
13
	public static function bootstrap()
14
	{
15
		self::getAimeos();
16
		\Aimeos\MShop::cache( false );
17
	}
18
19
20
	public static function context( $site = 'unittest' )
21
	{
22
		if( !isset( self::$context[$site] ) ) {
23
			self::$context[$site] = self::createContext( $site );
24
		}
25
26
		return clone self::$context[$site];
27
	}
28
29
30
	public static function view( $site = 'unittest', \Aimeos\MW\Config\Iface $config = null )
31
	{
32
		if( $config === null ) {
33
			$config = self::context( $site )->getConfig();
34
		}
35
36
		$view = new \Aimeos\MW\View\Standard( self::getTemplatePaths() );
37
38
		$param = ['site' => 'unittest'];
39
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $param );
40
		$view->addHelper( 'param', $helper );
41
42
		$trans = new \Aimeos\MW\Translation\None( 'de_DE' );
43
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $trans );
44
		$view->addHelper( 'translate', $helper );
45
46
		$helper = new \Aimeos\MW\View\Helper\Url\Standard( $view, 'http://baseurl' );
47
		$view->addHelper( 'url', $helper );
48
49
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, '.', '' );
50
		$view->addHelper( 'number', $helper );
51
52
		$helper = new \Aimeos\MW\View\Helper\Date\Standard( $view, 'Y-m-d' );
53
		$view->addHelper( 'date', $helper );
54
55
		$config = new \Aimeos\MW\Config\Decorator\Protect( $config, array( 'admin', 'client/html', 'controller/jsonadm' ) );
56
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
57
		$view->addHelper( 'config', $helper );
58
59
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_csrf_token', '_csrf_value' );
60
		$view->addHelper( 'csrf', $helper );
61
62
		$fcn = function() { return array( 'admin' ); };
63
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
64
		$view->addHelper( 'access', $helper );
65
66
		return $view;
67
	}
68
69
70
	public static function getTemplatePaths()
71
	{
72
		return self::getAimeos()->getTemplatePaths( 'admin/jqadm/templates' );
73
	}
74
75
76
	private static function getAimeos()
77
	{
78
		if( !isset( self::$aimeos ) )
79
		{
80
			require_once 'Bootstrap.php';
81
			spl_autoload_register( 'Aimeos\\Bootstrap::autoload' );
82
83
			$extdir = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
84
			self::$aimeos = new \Aimeos\Bootstrap( array( $extdir ), false );
85
		}
86
87
		return self::$aimeos;
88
	}
89
90
91
	/**
92
	 * @param string $site
93
	 */
94
	private static function createContext( $site )
95
	{
96
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
97
		$aimeos = self::getAimeos();
98
99
100
		$paths = $aimeos->getConfigPaths();
101
		$paths[] = __DIR__ . DIRECTORY_SEPARATOR . 'config';
102
		$file = __DIR__ . DIRECTORY_SEPARATOR . 'confdoc.ser';
103
		$local = array( 'resource' => array( 'fs' => array( 'adapter' => 'Standard', 'basedir' => __DIR__ . '/tmp' ) ) );
104
105
		$conf = new \Aimeos\MW\Config\PHPArray( $local, $paths );
106
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
107
		$conf = new \Aimeos\MW\Config\Decorator\Documentor( $conf, $file );
108
		$ctx->setConfig( $conf );
109
110
111
		$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
112
		$ctx->setDatabaseManager( $dbm );
113
114
115
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $conf );
116
		$ctx->setFilesystemManager( $fs );
117
118
119
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
120
		$ctx->setLogger( $logger );
121
122
123
		$cache = new \Aimeos\MW\Cache\None();
124
		$ctx->setCache( $cache );
125
126
127
		$i18n = new \Aimeos\MW\Translation\None( 'de' );
128
		$ctx->setI18n( array( 'de' => $i18n ) );
129
130
131
		$session = new \Aimeos\MW\Session\None();
132
		$ctx->setSession( $session );
133
134
135
		$localeManager = \Aimeos\MShop::create( $ctx, 'locale' );
136
		$locale = $localeManager->bootstrap( $site, '', '', false );
137
		$ctx->setLocale( $locale );
138
139
140
		$ctx->setEditor( 'ai-admin-jqadm:lib/custom' );
141
142
		return $ctx;
143
	}
144
}
145