Skip to content

Commit f93968d

Browse files
Dropping support for PhantomJS
1 parent 4c6b037 commit f93968d

7 files changed

Lines changed: 149 additions & 94 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace DPRMC\WebBot\Exceptions\WebBot;
4+
5+
class InvalidClientInConstructor extends \Exception {
6+
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace DPRMC\WebBot\Exceptions\WebBot;
4+
5+
class ResponseObjectNotSetForIndex extends \Exception {
6+
7+
}

src/FailureRule.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ class FailureRule {
1010
/**
1111
* A list of constants that represent the different types of failure rules that you can apply.
1212
*/
13-
const regex = 'regex';
14-
15-
const notFound = 'notFound';
13+
const regex = 'regex';
14+
const responseCodeNot = 'responseCodeNot'; // Typically 200 is passed in to this failure rule.
15+
const notFound = 'notFound';
1616

1717
/**
1818
* An array defining all of the failure rule types that I can process.
1919
*/
2020
const types = [
2121
self::regex,
22+
self::responseCodeNot,
2223
self::notFound,
2324
];
2425

@@ -34,6 +35,10 @@ class FailureRule {
3435
*/
3536
protected $parameters;
3637

38+
public static function instance() {
39+
return new static;
40+
}
41+
3742
public function __construct() {
3843
}
3944

@@ -80,6 +85,9 @@ public function run( $response, $breaksOnFailure ) {
8085
case self::regex:
8186
$result = $this->runFailureRuleRegEx( $this->parameters, $response->getBody() );
8287
break;
88+
case self::responseCodeNot:
89+
$result = $this->runFailureRuleNotResponseCode( 200, $response->getStatusCode() );
90+
break;
8391
default:
8492
throw new UndefinedFailureRuleType( "You attempted to run a failure rule type of [" . $this->type . "]" );
8593
endswitch;
@@ -95,7 +103,7 @@ public function run( $response, $breaksOnFailure ) {
95103
* @param string $pattern The regex pattern.
96104
* @param string $string
97105
*
98-
* @return bool
106+
* @return bool True means failure
99107
*/
100108
protected function runFailureRuleRegEx( $pattern, $string ) {
101109
if ( preg_match( '/' . $pattern . '/', $string ) === 1 ):
@@ -104,4 +112,20 @@ protected function runFailureRuleRegEx( $pattern, $string ) {
104112

105113
return false;
106114
}
115+
116+
/**
117+
* Pass in the code that would indicate success. Any other code indicates a failure.
118+
*
119+
* @param integer $codeThatMeansSuccess Typically 200
120+
* @param $actual
121+
*
122+
* @return bool
123+
*/
124+
protected function runFailureRuleNotResponseCode( $codeThatMeansSuccess, $actual ) {
125+
if ( $codeThatMeansSuccess == $actual ):
126+
return true;
127+
endif;
128+
129+
return false;
130+
}
107131
}

src/Step.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use GuzzleHttp\Psr7\Request;
77

88
class Step {
9+
protected $debug = false;
10+
911
protected $method;
1012
protected $url;
1113
protected $headers;
@@ -17,11 +19,14 @@ class Step {
1719
protected $successRules;
1820
protected $failureRules;
1921

20-
2122
protected $breakOnFailure = false;
2223
protected $breakOnSuccess = false;
2324

2425

26+
public static function instance() {
27+
return new static;
28+
}
29+
2530
public function __construct() {
2631
$this->headers = [];
2732
$this->formParams = [];
@@ -65,31 +70,33 @@ public function setBreakOnSuccess( $breakOnSuccess ) {
6570
return $this;
6671
}
6772

73+
6874
/**
6975
* @link http://docs.guzzlephp.org/en/stable/request-options.html#verify Explains the "verify" option set to false.
7076
*
71-
* @param \GuzzleHttp\Client $client
77+
* @param $client
7278
*
73-
* @return \DPRMC\WebBot\StepResult
79+
* @return bool|\DPRMC\WebBot\ContinueToNextStep|\DPRMC\WebBot\Failure|\DPRMC\WebBot\Success
7480
*/
7581
public function run( &$client ) {
7682
/**
7783
* @var \GuzzleHttp\Psr7\Response $response
7884
*/
7985
$response = $client->send( $this->getRequestObject(), [ 'form_params' => $this->formParams,
80-
'allow_redirects' => true,
81-
'debug' => false,
86+
'allow_redirects' => false,
87+
'debug' => $this->debug,
8288
'timeout' => $this->timeout,
8389
'sink' => null,
8490
'verify' => false,
8591
] );
8692

93+
8794
/**
8895
*
8996
*/
9097
foreach ( $this->successRules as $successRule ):
9198
/**
92-
* @var \DPRMC\WebBot\FailureRule $successRule
99+
* @var \DPRMC\WebBot\SuccessRule $successRule
93100
*/
94101
$stepResult = $successRule->run( $response, $this->breaksOnSuccess() );
95102
if ( false !== $stepResult ):
@@ -183,7 +190,6 @@ public function addFormParam( $name, $value ) {
183190
*/
184191
public function addQueryParam( $name, $value ) {
185192
$this->queryParams[ $name ] = $value;
186-
187193
return $this;
188194
}
189195

@@ -196,9 +202,7 @@ public function addQueryParam( $name, $value ) {
196202
* @return $this
197203
*/
198204
public function addFailureRule( $name, $failureRule ) {
199-
200205
$this->failureRules[ $name ] = $failureRule;
201-
202206
return $this;
203207
}
204208

@@ -209,12 +213,15 @@ public function addFailureRule( $name, $failureRule ) {
209213
* @return $this
210214
*/
211215
public function addSuccessRule( $name, $successRule ) {
212-
213216
$this->successRules[ $name ] = $successRule;
214-
215217
return $this;
216218
}
217219

220+
public function setDebug( $debug ) {
221+
$this->debug = (bool)$debug;
222+
223+
return $this;
224+
}
218225

219226

220227
}

src/SuccessRule.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class SuccessRule {
99
/**
1010
* A list of constants that represent the different types of success rules that you can apply.
1111
*/
12-
const regex = 'regex';
12+
const regex = 'regex';
13+
const responseCode = 'responseCode';
1314

1415
const notFound = 'notFound';
1516

@@ -18,6 +19,7 @@ class SuccessRule {
1819
*/
1920
const types = [
2021
self::regex,
22+
self::responseCode,
2123
self::notFound,
2224
];
2325

@@ -33,6 +35,10 @@ class SuccessRule {
3335
*/
3436
protected $parameters;
3537

38+
public static function instance() {
39+
return new static;
40+
}
41+
3642
public function __construct() {
3743
}
3844

@@ -60,18 +66,22 @@ public function setParameters( $parameters ) {
6066
}
6167

6268
/**
63-
* @param $response
69+
* @param \GuzzleHttp\Psr7\Response $response
6470
* @param $breaksOnSuccess
6571
*
6672
* @return bool|\DPRMC\WebBot\Success
6773
* @throws \DPRMC\WebBot\Exceptions\FailureRule\UndefinedSuccessRuleType
6874
*/
6975
public function run( $response, $breaksOnSuccess ) {
7076

77+
7178
switch ( $this->type ):
7279
case self::regex:
7380
$result = $this->runSuccessRuleRegEx( $this->parameters, $response->getBody() );
7481
break;
82+
case self::responseCode:
83+
$result = $this->runSuccessRuleRegEx( $this->parameters, $response->getStatusCode() );
84+
break;
7585
default:
7686
throw new UndefinedSuccessRuleType( "You attempted to run a success rule type of [" . $this->type . "]" );
7787
endswitch;
@@ -96,4 +106,18 @@ protected function runSuccessRuleRegEx( $pattern, $string ) {
96106

97107
return false;
98108
}
109+
110+
/**
111+
* @param $expected
112+
* @param $actual
113+
*
114+
* @return bool
115+
*/
116+
protected function runSuccessRuleResponseCode( $expected, $actual ) {
117+
if ( $expected == $actual ):
118+
return true;
119+
endif;
120+
121+
return false;
122+
}
99123
}

src/WebBot.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
namespace DPRMC\WebBot;
44

5+
use DPRMC\WebBot\Exceptions\WebBot\ResponseObjectNotSetForIndex;
56
use GuzzleHttp\Client;
7+
use GuzzleHttp\Psr7\Response;
8+
69

710
class WebBot {
11+
12+
813
/**
9-
* @var \GuzzleHttp\Client The Client that sends the HTTP requests.
14+
* @var \GuzzleHttp\Client $client
1015
*/
1116
protected $client;
1217

18+
1319
/**
1420
* @var array $steps An array of Step objects where the index is the step name.
1521
*/
@@ -25,6 +31,11 @@ class WebBot {
2531
*/
2632
protected $responses;
2733

34+
35+
public static function instance() {
36+
return new static;
37+
}
38+
2839
/**
2940
* WebBot constructor.
3041
* @link http://docs.guzzlephp.org/en/stable/quickstart.html#cookies Explains the cookies setting in the Client.
@@ -40,13 +51,16 @@ public function __construct() {
4051
*
4152
* @param string $name
4253
* @param \DPRMC\WebBot\Step $step
54+
* @param boolean $debug
4355
*
4456
* @return $this Required for Fluent interface.
4557
*/
46-
public function addStep( $name, $step ) {
58+
public function addStep( $name, $step, $debug = false ) {
59+
$step->setDebug( $debug );
4760
$this->steps[ $name ] = $step;
4861
$this->initResponseElement( $name );
4962

63+
5064
return $this;
5165
}
5266

@@ -65,6 +79,12 @@ public function run() {
6579
return $this;
6680
}
6781

82+
protected function initStepResultElements() {
83+
foreach ( $this->steps as $name => $step ):
84+
$this->initStepResultElement( $name );
85+
endforeach;
86+
}
87+
6888
/**
6989
* @param string $name
7090
* @param \DPRMC\WebBot\Step $step
@@ -96,20 +116,25 @@ protected function initStepResultElement( $name ) {
96116
* @param string $name
97117
*/
98118
protected function initResponseElement( $name ) {
99-
$this->responses[ $name ] = null;
119+
$this->responses[ $name ] = new Response();
100120
}
101121

102122
/**
103123
* @param string $name The step name of the Response who's body you want.
104124
*
105125
* @return \GuzzleHttp\Psr7\Stream|\Psr\Http\Message\StreamInterface
126+
* @throws \DPRMC\WebBot\Exceptions\WebBot\ResponseObjectNotSetForIndex
106127
*/
107128
public function getResponseBody( $name ) {
108129
/**
109130
* @var \GuzzleHttp\Psr7\Response $response
110131
*/
111132
$response = $this->responses[ $name ];
112133

134+
if ( Response::class !== get_class( $response ) ):
135+
throw new ResponseObjectNotSetForIndex( "The response object was not set for the step labeled: " . $name );
136+
endif;
137+
113138
return $response->getBody();
114139
}
115140

@@ -121,4 +146,11 @@ public function getResponseBody( $name ) {
121146
public function getStepResult( $name ) {
122147
return $this->stepResults[ $name ];
123148
}
149+
150+
/**
151+
* @return array An array of StepResult objects.
152+
*/
153+
public function getStepResults() {
154+
return $this->stepResults;
155+
}
124156
}

0 commit comments

Comments
 (0)