<?php
//Add two numbers class
class AdditionNumber
{
public function addTwoNumbers($number1, $number2)
{
return $number1 + $number2;
}
}
<?php
//Add two numbers class
// the version of PHPUnit 4.8 or older.
class AdditionNumberTest extends PHPUnit_Framework_TestCase
{
public function testAddTwoNumbersCanBeAdded()
{
$addition = new AdditionNumber();
$result = $addition->addTwoNumbers(1, 2);
$expected = 3;
$this->assertSame($result, $expected);
}
}
<?php
// Add two numbers class
// The version of PHPUnit 5.7 or older.
// The prefix "test" style
use PHPUnit/Framework/TestCase;
class AdditionNumberTest extends TestCase
{
public function testAddTwoNumbersCanBeAdded()
{
$addition = new AdditionNumber();
$result = $addition->addTwoNumbers(1, 2);
$expected = 3;
$this->assertSame($result, $expected);
}
}
<?php
// Add two numbers class.
// the version of PHPUnit 5.7 or older.
// annotation style
use PHPUnit/Framework/TestCase;
class AdditionNumberTest extends TestCase
{
/** @test */
public function addTwoNumbersCanBeAdded()
{
$addition = new AdditionNumber();
$result = $addition->addTwoNumbers(1, 2);
$expected = 3;
$this->assertSame($result, $expected);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="/path/to/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
charset="UTF-8"
processIsolation="false">
<testsuites>
<testsuite name="Test Suite Name">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="/path/to/folder"/>
</logging>
</phpunit>
Don't forget to install the Xdebug extension
The whole sample project is available here.
A PHP library to communicate with Yahoo Weather API.
Project is available here.
<?php
/*
* (c) Jérémy Marodon <marodon.jeremy@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Th3Mouk\YahooWeatherAPI\Tests;
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
use Th3Mouk\YahooWeatherAPI\YahooWeatherAPI;
class YahooWeatherClientTests extends TestCase
{
/** @test */
public function testCallApiWoeidException()
{
$service = new YahooWeatherAPI();
$this->expectException(\Exception::class);
try {
$response = $service->callApiWoeid(null);
} catch (\Exception $e) {
throw $e;
}
}
<?php
/*
* (c) Jérémy Marodon <marodon.jeremy@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Th3Mouk\YahooWeatherAPI\Tests;
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
use Th3Mouk\YahooWeatherAPI\YahooWeatherAPI;
class YahooWeatherClientTests extends TestCase
{
public function testCallApiWoeidException()
{
$service = new YahooWeatherAPI();
$this->expectException(\Exception::class);
try {
$response = $service->callApiWoeid(null);
} catch (\Exception $e) {
throw $e;
}
}
<?php
/*
* (c) Jérémy Marodon <marodon.jeremy@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Th3Mouk\YahooWeatherAPI\Tests;
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
use Th3Mouk\YahooWeatherAPI\YahooWeatherAPI;
class YahooWeatherClientTests extends TestCase
{
/**
* @expectedException \Exception
*/
public function testCallApiWoeidException()
{
$service = new YahooWeatherAPI();
$response = $service->callApiWoeid(null);
}
Method name | Mapped testing method name |
---|---|
callApiWoeid | testCallApiWoeidException |
callApiCityName | testCallApiCityNameException |
callApi | testCallApiLastResponse |
setClient | testSetClient |
setLastResponse | testSetLastResponse |
Project is available here
Project is available here.
language: php
sudo: false
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- nightly
matrix:
fast_finish: true
allow_failures:
- php: nightly
before_install:
- phpenv config-rm xdebug.ini
install:
- mkdir -p ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d
- echo "memory_limit=-1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- mkdir -p build/logs
- composer global require satooshi/php-coveralls:@stable --no-update
- composer global update --prefer-dist --no-interaction
- composer update --prefer-dist --no-interaction $COMPOSER_FLAGS
before_script:
- echo "zend_extension=xdebug.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini
script:
- vendor/bin/phpunit
after_success:
- bash <(curl -s https://codecov.io/bash)