title: Proper way to test REST API (php)
body: <p>Never thought that testing a REST API will be that difficult, when I was doing fullstack, I was dreaming about go to REST so I don't have mess with html and all that stuff, only Behat saves me, it helps a lot when comes to testing html pages.</p>
  
  <p>But when it comes to testing an API I have a headache. let's say I have some resource, like list of user's conversations.</p>
  
  <pre><code>{
    "_links": {
      "self": {
        "href": "/api/v1/conversations"
      }
    },
    "_embedded": {
      "items": [
        {
          "id": 4,
          "name": "Lana Smith",
          "avatarUrl": "/static/avatars/597bd819d90f2568107586.png",
          "createdAt": "2017-01-05T00:00:00+03:00",
          "updatedAt": "2017-01-05T00:00:00+03:00",
          "_links": {
            "self": {
              "href": "/api/v1/conversations/4"
            }
          }
        },
        {
          "id": 3,
          "name": "Ian North [Support Agent]",
          "avatarUrl": "/static/avatars/default.png",
          "createdAt": "2017-01-04T01:00:00+03:00",
          "updatedAt": "2017-01-04T01:02:00+03:00",
          "_links": {
            "self": {
              "href": "/api/v1/conversations/3"
            }
          }
        },
      ]
    }
  }
  </code></pre>
  
  <p>And as a decent human being I have Swagger documentation, that states that this particular resource should look like this, those fields are optional, those are of type integer, there you have a relation and so on and so forth</p>
  
  <p>But:</p>
  
  <ul>
  <li>how do I test that structure IS as swagger docs says it is</li>
  <li>how do I
  test that data is correct (like that user has exact his
  conversations, not someone else's, and correct number of them)</li>
  </ul>
  
  <p>I have few approaches in my mind:</p>
  
  <ul>
  <li>Just test whole payload of JSON hardcoded in test against one that came from response. The difficulty here that sometimes I don't know the exact values for example createdAt/updatedAt generated during fixtures loading, avatar urls are always random, unless default and so on.</li>
  <li>Use phpunit and test everything manually like: assertPropertyCount($response, 2, '_embedded.items) then assertProperty equals(...)</li>
  </ul>
  
  <p>How do I do it that way that it doesn't take 5 times longer that writing production code</p>