diff --git a/src/PositiveOverNegativeCountTrait.php b/src/PositiveOverNegativeCountTrait.php new file mode 100644 index 0000000..848e1b2 --- /dev/null +++ b/src/PositiveOverNegativeCountTrait.php @@ -0,0 +1,21 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PHPUnitGoodPractices; + +trait PositiveOverNegativeCountTrait +{ + // TODO: + // public static function assertCount($expectedCount, $haystack, $message = '') + // public static function assertAttributeCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '') + // public static function assertNotCount($expectedCount, $haystack, $message = '') + // public static function assertAttributeNotCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '') +} diff --git a/tests/PositiveOverNegativeCountTraitTest.php b/tests/PositiveOverNegativeCountTraitTest.php new file mode 100644 index 0000000..aefbf18 --- /dev/null +++ b/tests/PositiveOverNegativeCountTraitTest.php @@ -0,0 +1,136 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PHPUnitGoodPractices\Tests; + +use PHPUnit\Framework\Error\Warning; +use PHPUnit\Framework\TestCase; +use PHPUnitGoodPractices\PositiveOverNegativeCountTrait; + +/** + * @covers \PHPUnitGoodPractices\PositiveOverNegativeCountTrait + */ +final class PositiveOverNegativeCountTraitTest extends TestCase +{ + use PositiveOverNegativeCountTrait; + + public $fixtureAttributePositive = array(11); + public $fixtureAttributeZero = array(); + + public function expectException($exception) + { + if (is_callable(array('parent', 'expectException'))) { + parent::expectException($exception); + } else { + $this->setExpectedException($exception); + } + } + + /** + * @param array $data + * @param int $expected + * @param bool $shouldCrash + * + * @dataProvider provideAssertCountCases + */ + public function testAssertCount($data, $expected, $shouldCrash) + { + if ($shouldCrash) { + $this->expectException(Warning::class); + } + + $this->assertCount($expected, $data); + } + + public function provideAssertCountCases() + { + return array( + array(array('foo', 'bar'), 2, false), + array(array(), 0, false), + array(array(), -2, true), + ); + } + + /** + * @param array $data + * @param int $expected + * @param bool $shouldCrash + * + * @dataProvider provideAssertNotCountCases + */ + public function testAssertNotCount($data, $expected, $shouldCrash) + { + if ($shouldCrash) { + $this->expectException(Warning::class); + } + + $this->assertNotCount($expected, $data); + } + + public function provideAssertNotCountCases() + { + return array( + array(array('foo', 'bar'), -2, false), + array(array(), 10, false), + array(array(), -2, true), + ); + } + + /** + * @param string $key + * @param int $expected + * @param bool $shouldCrash + * + * @dataProvider provideAssertAttributeCountCases + */ + public function testAssertAttributeCount($key, $expected, $shouldCrash) + { + if ($shouldCrash) { + $this->expectException(Warning::class); + } + + $this->assertAttributeCount($expected, $key, $this); + } + + public function provideAssertAttributeCountCases() + { + return array( + array('fixtureAttributePositive', count($this->fixtureAttributePositive), false), + array('fixtureAttributeZero', count($this->fixtureAttributeZero), false), + array('fixtureAttributeZero', -1, true), + ); + } + + /** + * @param string $key + * @param int $expected + * @param bool $shouldCrash + * + * @dataProvider provideAssertAttributeCountCases + */ + public function testAssertAttributeNotCount($key, $expected, $shouldCrash) + { + if ($shouldCrash) { + $this->expectException(Warning::class); + } + + $this->assertAttributeNotCount($expected, $key, $this); + } + + public function provideAssertAttributeNotCountCases() + { + return array( + array('fixtureAttributePositive', 10 + count($this->fixtureAttributePositive), false), + array('fixtureAttributeZero', 10 + count($this->fixtureAttributeZero), false), + array('fixtureAttributeZero', -1, true), + ); + } +}