Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/QCheck/DataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace QCheck;

/**
* This class serves as a helper to create data providers
* for PHPUnit based testing.
*
* @package QCheck
*/
class DataProvider {
/**
* Transform an array of generators to an array of data that
* can be used for PHPUnit test methods.
*
* @param Generator[] $generators
* @param int $n number of data sets to generate.
* @return array
*/
public function provider(array $generators, $n = 100) {
$tuples = call_user_func_array('QCheck\Generator::tuples', $generators);
return $tuples->takeSamples($n);
}
}
33 changes: 33 additions & 0 deletions test/QCheck/DataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace QCheck;

class DataProviderTest extends \PHPUnit_Framework_TestCase {
function provider($name, $n = 10) {
return DataProvider::provider(array(
Generator::strings(),
Generator::ints(),
Generator::booleans()
), $n);
}

/**
* @dataProvider provider
*/
function testDataProvider($s, $i, $b) {
// this test is only supposed to prove that we have a valid provider
$this->assertTrue(is_string($s));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using $this->assertInternalType('string', $s); is preferred over using assertTrue

$this->assertTrue(is_int($i));
$this->assertTrue(is_bool($b));
}

function testDataProviderCount() {
$numbers = array(1, 10, 50);

foreach($numbers as $n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using foreach in a test is considered a bad practise. Using a dataprovider is advised instead.

$data = $this->provider('testDataProviderCount', $n);
$this->assertCount($n, $data);
$this->assertCount(3, $data[0]);
}
}
}