Passed
Push — master ( 5c9b02...302359 )
by Aimeos
26:48 queued 19:40
created

TestHelperJadm::createContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 24
nc 1
nop 1
dl 0
loc 44
rs 9.536
c 1
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
8
9
class TestHelperJadm
10
{
11
	private static $aimeos;
12
	private static $context;
13
14
15
	public static function bootstrap()
16
	{
17
		self::getAimeos();
18
	}
19
20
21
	public static function context( $site = 'unittest' )
22
	{
23
		if( !isset( self::$context[$site] ) ) {
24
			self::$context[$site] = self::createContext( $site );
25
		}
26
27
		return clone self::$context[$site];
28
	}
29
30
31
	public static function getAimeos()
32
	{
33
		if( !isset( self::$aimeos ) )
34
		{
35
			require_once 'Bootstrap.php';
36
			spl_autoload_register( 'Aimeos\\Bootstrap::autoload' );
37
38
			$extdir = dirname( dirname( dirname( dirname( __FILE__ ) ) ) );
39
			self::$aimeos = new \Aimeos\Bootstrap( array( $extdir ), false );
40
		}
41
42
		return self::$aimeos;
43
	}
44
45
46
	public static function getJsonadmPaths()
47
	{
48
		return self::getAimeos()->getTemplatePaths( 'admin/jsonadm/templates' );
49
	}
50
51
52
	private static function createContext( $site )
53
	{
54
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
55
		$aimeos = self::getAimeos();
56
57
58
		$paths = $aimeos->getConfigPaths();
59
		$paths[] = __DIR__ . DIRECTORY_SEPARATOR . 'config';
60
		$file = __DIR__ . DIRECTORY_SEPARATOR . 'confdoc.ser';
61
62
		$conf = new \Aimeos\MW\Config\PHPArray( [], $paths );
63
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
64
		$conf = new \Aimeos\MW\Config\Decorator\Documentor( $conf, $file );
65
		$ctx->setConfig( $conf );
66
67
68
		$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
69
		$ctx->setDatabaseManager( $dbm );
70
71
72
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
73
		$ctx->setLogger( $logger );
74
75
76
		$session = new \Aimeos\MW\Session\None();
77
		$ctx->setSession( $session );
78
79
80
		$i18n = new \Aimeos\MW\Translation\None( 'de' );
81
		$ctx->setI18n( array( 'de' => $i18n ) );
82
83
84
		$localeManager = \Aimeos\MShop::create( $ctx, 'locale' );
85
		$locale = $localeManager->bootstrap( $site, 'de', '', false );
86
		$ctx->setLocale( $locale );
87
88
89
		$view = self::createView( $conf );
90
		$ctx->setView( $view );
91
92
93
		$ctx->setEditor( 'ai-admin-jsonadm:admin/jsonadm' );
94
95
		return $ctx;
96
	}
97
98
99
	protected static function createView( \Aimeos\MW\Config\Iface $config )
100
	{
101
		$tmplpaths = self::getAimeos()->getTemplatePaths( 'admin/jsonadm/templates' );
102
103
		$view = new \Aimeos\MW\View\Standard( $tmplpaths );
104
105
		$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
106
		$view->addHelper( 'access', $helper );
107
108
		$trans = new \Aimeos\MW\Translation\None( 'de_DE' );
109
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $trans );
110
		$view->addHelper( 'translate', $helper );
111
112
		$helper = new \Aimeos\MW\View\Helper\Url\Standard( $view, 'http://baseurl' );
113
		$view->addHelper( 'url', $helper );
114
115
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, '.', '' );
116
		$view->addHelper( 'number', $helper );
117
118
		$helper = new \Aimeos\MW\View\Helper\Date\Standard( $view, 'Y-m-d' );
119
		$view->addHelper( 'date', $helper );
120
121
		$config = new \Aimeos\MW\Config\Decorator\Protect( $config, array( 'admin/jsonadm' ) );
122
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
123
		$view->addHelper( 'config', $helper );
124
125
		$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
126
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $view, $psr17Factory->createServerRequest( 'GET', 'https://aimeos.org' ) );
127
		$view->addHelper( 'request', $helper );
128
129
		$helper = new \Aimeos\MW\View\Helper\Response\Standard( $view, $psr17Factory->createResponse() );
130
		$view->addHelper( 'response', $helper );
131
132
		return $view;
133
	}
134
}
135