From 031f8900cb722831a2da979aa0778a033f6bac74 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sat, 9 May 2020 21:50:30 +0300 Subject: [PATCH] Provide tests for #2089 Closes: https://github.com/phalcon/zephir/pull/2090 --- stub/oo/propertyaccess.zep | 74 ++++++++++--------- stub/properties/protectedproperties.zep | 38 +++++++++- tests/Extension/FcallTest.php | 16 +--- .../Extension/Oo/Scopes/PrivateScopeTest.php | 37 +++++++--- .../Properties/PrivatePropertiesTest.php | 7 +- .../Properties/ProtectedPropertiesTest.php | 49 +++++++++--- .../CompilerFile/CheckDependenciesTest.php | 2 +- .../mocks/ExtendsProtectedProperties.php | 19 +++++ tests/fixtures/mocks/UserExample.php | 16 ++++ 9 files changed, 181 insertions(+), 77 deletions(-) create mode 100644 tests/fixtures/mocks/ExtendsProtectedProperties.php create mode 100644 tests/fixtures/mocks/UserExample.php diff --git a/stub/oo/propertyaccess.zep b/stub/oo/propertyaccess.zep index 3884375688..3aa0c4ad56 100644 --- a/stub/oo/propertyaccess.zep +++ b/stub/oo/propertyaccess.zep @@ -1,42 +1,44 @@ -/** - * Class with constructor + params - */ - namespace Stub\Oo; class PropertyAccess { - protected a; - - protected b; - - private privateVariable; - - public function __construct() - { - var test, test1; - let test = new \stdClass; - let test->test = ["1", 2, 3, 5, 6.00]; - let test->test2 = test->test[0]; - let test->test3 = test->test[1]; - let test->test3 = [test->test[1], 1, 2, 3]; - - let this->a = test; - let this->b = this->a->test2; - - let test1 = this->a; - } - - public Function setPrivatevariable(value) - { - let this->privateVariable = value; - - return this; - } - - public Function getPrivatevariable() - { - return this->privateVariable; - } + protected a; + + protected b; + + private privateVariable; + + public function __construct() + { + var test, test1; + let test = new \stdClass; + let test->test = ["1", 2, 3, 5, 6.00]; + let test->test2 = test->test[0]; + let test->test3 = test->test[1]; + let test->test3 = [test->test[1], 1, 2, 3]; + + let this->a = test; + let this->b = this->a->test2; + + let test1 = this->a; + } + + /** + * @issue https://github.com/phalcon/zephir/issues/1851 + */ + public function setPrivatevariable(value) -> + { + let this->privateVariable = value; + + return this; + } + + /** + * @issue https://github.com/phalcon/zephir/issues/1851 + */ + public function getPrivatevariable() -> var + { + return this->privateVariable; + } } diff --git a/stub/properties/protectedproperties.zep b/stub/properties/protectedproperties.zep index c2c3121ebe..f6bed785c5 100644 --- a/stub/properties/protectedproperties.zep +++ b/stub/properties/protectedproperties.zep @@ -1,9 +1,11 @@ namespace Stub\Properties; +/** + * Stub\Properties\ProtectedProperties + */ class ProtectedProperties { - /** * This is a protected property with no initial value */ @@ -39,46 +41,78 @@ class ProtectedProperties */ protected someString = "test"; + /** + * @var null|mixed + */ protected someVar {set, get}; + /** + * This is a property to test default value on extends + * @var array + */ + protected someArrayVar = [] { set, get }; + + /** + * @return null|mixed + */ public function getSomeNull() { return this->someNull; } + /** + * @return void + */ public function setSomeNull(var param) { let this->someNull = param; } + /** + * @return null + */ public function getSomeNullInitial() { return this->someNullInitial; } + /** + * @return bool + */ public function getSomeFalse() { return this->someFalse; } + /** + * @return bool + */ public function getSomeTrue() { return this->someTrue; } + /** + * @return int + */ public function getSomeInteger() { return this->someInteger; } + /** + * @return float + */ public function getSomeDouble() { return this->someDouble; } + /** + * @return string + */ public function getSomeString() { return this->someString; } - } diff --git a/tests/Extension/FcallTest.php b/tests/Extension/FcallTest.php index 8aa0a96389..8be03f8bef 100644 --- a/tests/Extension/FcallTest.php +++ b/tests/Extension/FcallTest.php @@ -5,8 +5,8 @@ * * (c) Phalcon Team * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. */ namespace Extension; @@ -15,10 +15,6 @@ use Stub\Fcall; use Stub\Oo\PropertyAccess; -class UserExample extends PropertyAccess -{ -} - class FcallTest extends TestCase { /** @var Fcall */ @@ -121,12 +117,4 @@ public function testFunctionDeclaration() $this->assertInstanceOf(\stdClass::class, \Stub\zephir_namespaced_method_with_type_casting(new \stdClass())); $this->assertInstanceOf(\stdClass::class, zephir_global_method_with_type_casting(new \stdClass())); } - - public function testIssue1851() - { - $example = new UserExample(); - $example->setPrivateVariable('test'); - - $this->assertSame('test', $example->getPrivateVariable()); - } } diff --git a/tests/Extension/Oo/Scopes/PrivateScopeTest.php b/tests/Extension/Oo/Scopes/PrivateScopeTest.php index 025484dfc5..b85a1fab43 100644 --- a/tests/Extension/Oo/Scopes/PrivateScopeTest.php +++ b/tests/Extension/Oo/Scopes/PrivateScopeTest.php @@ -201,11 +201,11 @@ public function shouldNotSetPrivatePropertyExtendedMagicObjInternal() $tester->setPropertyObj($object, 'privateProperty2', 'CHANGED'); // This related the way PHP < 7.4 handles object's properties when - // there is a magic __set method present. + // there is a magic __set method present. // - // Actually we DO NOT change property here (fixed). Only PHP 7.4 - // throws a Fatal Error. All previous versions just out a Notice and - // continue execution. + // Actually we DO NOT change property here (fixed). However, only + // PHP 7.4 throws a fatal error. All previous versions just out a + // notice and continue to execution. if (\PHP_VERSION_ID < 70400) { $this->assertEquals('private', $object->getPrivateProperty2()); } @@ -233,11 +233,11 @@ public function shouldNotSetPrivatePropertyExtendedMagicNewInternal() ); // This related the way PHP < 7.4 handles object's properties when - // there is a magic __set method present. + // there is a magic __set method present. // - // Actually we DO NOT change property here (fixed). Only PHP 7.4 - // throws a Fatal Error. All previous versions just out a Notice and - // continue execution. + // Actually we DO NOT change property here (fixed). However, only + // PHP 7.4 throws a fatal error. All previous versions just out a + // notice and continue to execution. if (\PHP_VERSION_ID < 70400) { $this->assertEquals('private', $object->getPrivateProperty2()); } @@ -290,11 +290,11 @@ public function shouldNotSetPrivatePropertyExtendedMagicNewPhp() ); // This related the way PHP < 7.4 handles object's properties when - // there is a magic __set method present. + // there is a magic __set method present. // - // Actually we DO NOT change property here (fixed). Only PHP 7.4 - // throws a Fatal Error. All previous versions just out a Notice and - // continue execution. + // Actually we DO NOT change property here (fixed). However, only + // PHP 7.4 throws a fatal error. All previous versions just out a + // notice and continue to execution. if (\PHP_VERSION_ID < 70400) { $this->assertEquals('private2', $object->getPrivateProperty2()); } @@ -353,4 +353,17 @@ public function shouldNotGetObjectVarsPrivatePropertyNewInternal() $this->assertArrayNotHasKey('privateProperty', $objectVars); } + + /** + * @test + * + * @see https://github.com/phalcon/zephir/issues/1851 + */ + public function shouldGetAndSetPrivatePropertyUsingParentGetterAndSetter() + { + $tester = new \UserExample(); + $tester->setPrivateVariable('test'); + + $this->assertEquals('test', $tester->getPrivateVariable()); + } } diff --git a/tests/Extension/Properties/PrivatePropertiesTest.php b/tests/Extension/Properties/PrivatePropertiesTest.php index 982a990bb3..a15e5dbbbc 100644 --- a/tests/Extension/Properties/PrivatePropertiesTest.php +++ b/tests/Extension/Properties/PrivatePropertiesTest.php @@ -5,19 +5,20 @@ * * (c) Phalcon Team * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. */ namespace Extension\Properties; use PHPUnit\Framework\TestCase; +use Stub\Properties\PrivateProperties; class PrivatePropertiesTest extends TestCase { public function testAssertations() { - $test = new \Stub\Properties\PrivateProperties(); + $test = new PrivateProperties(); $this->assertNull($test->getSomeNull()); $this->assertNull($test->getSomeNullInitial()); diff --git a/tests/Extension/Properties/ProtectedPropertiesTest.php b/tests/Extension/Properties/ProtectedPropertiesTest.php index 5704a81073..b931816528 100644 --- a/tests/Extension/Properties/ProtectedPropertiesTest.php +++ b/tests/Extension/Properties/ProtectedPropertiesTest.php @@ -5,29 +5,60 @@ * * (c) Phalcon Team * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. */ namespace Extension\Properties; use PHPUnit\Framework\TestCase; +use Stub\Properties\ProtectedProperties; class ProtectedPropertiesTest extends TestCase { - public function testAssertations() + /** @test */ + public function shouldGetDefaultValueOfProtectedPropertiesUsingGetters() { - $test = new \Stub\Properties\ProtectedProperties(); + $test = new ProtectedProperties(); $this->assertNull($test->getSomeNull()); $this->assertNull($test->getSomeNullInitial()); + $this->assertFalse($test->getSomeFalse()); $this->assertTrue($test->getSomeTrue()); - $this->assertSame($test->getSomeInteger(), 10); - $this->assertSame($test->getSomeDouble(), 10.25); - $this->assertSame($test->getSomeString(), 'test'); - $test->setSomeVar(($rand = rand(1, 1000) * 100)); - $this->assertSame($test->getSomeVar(), $rand); + $this->assertEquals(10, $test->getSomeInteger()); + $this->assertEquals(10.25, $test->getSomeDouble()); + + $this->assertEquals('test', $test->getSomeString()); + } + + /** @test */ + public function shouldSetAndGetProtectedPropertyUsingShortcuts() + { + $test = new ProtectedProperties(); + + $rand = uniqid('', true); + + $test->setSomeVar($rand); + $this->assertSame($rand, $test->getSomeVar()); + } + + /** + * @test + * + * @see https://github.com/phalcon/zephir/issues/2089 + */ + public function shouldSetAndGetProtectedPropertyUsingExtendedClass() + { + $test = new \ExtendsProtectedProperties(); + + $this->assertEquals(['key' => 'value'], $test->getSomeArrayVar()); + + $test->setSomeArrayVar(['key' => 'value']); + $this->assertEquals(['key' => 'value'], $test->getSomeArrayVar()); + + $test->setSomeArrayVar(['key2' => 'value2']); + $this->assertEquals(['key2' => 'value2'], $test->getSomeArrayVar()); } } diff --git a/tests/Zephir/CompilerFile/CheckDependenciesTest.php b/tests/Zephir/CompilerFile/CheckDependenciesTest.php index 1764fba421..4f45af695c 100644 --- a/tests/Zephir/CompilerFile/CheckDependenciesTest.php +++ b/tests/Zephir/CompilerFile/CheckDependenciesTest.php @@ -23,7 +23,7 @@ class CheckDependenciesTest extends KernelTestCase { public function testExtendsClassThatDoesNotExist() { - self::bootKernel(['config_files' => [__DIR__ . '/../../config.yml']]); + self::bootKernel(['config_files' => [__DIR__.'/../../config.yml']]); $logger = new TestLogger(); diff --git a/tests/fixtures/mocks/ExtendsProtectedProperties.php b/tests/fixtures/mocks/ExtendsProtectedProperties.php new file mode 100644 index 0000000000..fca486deac --- /dev/null +++ b/tests/fixtures/mocks/ExtendsProtectedProperties.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view + * LICENSE the file that was distributed with this source code. + */ + +use Stub\Properties\ProtectedProperties; + +class ExtendsProtectedProperties extends ProtectedProperties +{ + protected $someArrayVar = [ + 'key' => 'value', + ]; +} diff --git a/tests/fixtures/mocks/UserExample.php b/tests/fixtures/mocks/UserExample.php new file mode 100644 index 0000000000..facd6c50fa --- /dev/null +++ b/tests/fixtures/mocks/UserExample.php @@ -0,0 +1,16 @@ + + * + * For the full copyright and license information, please view + * LICENSE the file that was distributed with this source code. + */ + +use Stub\Oo\PropertyAccess; + +class UserExample extends PropertyAccess +{ +}