Skip to content

Commit b4f6902

Browse files
Stubbing out and testing the run function.
1 parent 3c23b9b commit b4f6902

4 files changed

Lines changed: 47 additions & 16 deletions

File tree

src/ClientDailyPriceFixedIncome.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,29 @@ protected function generateBodyForRequest() {
9595
* @return array
9696
*/
9797
protected function processResponse() {
98-
$body = (string)$this->response->getBody();
99-
98+
$body = $this->getBodyFromResponse();
10099
$prices = explode( "\n", $body );
101100
$prices = array_map( 'trim', $prices );
102-
101+
$prices = array_filter( $prices );
103102
array_pop( $prices ); // Remove the CRC check.
104103

105104
$return = [];
106-
foreach ( $this->cusips as $i => $cusip ) {
105+
foreach ( $this->cusips as $i => $cusip ):
107106
$return[ $cusip ] = $this->formatValueReturnedFromInteractiveData( $prices[ $i ] );
108-
}
107+
endforeach;
109108

110109
return $return;
111110
}
112111

112+
/**
113+
* Extracted this into it's own function so I can stub and test without
114+
* having to make a request to the IDC server.
115+
* @return string
116+
*/
117+
protected function getBodyFromResponse() {
118+
return (string)$this->response->getBody();
119+
}
120+
113121
protected function formatValueReturnedFromInteractiveData( $value ) {
114122
if ( is_numeric( $value ) ) {
115123
return (float)$value;

src/RemotePlusClient.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,28 +112,21 @@ protected function encodeUserAndPassForBasicAuthentication( $username, $pass ) {
112112
return base64_encode( $username . ':' . $pass );
113113
}
114114

115-
/**
116-
* Getter method for the Response object.
117-
* @return Response
118-
*/
119-
public function getResponse() {
120-
return $this->response;
121-
}
122-
123115
/**
124116
* @return mixed
125117
*/
126118
public function run() {
127-
$this->sendRequest();
119+
$this->response = $this->sendRequest();
128120

129121
return $this->processResponse();
130122
}
131123

132124
/**
133125
* Sends the request to Remote Plus, and saves the Response object into our local $response property.
126+
* @return Response The Guzzle response object.
134127
*/
135-
public function sendRequest() {
136-
$this->response = $this->client->request( 'POST', $this->page, [ 'debug' => $this->remotePlusDebug,
128+
protected function sendRequest() {
129+
return $this->client->request( 'POST', $this->page, [ 'debug' => $this->remotePlusDebug,
137130
'version' => $this->remotePlusHttpVersion,
138131
'headers' => [ 'Content-Type' => $this->remotePlusContentType,
139132
'Authorization' => $this->getAuthenticationHeaderValue( $this->user, $this->pass ), ],

tests/ClientDailyPriceFixedIncomeTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
use DPRMC\InteractiveData\ClientDailyPriceFixedIncome;
55
use DPRMC\InteractiveData\RemotePlusClient\Exceptions\UnparsableDateSentToConstructor;
66
use GuzzleHttp\Exception\ClientException;
7+
use GuzzleHttp\Psr7\Response;
78
use PHPUnit\Framework\TestCase;
9+
use ReflectionObject;
810

911
class ClientDailyPriceFixedIncomeTest extends TestCase {
1012

@@ -14,6 +16,8 @@ class ClientDailyPriceFixedIncomeTest extends TestCase {
1416

1517
protected $validDate = '2017-07-31';
1618

19+
protected $validPrices = [ '38259P508' => 919.46 ];
20+
1721
protected $invalidDate = 'this is not a date';
1822

1923
protected $validCusips = [ '38259P508' ]; // Google
@@ -54,4 +58,28 @@ public function testRunWithInvalidCredentials() {
5458
$client = new ClientDailyPriceFixedIncome( $this->invalidUser, $this->invalidPass, $this->validDate, $this->validCusips, FALSE );
5559
$client->run();
5660
}
61+
62+
public function testRunWithStubResponse() {
63+
$client = new ClientDailyPriceFixedIncome( $this->invalidUser, $this->invalidPass, $this->validDate, $this->validCusips, FALSE );
64+
65+
// Create a stub for the ClientDailyPriceFixedIncome class.
66+
$stubClient = $this->createPartialMock( ClientDailyPriceFixedIncome::class, [ 'sendRequest',
67+
'getBodyFromResponse' ] );
68+
// Configure the stub.
69+
$stubClient->method( 'sendRequest' )
70+
->willReturn( new Response() );
71+
72+
$fileContents = file_get_contents( './tests/files/38259P508.txt' );
73+
$stubClient->method( 'getBodyFromResponse' )
74+
->willReturn( $fileContents );
75+
76+
// Stub CUSIP list
77+
$refObject = new ReflectionObject( $stubClient );
78+
$refProperty = $refObject->getProperty( 'cusips' );
79+
$refProperty->setAccessible( TRUE );
80+
$refProperty->setValue( $stubClient, $this->validCusips );
81+
$cusipPriceArray = $stubClient->run();
82+
$this->assertArrayHasKey( $this->validCusips[ 0 ], $cusipPriceArray );
83+
$this->assertEquals( $this->validPrices[ $this->validCusips[ 0 ] ], $cusipPriceArray[ $this->validCusips[ 0 ] ] );
84+
}
5785
}

tests/files/38259P508.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
919.46
2+
\CRC=66ee

0 commit comments

Comments
 (0)