|
12 | 12 | use ReflectionMethod; |
13 | 13 | use Zend\Code\Generator\ClassGenerator; |
14 | 14 | use Zend\Code\Generator\DocBlockGenerator; |
| 15 | +use Zend\Code\Generator\Exception\InvalidArgumentException; |
15 | 16 | use Zend\Code\Generator\PropertyGenerator; |
16 | 17 | use Zend\Code\Generator\MethodGenerator; |
17 | 18 | use Zend\Code\Reflection\ClassReflection; |
@@ -563,16 +564,69 @@ public function testAddConstantThrowsExceptionWithInvalidName() |
563 | 564 | $classGenerator->addConstant([], 'value1'); |
564 | 565 | } |
565 | 566 |
|
566 | | - /** |
567 | | - * @group 6274 |
568 | | - */ |
569 | | - public function testAddConstantThrowsExceptionWithInvalidValue() |
| 567 | + public function testAddConstantThrowsExceptionWithEmptyConstantName() |
570 | 568 | { |
571 | | - $this->setExpectedException('InvalidArgumentException'); |
| 569 | + $classGenerator = new ClassGenerator(); |
| 570 | + |
| 571 | + $this->setExpectedException(InvalidArgumentException::class); |
| 572 | + |
| 573 | + $classGenerator->addConstant('', 'value'); |
| 574 | + } |
| 575 | + |
| 576 | + public function testAddConstantAcceptsMixedScalars() |
| 577 | + { |
| 578 | + $classGenerator = new ClassGenerator(); |
| 579 | + |
| 580 | + $classGenerator->addConstant('a', 'v'); |
| 581 | + $classGenerator->addConstant('b', 123); |
| 582 | + $classGenerator->addConstant('c', 123.456); |
| 583 | + $classGenerator->addConstant('d', []); |
| 584 | + $classGenerator->addConstant('e', ['v1' => 'v2']); |
| 585 | + $classGenerator->addConstant('f', ['v1' => ['v2' => 'v3']]); |
| 586 | + $classGenerator->addConstant('g', null); |
| 587 | + |
| 588 | + $this->assertEquals('v', $classGenerator->getConstant('a')->getDefaultValue()->getValue()); |
| 589 | + $this->assertEquals(123, $classGenerator->getConstant('b')->getDefaultValue()->getValue()); |
| 590 | + $this->assertEquals(123.456, $classGenerator->getConstant('c')->getDefaultValue()->getValue()); |
| 591 | + $this->assertEquals([], $classGenerator->getConstant('d')->getDefaultValue()->getValue()); |
| 592 | + $this->assertEquals(['v1' => 'v2'], $classGenerator->getConstant('e')->getDefaultValue()->getValue()); |
| 593 | + $this->assertEquals(['v1' => ['v2' => 'v3']], $classGenerator->getConstant('f')->getDefaultValue()->getValue()); |
| 594 | + $this->assertEquals(null, $classGenerator->getConstant('g')->getDefaultValue()->getValue()); |
| 595 | + } |
| 596 | + |
| 597 | + public function testAddConstantRejectsObjectConstantValue() |
| 598 | + { |
| 599 | + $classGenerator = new ClassGenerator(); |
| 600 | + |
| 601 | + $this->setExpectedException(InvalidArgumentException::class); |
| 602 | + |
| 603 | + $classGenerator->addConstant('a', new \stdClass()); |
| 604 | + } |
572 | 605 |
|
| 606 | + public function testAddConstantRejectsResourceConstantValue() |
| 607 | + { |
573 | 608 | $classGenerator = new ClassGenerator(); |
574 | 609 |
|
575 | | - $classGenerator->addConstant('x', null); |
| 610 | + $resource = fopen('php://memory', 'r'); |
| 611 | + |
| 612 | + try { |
| 613 | + $classGenerator->addConstant('a', $resource); |
| 614 | + |
| 615 | + $this->fail('Not supposed to be reached'); |
| 616 | + } catch (InvalidArgumentException $e) { |
| 617 | + $this->assertEmpty($classGenerator->getConstants()); |
| 618 | + } finally { |
| 619 | + fclose($resource); |
| 620 | + } |
| 621 | + } |
| 622 | + |
| 623 | + public function testAddConstantRejectsArrayWithInvalidNestedValue() |
| 624 | + { |
| 625 | + $classGenerator = new ClassGenerator(); |
| 626 | + |
| 627 | + $this->setExpectedException(InvalidArgumentException::class); |
| 628 | + |
| 629 | + $classGenerator->addConstant('a', [new \stdClass()]); |
576 | 630 | } |
577 | 631 |
|
578 | 632 | /** |
|
0 commit comments