Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit 48084b8

Browse files
committed
Add PositiveOverNegativeCountTrait
1 parent a502c24 commit 48084b8

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHPUnit Good Practices.
5+
*
6+
* (c) Dariusz Rumiński <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PHPUnitGoodPractices;
13+
14+
trait PositiveOverNegativeCountTrait
15+
{
16+
// TODO:
17+
// public static function assertCount($expectedCount, $haystack, $message = '')
18+
// public static function assertAttributeCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
19+
// public static function assertNotCount($expectedCount, $haystack, $message = '')
20+
// public static function assertAttributeNotCount($expectedCount, $haystackAttributeName, $haystackClassOrObject, $message = '')
21+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
/*
4+
* This file is part of PHPUnit Good Practices.
5+
*
6+
* (c) Dariusz Rumiński <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace PHPUnitGoodPractices\Tests;
13+
14+
use PHPUnit\Framework\Error\Warning;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnitGoodPractices\PositiveOverNegativeCountTrait;
17+
18+
/**
19+
* @covers \PHPUnitGoodPractices\PositiveOverNegativeCountTrait
20+
*/
21+
final class PositiveOverNegativeCountTraitTest extends TestCase
22+
{
23+
use PositiveOverNegativeCountTrait;
24+
25+
public $fixtureAttributePositive = array(11);
26+
public $fixtureAttributeZero = array();
27+
28+
public function expectException($exception)
29+
{
30+
if (is_callable(array('parent', 'expectException'))) {
31+
parent::expectException($exception);
32+
} else {
33+
$this->setExpectedException($exception);
34+
}
35+
}
36+
37+
/**
38+
* @param array $data
39+
* @param int $expected
40+
* @param bool $shouldCrash
41+
*
42+
* @dataProvider provideAssertCountCases
43+
*/
44+
public function testAssertCount($data, $expected, $shouldCrash)
45+
{
46+
if ($shouldCrash) {
47+
$this->expectException(Warning::class);
48+
}
49+
50+
$this->assertCount($expected, $data);
51+
}
52+
53+
public function provideAssertCountCases()
54+
{
55+
return array(
56+
array(array('foo', 'bar'), 2, false),
57+
array(array(), 0, false),
58+
array(array(), -2, true),
59+
);
60+
}
61+
62+
/**
63+
* @param array $data
64+
* @param int $expected
65+
* @param bool $shouldCrash
66+
*
67+
* @dataProvider provideAssertNotCountCases
68+
*/
69+
public function testAssertNotCount($data, $expected, $shouldCrash)
70+
{
71+
if ($shouldCrash) {
72+
$this->expectException(Warning::class);
73+
}
74+
75+
$this->assertNotCount($expected, $data);
76+
}
77+
78+
public function provideAssertNotCountCases()
79+
{
80+
return array(
81+
array(array('foo', 'bar'), -2, false),
82+
array(array(), 10, false),
83+
array(array(), -2, true),
84+
);
85+
}
86+
87+
/**
88+
* @param string $key
89+
* @param int $expected
90+
* @param bool $shouldCrash
91+
*
92+
* @dataProvider provideAssertAttributeCountCases
93+
*/
94+
public function testAssertAttributeCount($key, $expected, $shouldCrash)
95+
{
96+
if ($shouldCrash) {
97+
$this->expectException(Warning::class);
98+
}
99+
100+
$this->assertAttributeCount($expected, $key, $this);
101+
}
102+
103+
public function provideAssertAttributeCountCases()
104+
{
105+
return array(
106+
array('fixtureAttributePositive', count($this->fixtureAttributePositive), false),
107+
array('fixtureAttributeZero', count($this->fixtureAttributeZero), false),
108+
array('fixtureAttributeZero', -1, true),
109+
);
110+
}
111+
112+
/**
113+
* @param string $key
114+
* @param int $expected
115+
* @param bool $shouldCrash
116+
*
117+
* @dataProvider provideAssertAttributeCountCases
118+
*/
119+
public function testAssertAttributeNotCount($key, $expected, $shouldCrash)
120+
{
121+
if ($shouldCrash) {
122+
$this->expectException(Warning::class);
123+
}
124+
125+
$this->assertAttributeNotCount($expected, $key, $this);
126+
}
127+
128+
public function provideAssertAttributeNotCountCases()
129+
{
130+
return array(
131+
array('fixtureAttributePositive', 10 + count($this->fixtureAttributePositive), false),
132+
array('fixtureAttributeZero', 10 + count($this->fixtureAttributeZero), false),
133+
array('fixtureAttributeZero', -1, true),
134+
);
135+
}
136+
}

0 commit comments

Comments
 (0)