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

Add unit tests #70

Merged
merged 2 commits into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ php:
- 7.1
- 7.2

cache:
directories:
- $HOME/.composer/cache

# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
matrix:
fast_finish: true
Expand All @@ -16,5 +20,9 @@ matrix:
dist: precise

script:
# check syntax all PHP files, redirect stderr to file and return 0 if file is empty
# Check syntax all PHP files, redirect stderr to file and return 0 if file is empty
- find . -name '*.php' -exec php -l {} > /dev/null \; 2> /tmp/lint.err ; test ! -s /tmp/lint.err

# Execute PHPUnit tests
- composer install --prefer-dist
- vendor/bin/phpunit --coverage-text
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,11 @@ $statsd->event('Now it is fixed.',
- Add a configurable timeout for event submission via TCP
- Write unit tests
- Document service check functionality

## Contributing

### Tests

```bash
composer test
```
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,23 @@
"DataDog\\": "src/"
}
},
"autoload-dev": {
"files": [
"tests/curl_and_error_log_function_stubs.php",
"tests/mt_rand_function_stubs.php",
"tests/socket_function_stubs.php"
],
"psr-4": {
"DataDog\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"require-dev": {
"phpunit/phpunit": "4.8.36"
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true">
<testsuite name="unit">
<directory suffix="Test.php">tests/UnitTests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
169 changes: 169 additions & 0 deletions tests/TestHelpers/CurlSpy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

namespace DataDog\TestHelpers;

/**
* Class CurlSpy
*
* Useful for recording calls to stubbed global curl functions built into PHP
*
* @see \DataDog\CurlSpyTestCase
* @package DataDog
*/
class CurlSpy
{
/**
* @var array
*/
public $argsFromCurlInitCalls = array();

/**
* @var array
*/
public $curlInitReturnValues = array();

/**
* @var array
*/
public $argsFromCurlSetoptCalls = array();

/**
* @var array
*/
public $argsFromCurlExecCalls = array();

/**
* @var string
*/
public $responseBody = '';

/**
* @var array
*/
public $argsFromCurlGetinfoCalls = array();

/**
* @var int
*/
public $responseCode;

/**
* @var array
*/
public $argsFromCurlErrnoCalls = array();

/**
* @var int
*/
public $errorNumber;

/**
* @var array
*/
public $argsFromCurlCloseCalls = array();

/**
* @var array
*/
public $argsFromErrorLogCalls = array();

/**
* @param string $url
*/
public function curlInitWasCalledWithArg($url)
{
$this->argsFromCurlInitCalls[] = $url;
}

/**
* @param resource $ch
*/
public function curlInitDidReturn($ch)
{
$this->curlInitReturnValues[] = $ch;
}

/**
* @var array
*/
public $argsFromCurlErrorCalls = array();

/**
* @var string
*/
public $curlError = "";

/**
* @param resource $ch
* @param int $option
* @param mixed $value
*/
public function curlSetoptWasCalledWithArgs(
$ch,
$option,
$value
) {
$this->argsFromCurlSetoptCalls[] = array(
$ch,
$option,
$value,
);
}

/**
* @param resource $ch
*/
public function curlExecCalledWithArg($ch)
{
$this->argsFromCurlExecCalls[] = $ch;
}

/**
* @param resource $ch
*/
public function curlExecDidReturn($ch)
{
$this->curlExecReturnValues[] = $ch;
}

/**
* @param resource $ch
* @param int $opt
*/
public function curlGetinfoCalledWithArgs($ch, $opt)
{
$this->argsFromCurlGetinfoCalls[] = array($ch, $opt);
}

/**
* @param resource $ch
*/
public function curlErrnoCalledWithArg($ch)
{
$this->argsFromCurlErrnoCalls[] = $ch;
}

/**
* @param resource $ch
*/
public function curlCloseCalledWithArg($ch)
{
$this->argsFromCurlCloseCalls[] = $ch;
}

/**
* @param resource $ch
*/
public function curlErrorCalledWithArg($ch)
{
$this->argsFromCurlErrorCalls[] = $ch;
}

/**
* @param string $message
*/
public function errorLogCallsWithArg($message)
{
$this->argsFromErrorLogCalls[] = $message;
}
}
32 changes: 32 additions & 0 deletions tests/TestHelpers/CurlSpyTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace DataDog\TestHelpers;

use PHPUnit\Framework\TestCase;

$curlSpy = new CurlSpy();

class CurlSpyTestCase extends TestCase
{
/**
* Set up a spy object to capture calls to built in curl functions
*/
protected function setUp()
{
global $curlSpy;

$curlSpy = new CurlSpy();

parent::setUp();
}

/**
* @return \DataDog\TestHelpers\CurlSpy
*/
protected function getCurlSpy()
{
global $curlSpy;

return $curlSpy;
}
}
103 changes: 103 additions & 0 deletions tests/TestHelpers/SocketSpy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace DataDog\TestHelpers;

/**
* Class SocketSpy
*
* Useful for recording calls to stubbed global socket functions built into PHP
*
* @see \DataDog\SocketSpyTestCase
* @package DataDog
*/
class SocketSpy
{
/**
* @var array
*/
public $argsFromSocketCreateCalls = array();

/**
* @var array
*/
public $socketCreateReturnValues = array();

/**
* @var array
*/
public $argsFromSocketSetNonblockCalls = array();

/**
* @var array
*/
public $argsFromSocketSendtoCalls = array();

/**
* @var array
*/
public $argsFromSocketCloseCalls = array();

/**
* @param int $domain
* @param int $type
* @param int $protocol
*/
public function socketCreateWasCalledWithArgs($domain, $type, $protocol)
{
$this->argsFromSocketCreateCalls[] = array(
$domain,
$type,
$protocol
);
}

/**
* @param resource $socket
*/
public function socketCreateDidReturn($socket)
{
$this->socketCreateReturnValues[] = $socket;
}

/**
* @param resource $socket
*/
public function socketSetNonblockWasCalledWithArg($socket)
{
$this->argsFromSocketSetNonblockCalls[] = $socket;
}

/**
* @param resource $socket
* @param string $buf
* @param int $len
* @param int $flags
* @param string $addr
* @param int $port
*/
public function socketSendtoWasCalledWithArgs(
$socket,
$buf,
$len,
$flags,
$addr,
$port
) {
$this->argsFromSocketSendtoCalls[] = array(
$socket,
$buf,
$len,
$flags,
$addr,
$port
);
}

/**
* @param resource $socket
*/
public function socketCloseWasCalled($socket)
{
$this->argsFromSocketCloseCalls[] = $socket;
}
}