| Total Complexity | 53 |
| Total Lines | 271 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like GroupTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GroupTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class GroupTest extends TestCase |
||
| 14 | { |
||
| 15 | public function testConstructorWithAllParameters(): void |
||
| 16 | { |
||
| 17 | $clipPath = 'circle(50%)'; |
||
| 18 | $data = ['key1' => 'value1', 'key2' => 'value2']; |
||
| 19 | $item = $this->createMock(AVGItem::class); |
||
| 20 | $items = [ |
||
| 21 | $this->createMock(AVGItem::class), |
||
| 22 | $this->createMock(AVGItem::class), |
||
| 23 | ]; |
||
| 24 | $opacity = 0.8; |
||
| 25 | $transform = 'translate(10, 20)'; |
||
| 26 | |||
| 27 | $group = new Group($clipPath, $data, $item, $items, $opacity, $transform); |
||
| 28 | |||
| 29 | $this->assertSame($clipPath, $group->clipPath); |
||
| 30 | $this->assertSame($data, $group->data); |
||
| 31 | $this->assertSame($item, $group->item); |
||
| 32 | $this->assertSame($items, $group->items); |
||
| 33 | $this->assertSame($opacity, $group->opacity); |
||
| 34 | $this->assertSame($transform, $group->transform); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function testConstructorWithDefaultParameters(): void |
||
| 47 | } |
||
| 48 | |||
| 49 | public function testJsonSerializeWithAllProperties(): void |
||
| 50 | { |
||
| 51 | $clipPath = 'rect(10%, 20%, 30%, 40%)'; |
||
| 52 | $data = ['width' => 100, 'height' => 200]; |
||
| 53 | $item = $this->createMock(AVGItem::class); |
||
| 54 | $items = [ |
||
| 55 | $this->createMock(AVGItem::class), |
||
| 56 | $this->createMock(AVGItem::class), |
||
| 57 | ]; |
||
| 58 | $opacity = 0.5; |
||
| 59 | $transform = 'scale(2, 2)'; |
||
| 60 | |||
| 61 | $group = new Group($clipPath, $data, $item, $items, $opacity, $transform); |
||
| 62 | $result = $group->jsonSerialize(); |
||
| 63 | |||
| 64 | $this->assertSame(AVGItemType::GROUP->value, $result['type']); |
||
| 65 | $this->assertSame($clipPath, $result['clipPath']); |
||
| 66 | $this->assertSame($data, $result['data']); |
||
| 67 | $this->assertSame($item, $result['item']); |
||
| 68 | $this->assertSame($items, $result['items']); |
||
| 69 | $this->assertSame($opacity, $result['opacity']); |
||
| 70 | $this->assertSame($transform, $result['transform']); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function testJsonSerializeWithNullValues(): void |
||
| 74 | { |
||
| 75 | $group = new Group(); |
||
| 76 | $result = $group->jsonSerialize(); |
||
| 77 | |||
| 78 | $this->assertSame(AVGItemType::GROUP->value, $result['type']); |
||
| 79 | $this->assertArrayNotHasKey('clipPath', $result); |
||
| 80 | $this->assertArrayNotHasKey('data', $result); |
||
| 81 | $this->assertArrayNotHasKey('item', $result); |
||
| 82 | $this->assertArrayNotHasKey('items', $result); |
||
| 83 | $this->assertArrayNotHasKey('opacity', $result); |
||
| 84 | $this->assertArrayNotHasKey('transform', $result); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function testJsonSerializeWithEmptyData(): void |
||
| 88 | { |
||
| 89 | $group = new Group(data: []); |
||
| 90 | $result = $group->jsonSerialize(); |
||
| 91 | |||
| 92 | $this->assertArrayNotHasKey('data', $result); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testJsonSerializeWithEmptyItems(): void |
||
| 101 | } |
||
| 102 | |||
| 103 | public function testJsonSerializeWithPartialProperties(): void |
||
| 104 | { |
||
| 105 | $group = new Group( |
||
| 106 | clipPath: 'polygon(0% 0%, 100% 0%, 50% 100%)', |
||
| 107 | opacity: 1.0 |
||
| 108 | ); |
||
| 109 | $result = $group->jsonSerialize(); |
||
| 110 | |||
| 111 | $this->assertSame(AVGItemType::GROUP->value, $result['type']); |
||
| 112 | $this->assertSame('polygon(0% 0%, 100% 0%, 50% 100%)', $result['clipPath']); |
||
| 113 | $this->assertSame(1.0, $result['opacity']); |
||
| 114 | $this->assertArrayNotHasKey('data', $result); |
||
| 115 | $this->assertArrayNotHasKey('item', $result); |
||
| 116 | $this->assertArrayNotHasKey('items', $result); |
||
| 117 | $this->assertArrayNotHasKey('transform', $result); |
||
| 118 | } |
||
| 119 | |||
| 120 | public function testJsonSerializeWithZeroOpacity(): void |
||
| 121 | { |
||
| 122 | $group = new Group(opacity: 0.0); |
||
| 123 | $result = $group->jsonSerialize(); |
||
| 124 | |||
| 125 | $this->assertArrayHasKey('opacity', $result); |
||
| 126 | $this->assertSame(0.0, $result['opacity']); |
||
| 127 | } |
||
| 128 | |||
| 129 | public function testTypeConstant(): void |
||
| 130 | { |
||
| 131 | $this->assertSame(AVGItemType::GROUP, Group::TYPE); |
||
| 132 | } |
||
| 133 | |||
| 134 | public function testExtendsAVGItem(): void |
||
| 135 | { |
||
| 136 | $group = new Group(); |
||
| 137 | |||
| 138 | $this->assertInstanceOf(AVGItem::class, $group); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function testImplementsJsonSerializable(): void |
||
| 142 | { |
||
| 143 | $group = new Group(); |
||
| 144 | |||
| 145 | $this->assertInstanceOf(\JsonSerializable::class, $group); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Ensure every public property inherited from AVGItem (and present on Group) is serialized |
||
| 150 | * when assigned a non-default non-empty value, using type-aware assignments to avoid |
||
| 151 | * invalid TypeErrors (e.g. assigning string to ?array). |
||
| 152 | */ |
||
| 153 | public function testJsonSerializeIncludesAllAVGItemBaseProperties(): void |
||
| 154 | { |
||
| 155 | $group = new Group(); |
||
| 156 | |||
| 157 | $reflection = new \ReflectionObject($group); |
||
| 158 | $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC); |
||
| 159 | $skip = ['type']; // type always handled by base |
||
| 160 | |||
| 161 | $assigned = []; |
||
| 162 | |||
| 163 | foreach ($properties as $prop) { |
||
| 164 | $name = $prop->getName(); |
||
| 165 | if (in_array($name, $skip, true)) { |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Special handling for typed AVGItem property (object mock). |
||
| 170 | if ($name === 'item') { |
||
| 171 | $value = $this->createMock(AVGItem::class); |
||
| 172 | $group->$name = $value; |
||
| 173 | $assigned[$name] = $value; |
||
| 174 | continue; |
||
| 175 | } |
||
| 176 | |||
| 177 | // Special handling for AVGFilter-typed property. |
||
| 178 | if ($name === 'filter') { |
||
| 179 | $value = $this->createMock(AVGFilter::class); |
||
| 180 | $group->$name = $value; |
||
| 181 | $assigned[$name] = $value; |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | |||
| 185 | $current = $group->$name ?? null; |
||
| 186 | $value = $this->generateValueForProperty($prop, $current, $name); |
||
| 187 | |||
| 188 | // If generator decides to skip (returns a sentinel null and property already null) we move on. |
||
| 189 | if ($value !== null) { |
||
| 190 | $group->$name = $value; |
||
| 191 | $assigned[$name] = $value; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | $json = $group->jsonSerialize(); |
||
| 196 | $this->assertSame(AVGItemType::GROUP->value, $json['type']); |
||
| 197 | |||
| 198 | foreach ($assigned as $prop => $value) { |
||
| 199 | // Skip default-like values that component intentionally filters out |
||
| 200 | if ($value === null) { |
||
| 201 | continue; |
||
| 202 | } |
||
| 203 | if (is_array($value) && $value === []) { |
||
| 204 | continue; |
||
| 205 | } |
||
| 206 | if ((is_int($value) && $value === 0) || (is_string($value) && $value === '') || (is_float($value) && $value === 0.0)) { |
||
| 207 | continue; |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->assertArrayHasKey($prop, $json, "Expected property '$prop' to be serialized after assignment."); |
||
| 211 | if (array_key_exists($prop, $json)) { |
||
| 212 | $this->assertSame($value, $json[$prop], "Serialized value mismatch for property '$prop'"); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Generate a type-compatible non-default value for the given property. |
||
| 219 | * |
||
| 220 | * @return mixed |
||
| 221 | */ |
||
| 222 | private function generateValueForProperty(\ReflectionProperty $prop, mixed $current, string $name): mixed |
||
| 284 | } |
||
| 285 | } |
||
| 286 |