Description
The way to go with unit tests is:
- Refactor in the way to encapsulated all the code about interaction with remote server (curl, etc) in objects so the only responsibility of this objects will be those connections and nothing else. (this is about Emitters). At the end of refactoring there is no one variable or method with "debug" in name.
- Rewrite the tests in the way that tests uses mocks (https://phpunit.de/manual/current/en/test-doubles.html) of those objects instead of real objects. Since objects are only about remote connections there is no need for us to test it since curl library has its own tests in PHP core. In test if right methods are called on mocks the right quantity of time and with proper values of parameters.
$emitterCurlMock = //Here lots of lines that would set up mock!
// use: disableOriginalConstructor()
// set how what methods mock have and how many times they should be called
// Use returnCallback() to return somthing so code outside would "think" it deals with real object
// So at this point we know that right mock methods has been called right amount of time
$subject = Snowplow\Tracker\Subject()
$trucker = new Tracker($emitterCurlMock, $subject, "nodejs-tracker")
//Here we run our processing:
$trucker->trackUnstructEvent($json, $context, $timestamp)
//Here we get out from our tracker what data would be send (is not send since we use mock).
// So for instance if we send some JSON in a body check if this json is right:
$this->assertEquals($trucker->useApiToGetJsonThatWouldBeSend(), "{this is the json}");