File: test/ResponseBehaviour/ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviourTest.php

Recommend this page to a friend!
  Classes of nvb  >  PHP CURL Component  >  test/ResponseBehaviour/ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviourTest.php  >  Download  
File: test/ResponseBehaviour/ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviourTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: PHP CURL Component
Compose and execute HTTP requests with Curl
Author: By
Last change: replaced >>array()<< notation with >>[]<<
Date: 3 years ago
Size: 2,276 bytes
 

Contents

Class file image Download
<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since: 2015-12-16
 */

namespace Test\Net\Bazzline\Component\Curl\ResponseBehaviour;

use
Net\Bazzline\Component\Curl\Response\Response;
use
Net\Bazzline\Component\Curl\ResponseBehaviour\ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviour;
use
RuntimeException;
use
Test\Net\Bazzline\Component\Curl\AbstractTestCase;

class
ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviourTest extends AbstractTestCase
{
   
/**
     * @return array
     */
   
public function testCases()
    {
        return [
           
'status code below limit' => [
               
0,
               
1,
               
$this->createResponse(0),
               
false
           
],
           
'status code on the limit' => [
               
1,
               
1,
               
$this->createResponse(1),
               
true
           
],
           
'status code over the limit' => [
               
2,
               
1,
               
$this->createResponse(2),
               
true
           
]
        ];
    }

   
/**
     * @dataProvider testCases
     * @param int $statusCode
     * @param int $firstStatusCodeThatIsOverTheLimit
     * @param Response $response
     * @param bool $exceptionExpected
     */
   
public function testBehaviour($statusCode, $firstStatusCodeThatIsOverTheLimit, Response $response, $exceptionExpected)
    {
       
$behaviour = new ThrowRuntimeExceptionIfStatusCodeIsAboveTheLimitBehaviour($firstStatusCodeThatIsOverTheLimit);

        try {
           
$behaviour->behave($response);
        } catch (
RuntimeException $exception) {
            if (
$exceptionExpected) {
               
$expectedExceptionMessage = 'limit of status code exceeded. dumping response: ' . implode(', ', $response->convertIntoAnArray());
               
$this->assertEquals($exception->getMessage(), $expectedExceptionMessage);
            } else {
               
$this->fail('exception not expected: ' . $exception->getMessage());
            }
        }
    }

   
/**
     * @param int $statusCode
     * @return Response
     */
   
private function createResponse($statusCode)
    {
        return
$this->getNewResponse('example content', 'example content type', '', 0, [], $statusCode);
    }
}


For more information send a message to info at phpclasses dot org.