Completed
Branch master (7caa83)
by Aimeos
03:01
created

TestHelperJapi::createContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 9.1127
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2017-2018
6
 */
7
8
9
class TestHelperJapi
10
{
11
	private static $aimeos;
12
	private static $context = [];
13
14
15
	public static function bootstrap()
16
	{
17
		$aimeos = self::getAimeos();
0 ignored issues
show
Unused Code introduced by
The assignment to $aimeos is dead and can be removed.
Loading history...
18
		\Aimeos\MShop::cache( false );
19
		\Aimeos\Controller\Frontend::cache( false );
20
	}
21
22
23
	public static function getContext( $site = 'unittest' )
24
	{
25
		if( !isset( self::$context[$site] ) ) {
26
			self::$context[$site] = self::createContext( $site );
27
			self::$context[$site]->setView( self::getView( self::$context[$site]->getConfig() ) );
28
		}
29
30
		return clone self::$context[$site];
31
	}
32
33
34
	public static function getView( \Aimeos\MW\Config\Iface $config )
35
	{
36
		$view = new \Aimeos\MW\View\Standard( self::getTemplatePaths() );
37
38
		$trans = new \Aimeos\MW\Translation\None( 'en' );
39
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $trans );
40
		$view->addHelper( 'translate', $helper );
41
42
		$helper = new \Aimeos\MW\View\Helper\Url\Standard( $view, 'baseurl' );
43
		$view->addHelper( 'url', $helper );
44
45
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, '.', '' );
46
		$view->addHelper( 'number', $helper );
47
48
		$helper = new \Aimeos\MW\View\Helper\Date\Standard( $view, 'Y-m-d' );
49
		$view->addHelper( 'date', $helper );
50
51
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
52
		$view->addHelper( 'config', $helper );
53
54
		$helper = new \Aimeos\MW\View\Helper\Request\Standard( $view, new \Zend\Diactoros\ServerRequest() );
0 ignored issues
show
Bug introduced by
The type Zend\Diactoros\ServerRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
55
		$view->addHelper( 'request', $helper );
56
57
		$helper = new \Aimeos\MW\View\Helper\Response\Standard( $view, new \Zend\Diactoros\Response() );
0 ignored issues
show
Bug introduced by
The type Zend\Diactoros\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
58
		$view->addHelper( 'response', $helper );
59
60
		return $view;
61
	}
62
63
64
	public static function getTemplatePaths()
65
	{
66
		return self::getAimeos()->getCustomPaths( 'client/jsonapi/templates' );
67
	}
68
69
70
	private static function getAimeos()
71
	{
72
		if( !isset( self::$aimeos ) )
73
		{
74
			require_once 'Bootstrap.php';
75
			spl_autoload_register( 'Aimeos\\Bootstrap::autoload' );
76
77
			$extdir = dirname( dirname( dirname( dirname( __DIR__ ) ) ) );
78
			self::$aimeos = new \Aimeos\Bootstrap( array( $extdir ), true );
79
		}
80
81
		return self::$aimeos;
82
	}
83
84
85
	private static function createContext( $site )
86
	{
87
		$ctx = new \Aimeos\MShop\Context\Item\Standard();
88
		$aimeos = self::getAimeos();
89
90
91
		$paths = $aimeos->getConfigPaths();
92
		$paths[] = __DIR__ . DIRECTORY_SEPARATOR . 'config';
93
		$file = __DIR__ . DIRECTORY_SEPARATOR . 'confdoc.ser';
94
95
		$conf = new \Aimeos\MW\Config\PHPArray( [], $paths );
96
		$conf = new \Aimeos\MW\Config\Decorator\Memory( $conf );
97
		$conf = new \Aimeos\MW\Config\Decorator\Documentor( $conf, $file );
98
		$ctx->setConfig( $conf );
99
100
101
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $conf );
102
		$ctx->setDatabaseManager( $dbm );
103
104
105
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $conf );
106
		$ctx->setMessageQueueManager( $mq );
107
108
109
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
110
		$ctx->setLogger( $logger );
111
112
113
		$cache = new \Aimeos\MW\Cache\None();
114
		$ctx->setCache( $cache );
115
116
117
		$i18n = new \Aimeos\MW\Translation\None( 'en' );
118
		$ctx->setI18n( array( 'en' => $i18n ) );
119
120
121
		$session = new \Aimeos\MW\Session\None();
122
		$ctx->setSession( $session );
123
124
125
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::create( $ctx );
126
		$locale = $localeManager->bootstrap( $site, '', '', false );
127
		$ctx->setLocale( $locale );
128
129
130
		$ctx->setEditor( 'ai-client-jsonapi:unittest' );
131
132
		return $ctx;
133
	}
134
}