Completed
Push — master ( 176f72...323c2d )
by Aimeos
08:35
created

TestHelperMShop::createView()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 1
dl 0
loc 28
rs 8.8571
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-2016
7
 */
8
9
10
class TestHelperMShop
11
{
12
	private static $aimeos;
13
	private static $context = [];
14
15
16
	/**
17
	 * Initializes the environment
18
	 */
19
	public static function bootstrap()
20
	{
21
		self::getAimeos();
22
		\Aimeos\MShop\Factory::setCache( false );
23
	}
24
25
26
	/**
27
	 * Returns the context object
28
	 *
29
	 * @param string $site Site code
30
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
31
	 */
32
	public static function getContext( $site = 'unittest' )
33
	{
34
		if( !isset( self::$context[$site] ) ) {
35
			self::$context[$site] = self::createContext( $site );
36
		}
37
38
		return clone self::$context[$site];
39
	}
40
41
42
	/**
43
	 * Returns the Aimeos bootstrap object
44
	 *
45
	 * @return \Aimeos\Bootstrap Aimeos bootstrap object
46
	 */
47
	private static function getAimeos()
48
	{
49
		if( !isset( self::$aimeos ) )
50
		{
51
			require_once dirname( dirname( dirname( __DIR__ ) ) ) . DIRECTORY_SEPARATOR . 'Bootstrap.php';
52
53
			self::$aimeos = new \Aimeos\Bootstrap( [], false );
54
		}
55
56
		return self::$aimeos;
57
	}
58
59
60
	/**
61
	 * Creates a new context object
62
	 *
63
	 * @param string $site Site code
64
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
65
	 */
66
	private static function createContext( $site )
67
	{
68
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
69
		$aimeos = self::getAimeos();
70
71
72
		$paths = $aimeos->getConfigPaths();
73
		$paths[] = __DIR__ . DIRECTORY_SEPARATOR . 'config';
74
		$file = __DIR__ . DIRECTORY_SEPARATOR . 'confdoc.ser';
75
76
		$conf = new \Aimeos\MW\Config\PHPArray( [], $paths );
77
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
78
		$conf = new \Aimeos\MW\Config\Decorator\Documentor( $conf, $file );
79
		$ctx->setConfig( $conf );
80
81
82
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
83
		$ctx->setLogger( $logger );
84
85
86
		$dbm = new \Aimeos\MW\DB\Manager\PDO( $conf );
87
		$ctx->setDatabaseManager( $dbm );
88
89
90
		$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $conf );
91
		$ctx->setFilesystemManager( $fs );
92
93
94
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $conf );
95
		$ctx->setMessageQueueManager( $mq );
96
97
98
		$cache = new \Aimeos\MW\Cache\None();
99
		$ctx->setCache( $cache );
100
101
102
		$i18n = new \Aimeos\MW\Translation\None( 'de' );
103
		$ctx->setI18n( array( 'de' => $i18n ) );
104
105
106
		$session = new \Aimeos\MW\Session\None();
107
		$ctx->setSession( $session );
108
109
110
		$mail = new \Aimeos\MW\Mail\None();
111
		$ctx->setMail( $mail );
112
113
114
		$view = self::createView( $conf );
115
		$ctx->setView( $view );
116
117
118
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $ctx );
119
		$locale = $localeManager->bootstrap( $site, 'de', '', false );
120
		$ctx->setLocale( $locale );
121
122
123
		$ctx->setEditor( 'core:unittest' );
124
125
		return $ctx;
126
	}
127
128
129
	/**
130
	 * Creates a new view object
131
	 *
132
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
133
	 * @return \Aimeos\MW\View\Iface View object
134
	 */
135
	protected static function createView( \Aimeos\MW\Config\Iface $config )
136
	{
137
		$tmplpaths = array_merge_recursive(
138
			self::getAimeos()->getCustomPaths( 'client/html/templates' ),
139
			self::getAimeos()->getCustomPaths( 'controller/jobs/templates' )
140
		);
141
142
		$view = new \Aimeos\MW\View\Standard( $tmplpaths );
143
144
		$trans = new \Aimeos\MW\Translation\None( 'de_DE' );
145
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $trans );
146
		$view->addHelper( 'translate', $helper );
147
148
		$helper = new \Aimeos\MW\View\Helper\Url\Standard( $view, 'http://baseurl' );
149
		$view->addHelper( 'url', $helper );
150
151
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, '.', '' );
152
		$view->addHelper( 'number', $helper );
153
154
		$helper = new \Aimeos\MW\View\Helper\Date\Standard( $view, 'Y-m-d' );
155
		$view->addHelper( 'date', $helper );
156
157
		$config = new \Aimeos\MW\Config\Decorator\Protect( $config, array( 'controller/jobs', 'client/html' ) );
158
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
159
		$view->addHelper( 'config', $helper );
160
161
		return $view;
162
	}
163
}
164