Skip to content

Commit

Permalink
Provide tests for #2089
Browse files Browse the repository at this point in the history
Closes: #2090
  • Loading branch information
sergeyklay committed May 9, 2020
1 parent bd50518 commit 031f890
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 77 deletions.
74 changes: 38 additions & 36 deletions stub/oo/propertyaccess.zep
Original file line number Diff line number Diff line change
@@ -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) -> <PropertyAccess>
{
let this->privateVariable = value;

return this;
}

/**
* @issue https://github.com/phalcon/zephir/issues/1851
*/
public function getPrivatevariable() -> var
{
return this->privateVariable;
}
}
38 changes: 36 additions & 2 deletions stub/properties/protectedproperties.zep
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

namespace Stub\Properties;

/**
* Stub\Properties\ProtectedProperties
*/
class ProtectedProperties
{

/**
* This is a protected property with no initial value
*/
Expand Down Expand Up @@ -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;
}

}
16 changes: 2 additions & 14 deletions tests/Extension/FcallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
* (c) Phalcon Team <[email protected]>
*
* 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;
Expand All @@ -15,10 +15,6 @@
use Stub\Fcall;
use Stub\Oo\PropertyAccess;

class UserExample extends PropertyAccess
{
}

class FcallTest extends TestCase
{
/** @var Fcall */
Expand Down Expand Up @@ -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());
}
}
37 changes: 25 additions & 12 deletions tests/Extension/Oo/Scopes/PrivateScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
}
7 changes: 4 additions & 3 deletions tests/Extension/Properties/PrivatePropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
*
* (c) Phalcon Team <[email protected]>
*
* 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());
Expand Down
49 changes: 40 additions & 9 deletions tests/Extension/Properties/ProtectedPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,60 @@
*
* (c) Phalcon Team <[email protected]>
*
* 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());
}
}
2 changes: 1 addition & 1 deletion tests/Zephir/CompilerFile/CheckDependenciesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
19 changes: 19 additions & 0 deletions tests/fixtures/mocks/ExtendsProtectedProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* This file is part of the Zephir.
*
* (c) Phalcon Team <[email protected]>
*
* 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',
];
}
Loading

0 comments on commit 031f890

Please sign in to comment.