Skip to content

Instantly share code, notes, and snippets.

@corburn
Last active August 29, 2015 14:02
Show Gist options
  • Save corburn/833157ab3ce8ad65bd8f to your computer and use it in GitHub Desktop.
Save corburn/833157ab3ce8ad65bd8f to your computer and use it in GitHub Desktop.
PHP PSR-4 Package
vendor/
composer.lock
<?php namespace \Vendor\NameSpace;
class ClassName {
public function doStuff($bool = true)
{
return $bool;
}
}
<?php namespace \Vendor\Tests
/**
* Conventions and steps for writing tests:
* 1. The tests for a class Class go into a class ClassTest.
* 2. ClassTest inherits (most of the time) from PHPUnit_Framework_TestCase.
* 3. The tests are public methods that are named test*.
* Alternatively, you can use the @test annotation in a method's docblock to mark it as a test method.
* 4. Inside the test methods, assertion methods such as assertEquals() (see Appendix A) are used to assert that an actual value matches an expected value.
*/
use \Vendor\NameSpace\ClassName;
/**
* Using the markTestSkipped(string $message) function or @require annotation will skip the marked test(s)
* @requires extension | PHP | PHPUnit | OS | function <precondition>
*/
class ClassNameTest extends \PHPUnit_Framework_TestCase {
protected function setUp() {
$this->instance = new ClassName;
}
public function testSomething() {
// Raises an PHPUnit_Framework_IncompleteTestError exception marking the test incomplete
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
///////////////////////
// Test Dependencies //
///////////////////////
public function testProducerFirst() {
$this->assertTrue(true);
return 'first';
}
public function testProducerSecond() {
$this->assertTrue(true);
return 'second';
}
/**
* @depends testProducerFirst
* @depends testProducerSecond
*/
public function testConsumer() {
$this->assertEquals(
array('first', 'second'),
func_get_args()
);
}
////////////////////
// Data Providers //
////////////////////
public function additionProvider() {
// All data providers are executed before the setup* methods so PHPUnit can comupute the total number of tests
// Can be an array of arrays or an iterator
return array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
);
}
/**
* @dataProvider additionProvider
*/
public function testAdd($a, $b, $expected) {
// @dataProvider arguments come before @depends arguments
$this->assertEquals($expected, $a + $b);
// The result of a test that uses data providers cannot be injected into a depending test
}
////////////////////////
// Testing Exceptions //
////////////////////////
/**
* These can be combined or used separately
* Note: PHP's error_reporting runtime configuration can limit which errors PHPUnit will convert to exceptions
* @expectedException PHPUnit_Framework_Error
* @expectedException PHPUnit_Framework_Error_Warning
* @expectedException PHPUnit_Framework_Error_Notice
* @expectedException InvalidArgumentException
* @expectedExceptionMessage Right Message
* @expectedExceptionCode 20
*/
public function testException() {
$this->setExpectedException(
'InvalidArgumentException', 'Right Message', 20
);
// Return the name of the expected exception
$this->getExpectedException();
try {
} catch(InvalidArgumentException $e) {
return;
}
$this->fail('An expected exception has not been raised.');
}
////////////////////
// Testing output //
////////////////////
public function testExpectFooActualFoo() {
$this->expectOutputRegex(string $regularExpression)
$this->expectOutputString(string $expectedString);
print 'foo';
}
}
{
"name": "vendor/project-name",
"description": "",
// Only use if the version cannot be inferred from the VCS tag name
"version": "0.0.0[-dev | -patch | -alpha | -beta | -RC]",
"type": "library | project | metapackage | composer-plugin | <custom-type>",
"homepage": "example.org",
"time": "YYYY-MM-DD" | "YYYY-MM-DD HH:MM:SS"
"license": "MIT" | ["GPL-3.0+"],
"keywords": ["foo", "bar"],
"authors": [
{
"name": "Jason Travis",
"email": "JasonTravis@nau.edu",
"homepage": "corburn.github.io",
"role": "Developer"
}
],
"support": {
"email": "support@example.org",
"issues": "issues.example.org",
"forum": "forum.example.org",
"wiki": "wiki.example.org",
"irc": "irc://server/channel",
"source": "source.example.org"
}
"require": {},
"require-dev": {
"phpunit/phpunit": "[4.*] [@dev | @patch | @alpha | @beta | @RC]",
"phpunit/php-invoker": "*",
"phpunit/dbunit": ">=1.2",
"phpunit/selenium": ">=1.2",
"phpdocumentor/phpdocumentor": "2.*"
},
"autoload": {
"psr-4": {
"Vendor\\NameSpace\\" | "": "src/" | ["src/", "lib/"]
},
// Libraries that do not support PSR-0/4
"classmap": ["src/", "lib/", "ClassName.php"],
// Required with every request
"files": ["src/MyLibrary/helperFunctions.php"]
},
"autoload-dev": {
"psr-4": { "Vendor\\Tests\\": "tests/" }
},
"minimum-stability": "dev" | "alpha" | "beta" | "RC" | "stable",
"prefer-stable": true
// See https://getcomposer.org/doc/04-schema.md for more options
// The repositories field can specify an array of places to look for packages
}
<?php namespace \Vendor\Tests
/**
* Four stages of a unit test
* 1. Set up fixture
* 2. Exercise System Under Test
* 3. Verify outcome
* 4. Teardown
*/
class MyGuestbookTest extends \PHPUnit_Extensions_Database_TestCase {
/**
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection() {
$pdo = new PDO('sqlite::memory:');
return $this->createDefaultDBConnection($pdo, ':memory:');
}
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet() {
return $this->createFlatXMLDataSet(dirname(__FILE__).'/_files/guestbook-seed.xml');
}
}
?>
project
├── .puppet/
├── bin/
├── config/
├── src/
│   └── ClassName.php
├── tests/
│   ├── ClassNameTest.php
│   └── DatabaseTest.php
├── web/
│   ├── css/
│   ├── img/
│   └── index.php/
├── .bowerrc
├── .gitignore
├── .travis.yml
├── Gemfile
├── README.md
├── Vagrantfile
├── bower.json
├── build.xml
├── composer.json
├── package.json
└── phpunit.xml.dist
<?xml version="1.0" encoding="UTF-8" ?>
<phpdoc>
<parser>
<target>reports/doc</target>
</parser>
<transformer>
<target>reports/doc</target>
</transformer>
<files>
<directory>app</directory>
</files>
</phpdoc>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="true"
processIsolation="false"
stopOnFailure="false"
strict="true"
syntaxCheck="false"
verbose="true"
>
<testsuites>
<testsuite name="Application Test Suite">
<directory>./app/tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app/</directory>
<!--
<file>/path/to/file</file>
<exclude>
<directory suffix=".php">/path/to/files</directory>
<file>/path/to/file</file>
</exclude>
-->
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="reports" charset="UTF-8" highlight="true" lowUpperBound="35" highLowerBound="70"/>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false" showOnlySummary="true"/>
<log type="testdox-html" target="reports/testdox.html"/>
<!--<log type="coverage-clover" target="/tmp/coverage.xml"/>
<log type="coverage-php" target="/tmp/coverage.serialized"/>
<log type="json" target="/tmp/logfile.json"/>
<log type="tap" target="/tmp/logfile.tap"/>
<log type="junit" target="/tmp/logfile.xml" logIncompleteSkipped="false"/>
<log type="testdox-text" target="/tmp/testdox.txt"/>-->
</logging>
</phpunit>
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
before_script:
- composer self-update
- composer install --prefer-source --no-interaction --dev
script: phpunit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment