Passed
Pull Request — master (#195)
by Jonathan
03:35 queued 24s
created

Lib_Test_TestAbstract::run()   B

Complexity

Conditions 5
Paths 30

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 56
rs 8.9688
cc 5
nc 30
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
 * TODO Add logging of original xml's (request and response)
4
 * 
5
 * result file name format: TEST_NAME _ WSDL_NAME _ PHP_VERSION
6
 */
7
abstract class Lib_Test_TestAbstract
8
{
9
	protected $_wsdl;
10
11
	protected $_userName = '[email protected]';
12
	//protected $_password = 'qwqDbM3q'+token;
13
	protected $_password = 'qwqDbM3qD26eh0xIZhFmf7poWeYcTvrHq';
14
	protected $_token = 'D26eh0xIZhFmf7poWeYcTvrHq';
15
16
	protected $_portalId = NULL;
17
18
	protected $_mySforceConnection;
19
	protected $_mylogin;
20
	protected $_mySoapClient;
21
	
22
	protected $_logger;
23
	protected $_soapDir;
24
	protected $_phpVersion;
25
	
26
	protected $_medaDataWsdlName = 'metadata';
27
	
28
	public function __construct($soapDir)
29
	{
30
		$this->_soapDir = $soapDir;
31
		$this->_logger = new Lib_Utils_FileLogger(
32
							$this->getLogName(),
33
							'w');
34
		
35
		$this->_phpVersion = phpversion();
36
		header('Content-Type: text/plain');
37
	}
38
	
39
	/**
40
	 * @param Lib_Utils_FileLogger $metaInfoLogger
41
	 * @return void
42
	 */
43
	public function run(Lib_Utils_FileLogger $metaInfoLogger)
44
	{
45
		try {
46
			ob_start();
47
			$start_time = microtime(TRUE);
48
			
49
			try {
50
				$this->_run();
51
			} catch (SoapFault $fault) {
52
				$end_time = microtime(TRUE);
53
				// some time SoapFaul is sign of positive test result
54
				$resultStr = ob_get_clean();
55
				$this->_logger->write($resultStr);
56
				print '<pre>' . $resultStr . '</pre>';
57
				
58
				$this->_logger->write($fault->faultstring);
59
				print '<pre>SoapFault:</pre>';
60
				print '<pre>' . $fault->faultstring . '</pre>';
61
				
62
				try {
63
//					$this->_validateSoapFault($resultStr . $fault->faultstring);
64
					$this->_validateSoapFault($fault->faultstring);
65
					$metaInfoLogger->write(basename($this->getLogName()) . ' - test is ok!');
66
				} catch (Lib_Exception_InvalidResponse $e) {
67
					$metaInfoLogger->write(basename($this->getLogName()) . ' - test failed!');
68
				}
69
70
				return;
71
			}	
72
			
73
			$end_time = microtime(TRUE);
74
			$resultStr = ob_get_clean();
75
			$this->_logger->write($resultStr);
76
			print $resultStr;
77
			
78
			try {
79
				$this->_validate($resultStr);
80
				$metaInfoLogger->write(basename($this->getLogName()) . ' - test is ok!');
81
			} catch (Lib_Exception_InvalidResponse $e) {
82
				$metaInfoLogger->write(basename($this->getLogName()) . ' - test failed!');
83
			}
84
			
85
			$metaInfoLogger->write('Total time: '. ($end_time - $start_time));
86
			$metaInfoLogger->write('Max allocated memory: '. memory_get_peak_usage(TRUE));			
87
		} catch (Exception $e) {
88
			$this->_logger->write($this->_mySforceConnection->getLastRequest());
89
			ob_start();
90
			print_r($e);
91
			$eStr = ob_get_clean();
92
			$this->_logger->write($eStr);
93
			
94
			print $this->_mySforceConnection->getLastRequest();
95
			echo "\n\n\n";
96
			print $eStr;
97
			
98
			$metaInfoLogger->write('Test failed!');
99
		}
100
	}
101
	
102
	public function getLogName()
103
	{
104
		return $this->_soapDir . '/results/' . $this->getTestName() . '_' . $this->getWSDLName() . '_' . 
105
					$this->getWSDLVersion()	. '_' . phpversion();
106
	}
107
	
108
	protected function _getMetaDataWSDL()
109
	{
110
		return $this->_soapDir . '/' . $this->_medaDataWsdlName . '.wsdl.xml';
111
	}
112
	
113
	/**
114
	 * @return void
115
	 * @throws SoapFault
116
	 */
117
	abstract protected function _run();
118
	
119
	/**
120
	 * NOTE: implement in sub-classes.
121
	 * 
122
	 * @param string $rs
123
	 * @return void
124
	 * @throws Lib_Exception_InvalidResponse
125
	 */
126
	protected function _validate($rs)
127
	{
128
		// generic check
129
		if(strpos($rs, 'errors') !== FALSE) {
130
			throw new Lib_Exception_InvalidResponse();
131
		}
132
	}
133
	
134
	/**
135
	 * NOTE: implement in sub-classes.
136
	 * 
137
	 * @param string $rs
138
	 * @return void
139
	 * @throws Lib_Exception_InvalidResponse
140
	 */
141
	protected function _validateSoapFault($rs)
142
	{
143
		throw new Lib_Exception_InvalidResponse();
144
	}
145
146
	/**
147
	 * 
148
	 * @return string location of local wsdl file
149
	 */
150
	abstract public function getWSDL();
151
152
	abstract public function getWSDLName();
153
	
154
	abstract public function getWSDLVersion();
155
156
	abstract public function getTestName();
157
}
158