jaxon-php /
jaxon-mono
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Jaxon\Attributes\Tests\TestAttributes; |
||
| 4 | |||
| 5 | use Jaxon\Attributes\Tests\AttributeTrait; |
||
| 6 | use Jaxon\Attributes\Tests\Attr\Ajax\Attribute; |
||
| 7 | use Jaxon\Attributes\Tests\Attr\Ajax\ClassAttribute; |
||
| 8 | use Jaxon\Attributes\Tests\Attr\Ajax\ClassExcluded; |
||
| 9 | use Jaxon\Exception\SetupException; |
||
| 10 | use PHPUnit\Framework\TestCase; |
||
| 11 | |||
| 12 | use function Jaxon\Attributes\_register; |
||
| 13 | use function Jaxon\jaxon; |
||
| 14 | |||
| 15 | class AttributeTest extends TestCase |
||
| 16 | { |
||
| 17 | use AttributeTrait; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $sCacheDir; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @throws SetupException |
||
| 26 | */ |
||
| 27 | public function setUp(): void |
||
| 28 | { |
||
| 29 | $this->sCacheDir = __DIR__ . '/../cache'; |
||
| 30 | @mkdir($this->sCacheDir); |
||
|
0 ignored issues
–
show
|
|||
| 31 | |||
| 32 | jaxon()->di()->getPluginManager()->registerPlugins(); |
||
| 33 | _register(); |
||
| 34 | |||
| 35 | jaxon()->di()->val('jaxon_attributes_cache_dir', $this->sCacheDir); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @throws SetupException |
||
| 40 | */ |
||
| 41 | public function tearDown(): void |
||
| 42 | { |
||
| 43 | jaxon()->reset(); |
||
| 44 | parent::tearDown(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @throws SetupException |
||
| 49 | */ |
||
| 50 | public function testUploadAndExcludeAttribute() |
||
| 51 | { |
||
| 52 | $xMetadata = $this->getAttributes(Attribute::class, ['saveFiles', 'doNot']); |
||
| 53 | $bExcluded = $xMetadata->isExcluded(); |
||
| 54 | $aProperties = $xMetadata->getProperties(); |
||
| 55 | $aExcluded = $xMetadata->getExceptMethods(); |
||
| 56 | |||
| 57 | $this->assertFalse($bExcluded); |
||
| 58 | |||
| 59 | $this->assertCount(1, $aExcluded); |
||
| 60 | $this->assertEquals('doNot', $aExcluded[0]); |
||
| 61 | |||
| 62 | $this->assertCount(1, $aProperties); |
||
| 63 | $this->assertArrayHasKey('saveFiles', $aProperties); |
||
| 64 | $this->assertCount(1, $aProperties['saveFiles']); |
||
| 65 | $this->assertEquals("'user-files'", $aProperties['saveFiles']['upload']); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @throws SetupException |
||
| 70 | */ |
||
| 71 | public function testDatabagAttribute() |
||
| 72 | { |
||
| 73 | $xMetadata = $this->getAttributes(Attribute::class, ['withBags']); |
||
| 74 | $bExcluded = $xMetadata->isExcluded(); |
||
| 75 | $aProperties = $xMetadata->getProperties(); |
||
| 76 | $aExcluded = $xMetadata->getExceptMethods(); |
||
|
0 ignored issues
–
show
|
|||
| 77 | |||
| 78 | $this->assertFalse($bExcluded); |
||
| 79 | |||
| 80 | $this->assertCount(1, $aProperties); |
||
| 81 | $this->assertArrayHasKey('withBags', $aProperties); |
||
| 82 | $this->assertCount(1, $aProperties['withBags']); |
||
| 83 | $this->assertCount(2, $aProperties['withBags']['bags']); |
||
| 84 | $this->assertEquals('user.name', $aProperties['withBags']['bags'][0]); |
||
| 85 | $this->assertEquals('page.number', $aProperties['withBags']['bags'][1]); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @throws SetupException |
||
| 90 | */ |
||
| 91 | public function testCallbacksAttribute() |
||
| 92 | { |
||
| 93 | $xMetadata = $this->getAttributes(Attribute::class, ['cbSingle', 'cbMultiple', 'cbParams']); |
||
| 94 | $bExcluded = $xMetadata->isExcluded(); |
||
| 95 | $aProperties = $xMetadata->getProperties(); |
||
| 96 | $aExcluded = $xMetadata->getExceptMethods(); |
||
|
0 ignored issues
–
show
|
|||
| 97 | |||
| 98 | $this->assertFalse($bExcluded); |
||
| 99 | |||
| 100 | $this->assertCount(3, $aProperties); |
||
| 101 | $this->assertArrayHasKey('cbSingle', $aProperties); |
||
| 102 | $this->assertArrayHasKey('cbMultiple', $aProperties); |
||
| 103 | $this->assertArrayHasKey('cbParams', $aProperties); |
||
| 104 | |||
| 105 | $this->assertCount(1, $aProperties['cbSingle']['__before']); |
||
| 106 | $this->assertCount(2, $aProperties['cbMultiple']['__before']); |
||
| 107 | $this->assertCount(2, $aProperties['cbParams']['__before']); |
||
| 108 | $this->assertArrayHasKey('funcBefore', $aProperties['cbSingle']['__before']); |
||
| 109 | $this->assertArrayHasKey('funcBefore1', $aProperties['cbMultiple']['__before']); |
||
| 110 | $this->assertArrayHasKey('funcBefore2', $aProperties['cbMultiple']['__before']); |
||
| 111 | $this->assertArrayHasKey('funcBefore1', $aProperties['cbParams']['__before']); |
||
| 112 | $this->assertArrayHasKey('funcBefore2', $aProperties['cbParams']['__before']); |
||
| 113 | $this->assertIsArray($aProperties['cbSingle']['__before']['funcBefore']); |
||
| 114 | $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore1']); |
||
| 115 | $this->assertIsArray($aProperties['cbMultiple']['__before']['funcBefore2']); |
||
| 116 | $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore1']); |
||
| 117 | $this->assertIsArray($aProperties['cbParams']['__before']['funcBefore2']); |
||
| 118 | |||
| 119 | $this->assertCount(1, $aProperties['cbSingle']['__after']); |
||
| 120 | $this->assertCount(3, $aProperties['cbMultiple']['__after']); |
||
| 121 | $this->assertCount(1, $aProperties['cbParams']['__after']); |
||
| 122 | $this->assertArrayHasKey('funcAfter', $aProperties['cbSingle']['__after']); |
||
| 123 | $this->assertArrayHasKey('funcAfter1', $aProperties['cbMultiple']['__after']); |
||
| 124 | $this->assertArrayHasKey('funcAfter2', $aProperties['cbMultiple']['__after']); |
||
| 125 | $this->assertArrayHasKey('funcAfter3', $aProperties['cbMultiple']['__after']); |
||
| 126 | $this->assertArrayHasKey('funcAfter1', $aProperties['cbParams']['__after']); |
||
| 127 | $this->assertIsArray($aProperties['cbSingle']['__after']['funcAfter']); |
||
| 128 | $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter1']); |
||
| 129 | $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter2']); |
||
| 130 | $this->assertIsArray($aProperties['cbMultiple']['__after']['funcAfter3']); |
||
| 131 | $this->assertIsArray($aProperties['cbParams']['__after']['funcAfter1']); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @throws SetupException |
||
| 136 | */ |
||
| 137 | public function testContainerAttribute() |
||
| 138 | { |
||
| 139 | $xMetadata = $this->getAttributes(Attribute::class, ['di1', 'di2']); |
||
| 140 | $bExcluded = $xMetadata->isExcluded(); |
||
| 141 | $aProperties = $xMetadata->getProperties(); |
||
| 142 | $aExcluded = $xMetadata->getExceptMethods(); |
||
|
0 ignored issues
–
show
|
|||
| 143 | |||
| 144 | $this->assertFalse($bExcluded); |
||
| 145 | |||
| 146 | $this->assertCount(2, $aProperties); |
||
| 147 | $this->assertArrayHasKey('di1', $aProperties); |
||
| 148 | $this->assertArrayHasKey('di2', $aProperties); |
||
| 149 | $this->assertCount(2, $aProperties['di1']['__di']); |
||
| 150 | $this->assertCount(2, $aProperties['di2']['__di']); |
||
| 151 | $this->assertEquals('Jaxon\Attributes\Tests\Service\ColorService', $aProperties['di1']['__di']['colorService']); |
||
| 152 | $this->assertEquals('Jaxon\Attributes\Tests\Attr\Ajax\FontService', $aProperties['di1']['__di']['fontService']); |
||
| 153 | $this->assertEquals('Jaxon\Attributes\Tests\Service\ColorService', $aProperties['di2']['__di']['colorService']); |
||
| 154 | $this->assertEquals('Jaxon\Attributes\Tests\Service\TextService', $aProperties['di2']['__di']['textService']); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @throws SetupException |
||
| 159 | */ |
||
| 160 | public function testClassAttribute() |
||
| 161 | { |
||
| 162 | $xMetadata = $this->getAttributes(ClassAttribute::class, []); |
||
| 163 | $bExcluded = $xMetadata->isExcluded(); |
||
| 164 | $aProperties = $xMetadata->getProperties(); |
||
| 165 | $aExcluded = $xMetadata->getExceptMethods(); |
||
|
0 ignored issues
–
show
|
|||
| 166 | |||
| 167 | $this->assertFalse($bExcluded); |
||
| 168 | |||
| 169 | $this->assertCount(1, $aProperties); |
||
| 170 | $this->assertArrayHasKey('*', $aProperties); |
||
| 171 | $this->assertCount(5, $aProperties['*']); |
||
| 172 | $this->assertArrayHasKey('bags', $aProperties['*']); |
||
| 173 | $this->assertArrayHasKey('callback', $aProperties['*']); |
||
| 174 | $this->assertArrayHasKey('__before', $aProperties['*']); |
||
| 175 | $this->assertArrayHasKey('__after', $aProperties['*']); |
||
| 176 | $this->assertArrayHasKey('__di', $aProperties['*']); |
||
| 177 | |||
| 178 | $this->assertCount(2, $aProperties['*']['bags']); |
||
| 179 | $this->assertEquals('user.name', $aProperties['*']['bags'][0]); |
||
| 180 | $this->assertEquals('page.number', $aProperties['*']['bags'][1]); |
||
| 181 | |||
| 182 | $this->assertCount(2, $aProperties['*']['__before']); |
||
| 183 | $this->assertArrayHasKey('funcBefore1', $aProperties['*']['__before']); |
||
| 184 | $this->assertArrayHasKey('funcBefore2', $aProperties['*']['__before']); |
||
| 185 | $this->assertIsArray($aProperties['*']['__before']['funcBefore1']); |
||
| 186 | $this->assertIsArray($aProperties['*']['__before']['funcBefore2']); |
||
| 187 | |||
| 188 | $this->assertCount(3, $aProperties['*']['__after']); |
||
| 189 | $this->assertArrayHasKey('funcAfter1', $aProperties['*']['__after']); |
||
| 190 | $this->assertArrayHasKey('funcAfter2', $aProperties['*']['__after']); |
||
| 191 | $this->assertArrayHasKey('funcAfter3', $aProperties['*']['__after']); |
||
| 192 | $this->assertIsArray($aProperties['*']['__after']['funcAfter1']); |
||
| 193 | $this->assertIsArray($aProperties['*']['__after']['funcAfter2']); |
||
| 194 | $this->assertIsArray($aProperties['*']['__after']['funcAfter3']); |
||
| 195 | |||
| 196 | $this->assertCount(3, $aProperties['*']['__di']); |
||
| 197 | $this->assertArrayHasKey('colorService', $aProperties['*']['__di']); |
||
| 198 | $this->assertArrayHasKey('textService', $aProperties['*']['__di']); |
||
| 199 | $this->assertArrayHasKey('fontService', $aProperties['*']['__di']); |
||
| 200 | $this->assertEquals('Jaxon\Attributes\Tests\Service\ColorService', $aProperties['*']['__di']['colorService']); |
||
| 201 | $this->assertEquals('Jaxon\Attributes\Tests\Service\TextService', $aProperties['*']['__di']['textService']); |
||
| 202 | $this->assertEquals('Jaxon\Attributes\Tests\Attr\Ajax\FontService', $aProperties['*']['__di']['fontService']); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @throws SetupException |
||
| 207 | */ |
||
| 208 | public function testClassExcludeAttribute() |
||
| 209 | { |
||
| 210 | $xMetadata = $this->getAttributes(ClassExcluded::class, ['doNot', 'withBags', 'cbSingle']); |
||
| 211 | $bExcluded = $xMetadata->isExcluded(); |
||
| 212 | $aProperties = $xMetadata->getProperties(); |
||
| 213 | $aExcluded = $xMetadata->getExceptMethods(); |
||
| 214 | |||
| 215 | $xData = $xMetadata->exclude(); |
||
| 216 | $this->assertTrue($xData->getValue()); |
||
| 217 | $this->assertEquals('protected', $xData->getName()); |
||
| 218 | |||
| 219 | $this->assertTrue($bExcluded); |
||
| 220 | $this->assertEmpty($aProperties); |
||
| 221 | $this->assertEmpty($aExcluded); |
||
| 222 | } |
||
| 223 | |||
| 224 | public function testExcludeAttributeError() |
||
| 225 | { |
||
| 226 | $this->expectException(SetupException::class); |
||
| 227 | $this->getAttributes(Attribute::class, ['doNotError']); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function testDatabagAttributeError() |
||
| 231 | { |
||
| 232 | $this->expectException(SetupException::class); |
||
| 233 | $this->getAttributes(Attribute::class, ['withBagsError']); |
||
| 234 | } |
||
| 235 | |||
| 236 | public function testUploadAttributeWrongName() |
||
| 237 | { |
||
| 238 | $this->expectException(SetupException::class); |
||
| 239 | $this->getAttributes(Attribute::class, ['saveFilesWrongName']); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function testUploadAttributeMultiple() |
||
| 243 | { |
||
| 244 | $this->expectException(SetupException::class); |
||
| 245 | $this->getAttributes(Attribute::class, ['saveFilesMultiple']); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function testCallbacksBeforeAttributeNoCall() |
||
| 249 | { |
||
| 250 | $this->expectException(SetupException::class); |
||
| 251 | $this->getAttributes(Attribute::class, ['cbBeforeNoCall']); |
||
| 252 | } |
||
| 253 | |||
| 254 | public function testCallbacksBeforeAttributeUnknownAttr() |
||
| 255 | { |
||
| 256 | $this->expectException(SetupException::class); |
||
| 257 | $this->getAttributes(Attribute::class, ['cbBeforeUnknownAttr']); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function testCallbacksBeforeAttributeWrongAttrType() |
||
| 261 | { |
||
| 262 | $this->expectException(SetupException::class); |
||
| 263 | $this->getAttributes(Attribute::class, ['cbBeforeWrongAttrType']); |
||
| 264 | } |
||
| 265 | |||
| 266 | public function testCallbacksAfterAttributeNoCall() |
||
| 267 | { |
||
| 268 | $this->expectException(SetupException::class); |
||
| 269 | $this->getAttributes(Attribute::class, ['cbAfterNoCall']); |
||
| 270 | } |
||
| 271 | |||
| 272 | public function testCallbacksAfterAttributeUnknownAttr() |
||
| 273 | { |
||
| 274 | $this->expectException(SetupException::class); |
||
| 275 | $this->getAttributes(Attribute::class, ['cbAfterUnknownAttr']); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function testCallbacksAfterAttributeWrongAttrType() |
||
| 279 | { |
||
| 280 | $this->expectException(SetupException::class); |
||
| 281 | $this->getAttributes(Attribute::class, ['cbAfterWrongAttrType']); |
||
| 282 | } |
||
| 283 | |||
| 284 | public function testContainerAttributeUnknownAttr() |
||
| 285 | { |
||
| 286 | $this->expectException(SetupException::class); |
||
| 287 | $this->getAttributes(Attribute::class, ['diUnknownAttr']); |
||
| 288 | } |
||
| 289 | |||
| 290 | public function testContainerAttributeWrongAttrType() |
||
| 291 | { |
||
| 292 | $this->expectException(SetupException::class); |
||
| 293 | $this->getAttributes(Attribute::class, ['diWrongAttrType']); |
||
| 294 | } |
||
| 295 | |||
| 296 | public function testContainerAttributeWrongClassType() |
||
| 297 | { |
||
| 298 | $this->expectException(SetupException::class); |
||
| 299 | $this->getAttributes(Attribute::class, ['diWrongClassType']); |
||
| 300 | } |
||
| 301 | } |
||
| 302 |
If you suppress an error, we recommend checking for the error condition explicitly: