Completed
Push — master ( ac66b3...ab3b6f )
by Aimeos
07:35 queued 01:23
created

TestHelper::createContext()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 79
Code Lines 41

Duplication

Lines 34
Ratio 43.04 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 34
loc 79
rs 8.8701
cc 3
eloc 41
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
/**
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
7
 * @copyright Aimeos (aimeos.org), 2015
8
 */
9
class TestHelper
10
{
11
	private static $aimeos;
12
	private static $context = array();
13
14
15
	public static function bootstrap()
16
	{
17
		$aimeos = self::getAimeos();
18
19
		$includepaths = $aimeos->getIncludePaths();
20
		$includepaths[] = get_include_path();
21
		set_include_path( implode( PATH_SEPARATOR, $includepaths ) );
22
	}
23
24
25
	public static function getContext( $site = 'unittest' )
26
	{
27
		if( !isset( self::$context[$site] ) ) {
28
			self::$context[$site] = self::createContext( $site );
29
		}
30
31
		return clone self::$context[$site];
32
	}
33
34
35
	private static function getAimeos()
36
	{
37
		if( !isset( self::$aimeos ) )
38
		{
39
			require_once 'Bootstrap.php';
40
			spl_autoload_register( 'Aimeos\\Bootstrap::autoload' );
41
42
			$extdir = dirname( dirname( dirname( __DIR__ ) ) );
43
			self::$aimeos = new \Aimeos\Bootstrap( array( $extdir ), true );
44
		}
45
46
		return self::$aimeos;
47
	}
48
49
50
	/**
51
	 * @param string $site
52
	 */
53
	private static function createContext( $site )
54
	{
55
		$ctx = new \Aimeos\MShop\Context\Item\Ezpublish();
56
		$aimeos = self::getAimeos();
57
58
59
		$paths = $aimeos->getConfigPaths();
60
		$paths[] = __DIR__ . DIRECTORY_SEPARATOR . 'config';
61
62
		$conf = new \Aimeos\MW\Config\PHPArray( array(), $paths );
63
		$ctx->setConfig( $conf );
64
65
66
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $conf );
67
		$ctx->setDatabaseManager( $dbm );
68
69
70
		$logger = new \Aimeos\MW\Logger\File( $site . '.log', \Aimeos\MW\Logger\Base::DEBUG );
71
		$ctx->setLogger( $logger );
72
73
74
		$cache = new \Aimeos\MW\Cache\None();
75
		$ctx->setCache( $cache );
76
77
78
		$i18n = new \Aimeos\MW\Translation\None( 'de' );
79
		$ctx->setI18n( array( 'de' => $i18n ) );
80
81
82
		$session = new \Aimeos\MW\Session\None();
83
		$ctx->setSession( $session );
84
85
86
		$localeManager = \Aimeos\MShop\Locale\Manager\Factory::createManager( $ctx );
87
		$localeItem = $localeManager->bootstrap( $site, '', '', false );
88
89
		$ctx->setLocale( $localeItem );
90
91
		$ctx->setEditor( 'ai-ezpublish:unittest' );
92
93
94 View Code Duplication
		$ctx->setEzUser( function( $code, $email, $password ) use ( $ctx ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
96
			$id = mt_rand( -0x7fffffff,-1 );
97
			$dbm = $ctx->getDatabaseManager();
98
			$dbconf = $ctx->getConfig()->get( 'resource/db-customer' );
99
			$dbname = ( $dbconf ? 'db-customer' : 'db' );
100
			$conn = $dbm->acquire( $dbname );
101
102
			try
103
			{
104
				$stmt = $conn->create( 'INSERT INTO "ezuser" (
105
					"contentobject_id", "email", "login", "login_normalized", "password_hash", "password_hash_type"
106
				) VALUES (
107
					?, ?, ?, ?, ?, 5
108
				)' );
109
110
				$stmt->bind( 1, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
111
				$stmt->bind( 2, $email );
112
				$stmt->bind( 3, $code );
113
				$stmt->bind( 4, $code );
114
				$stmt->bind( 5, $password );
115
116
				$stmt->execute()->finish();
117
118
				$dbm->release( $conn, $dbname );
119
			}
120
			catch( \Exception $e )
121
			{
122
				$dbm->release( $conn, $dbname );
123
				throw $e;
124
			}
125
126
			return $id;
127
		} );
128
129
130
		return $ctx;
131
	}
132
}
133