Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kindly provide us the implementation process. #15

Open
lds-engineers opened this issue Mar 15, 2018 · 11 comments
Open

Kindly provide us the implementation process. #15

lds-engineers opened this issue Mar 15, 2018 · 11 comments

Comments

@lds-engineers
Copy link

Could not able to implement this in php. Kindly provide us the implementation process.

@byjg
Copy link
Owner

byjg commented Mar 15, 2018

Hello,

Your question is so broad. Could you specify a little bit more?

@lds-engineers
Copy link
Author

I am installing this using composer after that i am hitting the url where src path is but could not able to run. cannot able to implement this.

@lds-engineers
Copy link
Author

Hi, Kindly check the screenshot we are getting this error.
getting-path-error

@byjg
Copy link
Owner

byjg commented Mar 16, 2018

This library does not provide a REST Server. This component can validate if your REST application (previously created and working) is receiving and responding the values that are defined previously in your swagger JSON file.

So basically you have three usages:

Using it as Functional Test cases

In that case you need to have a REST Server running and need to have a PHPUnit class and create your test cases. See below an example of test case:

    public function testPost()
    {
        $request = new \ByJG\Swagger\SwaggerRequester();
        $request
            ->withMethod('POST')
            ->withPath("/path/for/post/2")
            ->withRequestBody(['name'=>'new name', 'field' => 'value']);

        $this->assertRequest($request);
    }

This code will try do a POST request to server is defined in the swagger.json, with path "/path/for/post/2" and with a URL-Form-Enconded parameter.

If the request fail, or if the response is not as expected in the SWAGGER.JSON then a exception will be throwed and you know there are something not expected according your documentation.

Using it as Unit Test cases

In that case you are just validate the request and response parameter without make a REAL Request to your rest server. You have to create a test case and pass the parameters and Assert if all results as a expected.

For example:

<?php
// Create the SwaggerSchema object;
$swaggerSchema = new \ByJG\Swagger\SwaggerSchema($contentsOfSwaggerJson);

// Get the definitions you want to test
$path = '/path/to/method';
$statusExpected = 200;
$method = 'POST';
$bodyRequestDef = $swaggerSchema->getRequestParameters($path, $method);
$bodyResponseDef = $swaggerSchema->getResponseParameters($path, $method, $statusExpected);

// Match the result. If failed an exception will be throwed:
$bodyRequestDef->match($requestBody);
$bodyResponseDef->match($responseBody);

Using it as Runtime parameters validator

In that case you'll put the Swagger Test in front of your request and make the validations before execute your method.

This is guarantee your are receiving exactly you defined in the documentation.

@lds-engineers
Copy link
Author

Kindly let me know that how i will provide the path "$path = '/path/to/method';". There is only path error.
codes

@byjg
Copy link
Owner

byjg commented Mar 20, 2018

This component is totally based on your swagger.json file. In your case you are reading the JSON file at line 252.

Your "path" has 2 errors:

  • Has the host
  • Has the Parameter

Both (host and parameters) and the path also are provided by the swagger.json. I create a swagger.json that fit in your example:

{
  "swagger": "2.0",
  "host": "122.176.28.104",
  "schemes": [
    "http"
  ],
  "paths": {
    "/skylineuniversity/local/lingk/mvcswagger/getuser.php": {
      "get": {
        "parameters": [
          {
            "in": "query",
            "name": "wstoken",
            "description": "The authentication token",
            "required": true
          }
        ],
        "responses": {
          "200": {
            "description": "Everything is OK"
          }
        }
      }
    }
  }
}

And change the line 254 to:

$path = "/skylineuniversity/local/lingk/mvcswagger/getuser.php"

And provide the follow value for the $requestBody variable before the line 264:

$requestBody = [
    "wstoken" => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
];

Some notes

  • I strongly suggest you read a little bit more about REST Services and the Swagger.json (OAS 2.0);
  • If you want to create a full PHP REST application with Swagger integrated, database, PSR-11 implementations and others, I suggest you to take a look at another project mine: https://github.com/byjg/php-rest-template.

@lds-engineers
Copy link
Author

Hi sir,

Hope you are doing well!

Thanks for replying.

Now the path error is gone and there is an error which says "Body is passed but there is no request body definition".

code-after-update
request-error

@byjg
Copy link
Owner

byjg commented Mar 20, 2018

Sorry, please remove the $requestBody parameter by putting $requestBody = []. The request body is only necessary if you passing a JSON or a multipart/form-data.

Until this moment the class is not validating parameters in the "QUERY". Only in the PATH or BODY.

@lds-engineers
Copy link
Author

Hi Sir,

Thanks for your reply.

Now every error is gone. But i just wanted to know this part which is added in screenshot.
image

@byjg
Copy link
Owner

byjg commented Mar 22, 2018

As I said before, instead to purely remove the $requestBody parameter you have to empty it, doing this: $requestBody = []

@byjg
Copy link
Owner

byjg commented Jun 7, 2018

May I close this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants