| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Dmitry Gladyshev <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace Rucaptcha\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GuzzleHttp\Client as GuzzleClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use GuzzleHttp\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use GuzzleHttp\Handler\MockHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use GuzzleHttp\HandlerStack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use GuzzleHttp\Psr7\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Rucaptcha\Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Rucaptcha\GenericClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class GenericClientTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function testGetHttpClientMustReturnClientInterfaceInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $client = new GenericClient(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $httpClient = $this->invokeMethod($client, 'getHttpClient'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->assertInstanceOf(ClientInterface::class, $httpClient); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function testGetLoggerMustReturnLoggerInterfaceInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $client = new GenericClient(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $logger = $this->invokeMethod($client, 'getLogger'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assertInstanceOf(LoggerInterface::class, $logger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function testEmptyCaptchaIdBeforeFirstSendCaptchaTask() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $client = new GenericClient(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertEquals('', $client->getLastCaptchaId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @dataProvider providerCorrectInResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function testSendCaptchaMustReturnCaptchaIdAndStoreLastCaptchaId($response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $client = self::buildClientWithMockedGuzzle($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $serverResponse = $client->sendCaptcha(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertEquals('1234567890', $serverResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->assertEquals('1234567890', $client->getLastCaptchaId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param string $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @dataProvider providerCorrectResResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function testGetCaptchaResultReturnSolvedStringOrFalse($response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $client = self::buildClientWithMockedGuzzle($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $serverResponse = $client->getCaptchaResult(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if($response==='CAPCHA_NOT_READY') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->assertFalse($serverResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $this->assertEquals('1234567890', $serverResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param $errorResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @dataProvider providerErrorInResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @expectedException \Rucaptcha\Exception\RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function testSendCaptchaMustThrowRucaptchaExceptionOnErrorResponse($errorResponse) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $client = self::buildClientWithMockedGuzzle($errorResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $client->sendCaptcha(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param $errorResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @dataProvider providerErrorResResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @expectedException \Rucaptcha\Exception\RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function testGetCaptchaResultMustThrowRucaptchaExceptionOnErrorResponse($errorResponse) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $client = self::buildClientWithMockedGuzzle($errorResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $client->getCaptchaResult(''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     /* Data providers */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function providerCorrectInResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ['OK|1234567890'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function providerCorrectResResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ['OK|1234567890'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ['CAPCHA_NOT_READY'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     public function providerErrorInResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             [Error::KEY_DOES_NOT_EXIST], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             [Error::WRONG_USER_KEY], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             [Error::ZERO_BALANCE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             [Error::NO_SLOT_AVAILABLE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             [Error::ZERO_CAPTCHA_FILESIZE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             [Error::TOO_BIG_CAPTCHA_FILESIZE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             [Error::WRONG_FILE_EXTENSION], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             [Error::IMAGE_TYPE_NOT_SUPPORTED], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             [Error::IP_NOT_ALLOWED], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             [Error::IP_BANNED], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             ["<html><head></head><body><h1>Page Not Found 404</h1></body></html>"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ["ERROR_NEW_SERVICE_ERROR_TYPE"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function providerErrorResResponse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             [Error::WRONG_ID_FORMAT], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             [Error::KEY_DOES_NOT_EXIST], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             [Error::CAPTCHA_UNSOLVABLE], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             [Error::WRONG_CAPTCHA_ID], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             [Error::BAD_DUPLICATES], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             ["<html><head></head><body><h1>Page Not Found 404</h1></body></html>"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             ["ERROR_NEW_SERVICE_ERROR_TYPE"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /* Helpers */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     public static function buildClientWithMockedGuzzle($response) | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         $mock = new MockHandler([ | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             new Response(200, [], $response) | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         $handler = HandlerStack::create($mock); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         $httpClient = new GuzzleClient(['handler' => $handler]); | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         $genericClient = new GenericClient(''); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |         $genericClient->setHttpClient($httpClient); | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |         return $genericClient; | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * Call protected/private method of a class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param object &$object    Instantiated object that we will run method on. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param string $methodName Method name to call | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      * @param array  $parameters Array of parameters to pass into method. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @return mixed Method return. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     public function invokeMethod(&$object, $methodName, array $parameters = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         $reflection = new \ReflectionClass(get_class($object)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $method = $reflection->getMethod($methodName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         $method->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         return $method->invokeArgs($object, $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |