1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Testing;
13:
14: use Chromabits\Nucleus\Exceptions\LackOfCoffeeException;
15: use Chromabits\Nucleus\Meditation\Primitives\CompoundTypes;
16: use PHPUnit_Framework_TestCase as BaseTestCase;
17:
18: /**
19: * Class TestCase.
20: *
21: * A base test case with some extra assertions
22: *
23: * @author Eduardo Trujillo <ed@chromabits.com>
24: * @package Chromabits\Nucleus\Testing
25: */
26: abstract class TestCase extends BaseTestCase
27: {
28: /**
29: * Asserts that a variable is of a given type.
30: *
31: * If $expected is an array, each string value will be used in a type
32: * check. This is useful for checking if a class also implements certain
33: * interfaces.
34: *
35: * @param string|array $expected
36: * @param mixed $actual
37: * @param string $message
38: */
39: public static function assertInstanceOf($expected, $actual, $message = '')
40: {
41: if (is_array($expected)) {
42: foreach ($expected as $expectedSingle) {
43: parent::assertInstanceOf($expectedSingle, $actual, $message);
44: }
45:
46: return;
47: }
48:
49: parent::assertInstanceOf($expected, $actual, $message);
50: }
51:
52: /**
53: * Run assert equals with an input matrix.
54: *
55: * Every entry should be formatted as following:
56: *
57: * [$expected, $equals, $message (optional)]
58: *
59: * @param array $comparisons
60: *
61: * @throws LackOfCoffeeException
62: */
63: public static function assertEqualsMatrix(array $comparisons)
64: {
65: $total = count($comparisons);
66:
67: foreach ($comparisons as $index => $comparison) {
68: if (count($comparison) < 2) {
69: throw new LackOfCoffeeException('Comparison entry is invalid.');
70: }
71:
72: if (array_key_exists(2, $comparison)) {
73: $message = $comparison[2];
74: } else {
75: $message = vsprintf(
76: 'Comparison %d (of %d) is expected to be equal.',
77: [$index + 1, $total]
78: );
79: }
80:
81: static::assertEquals(
82: $comparison[0],
83: $comparison[1],
84: $message
85: );
86: }
87: }
88:
89: /**
90: * Assert that the provided value is an array.
91: *
92: * @param mixed $actual
93: * @param string $message
94: */
95: public static function assertIsArray($actual, $message = '')
96: {
97: static::assertInternalType(
98: CompoundTypes::COMPOUND_ARRAY,
99: $actual,
100: $message
101: );
102: }
103:
104: /**
105: * Assert that an object has all attributes in an array.
106: *
107: * @param array $attributes
108: * @param mixed $object
109: * @param string $message
110: */
111: public function assertObjectHasAttributes(
112: array $attributes,
113: $object,
114: $message = ''
115: ) {
116: foreach ($attributes as $attr) {
117: $this->assertObjectHasAttribute($attr, $object, $message);
118: }
119: }
120: }
121: