summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDmitry Stogov <dmitry@zend.com>2015-12-08 12:40:42 +0300
committerDmitry Stogov <dmitry@zend.com>2015-12-08 12:40:42 +0300
commita75c195000b3226904103244fa9c3d0ce1111838 (patch)
tree1242c6dc6078647c43a295e4c83d8b6d00e88324 /tests
parenta8b7d0c29d13902f748c08d8a58e3d304450a1a2 (diff)
downloadphp-git-a75c195000b3226904103244fa9c3d0ce1111838.tar.gz
Implemented the RFC `Support Class Constant Visibility`.
Squashed commit of the following: commit f11ca0e7a57793fa0e3e7f6d451720e6c42bb0b9 Author: Dmitry Stogov <dmitry@zend.com> Date: Tue Dec 8 12:38:42 2015 +0300 Fixed test expectation commit 211f873f542504d0a0f72b6b5cb23908a1c99a2d Author: Dmitry Stogov <dmitry@zend.com> Date: Tue Dec 8 12:28:38 2015 +0300 Embed zend_class_constant.flags into zend_class_constants.value.u2.access_flags commit 51deab84b2cdbf9cdb1a838cf33b2ee45c61748b Author: Dmitry Stogov <dmitry@zend.com> Date: Mon Dec 7 11:18:55 2015 +0300 Fixed issues found by Nikita commit 544dbd5b47e40d38a8ccb96bc5583e9cb7fdd723 Author: Dmitry Stogov <dmitry@zend.com> Date: Sat Dec 5 02:41:05 2015 +0300 Refactored immplementation of https://wiki.php.net/rfc/class_const_visibility @reeze created an RFC here and I emailed internals here and didn't get any responses positive/negative.
Diffstat (limited to 'tests')
-rw-r--r--tests/classes/constants_comments_001.phpt34
-rw-r--r--tests/classes/constants_visibility_001.phpt23
-rw-r--r--tests/classes/constants_visibility_002.phpt30
-rw-r--r--tests/classes/constants_visibility_003.phpt30
-rw-r--r--tests/classes/constants_visibility_004.phpt28
-rw-r--r--tests/classes/constants_visibility_005.phpt10
-rw-r--r--tests/classes/constants_visibility_006.phpt11
-rw-r--r--tests/classes/constants_visibility_007.phpt10
-rw-r--r--tests/classes/constants_visibility_error_001.phpt16
-rw-r--r--tests/classes/constants_visibility_error_002.phpt16
-rw-r--r--tests/classes/constants_visibility_error_003.phpt16
-rw-r--r--tests/classes/constants_visibility_error_004.phpt16
-rw-r--r--tests/classes/interface_constant_inheritance_005.phpt12
-rw-r--r--tests/classes/interface_constant_inheritance_006.phpt10
-rw-r--r--tests/classes/interface_constant_inheritance_007.phpt10
15 files changed, 272 insertions, 0 deletions
diff --git a/tests/classes/constants_comments_001.phpt b/tests/classes/constants_comments_001.phpt
new file mode 100644
index 0000000000..dbdd67c332
--- /dev/null
+++ b/tests/classes/constants_comments_001.phpt
@@ -0,0 +1,34 @@
+--TEST--
+Class constants and doc comments
+--INI--
+opcache.save_comments=1
+--FILE--
+<?php
+class X {
+ /** comment X1 */
+ const X1 = 1;
+ const X2 = 2;
+ /** comment X3 */
+ const X3 = 3;
+}
+class Y extends X {
+ /** comment Y1 */
+ const Y1 = 1;
+ const Y2 = 2;
+ /** comment Y3 */
+ const Y3 = 3;
+}
+$r = new ReflectionClass('Y');
+foreach ($r->getReflectionConstants() as $rc) {
+ echo $rc->getName() . " : " . $rc->getDocComment() . "\n";
+}
+
+
+?>
+--EXPECT--
+Y1 : /** comment Y1 */
+Y2 :
+Y3 : /** comment Y3 */
+X1 : /** comment X1 */
+X2 :
+X3 : /** comment X3 */
diff --git a/tests/classes/constants_visibility_001.phpt b/tests/classes/constants_visibility_001.phpt
new file mode 100644
index 0000000000..37a0154d92
--- /dev/null
+++ b/tests/classes/constants_visibility_001.phpt
@@ -0,0 +1,23 @@
+--TEST--
+Class public constant visibility
+--FILE--
+<?php
+class A {
+ public const publicConst = 'publicConst';
+ static function staticConstDump() {
+ var_dump(self::publicConst);
+ }
+ function constDump() {
+ var_dump(self::publicConst);
+ }
+}
+
+var_dump(A::publicConst);
+A::staticConstDump();
+(new A())->constDump();
+
+?>
+--EXPECTF--
+string(11) "publicConst"
+string(11) "publicConst"
+string(11) "publicConst"
diff --git a/tests/classes/constants_visibility_002.phpt b/tests/classes/constants_visibility_002.phpt
new file mode 100644
index 0000000000..82bdd6df1f
--- /dev/null
+++ b/tests/classes/constants_visibility_002.phpt
@@ -0,0 +1,30 @@
+--TEST--
+Class protected constant visibility
+--FILE--
+<?php
+class A {
+ protected const protectedConst = 'protectedConst';
+ static function staticConstDump() {
+ var_dump(self::protectedConst);
+ }
+ function constDump() {
+ var_dump(self::protectedConst);
+ }
+}
+
+A::staticConstDump();
+(new A())->constDump();
+constant('A::protectedConst');
+
+?>
+--EXPECTF--
+string(14) "protectedConst"
+string(14) "protectedConst"
+
+Warning: constant(): Couldn't find constant A::protectedConst in %s on line 14
+
+Fatal error: Uncaught Error: Cannot access protected const A::protectedConst in %s:14
+Stack trace:
+#0 %s(14): constant('A::protectedCon...')
+#1 {main}
+ thrown in %s on line 14
diff --git a/tests/classes/constants_visibility_003.phpt b/tests/classes/constants_visibility_003.phpt
new file mode 100644
index 0000000000..7f049abffb
--- /dev/null
+++ b/tests/classes/constants_visibility_003.phpt
@@ -0,0 +1,30 @@
+--TEST--
+Class private constant visibility
+--FILE--
+<?php
+class A {
+ private const privateConst = 'privateConst';
+ static function staticConstDump() {
+ var_dump(self::privateConst);
+ }
+ function constDump() {
+ var_dump(self::privateConst);
+ }
+}
+
+A::staticConstDump();
+(new A())->constDump();
+constant('A::privateConst');
+
+?>
+--EXPECTF--
+string(12) "privateConst"
+string(12) "privateConst"
+
+Warning: constant(): Couldn't find constant A::privateConst in %s on line 14
+
+Fatal error: Uncaught Error: Cannot access private const A::privateConst in %s:14
+Stack trace:
+#0 %s(14): constant('A::privateConst')
+#1 {main}
+ thrown in %s on line 14
diff --git a/tests/classes/constants_visibility_004.phpt b/tests/classes/constants_visibility_004.phpt
new file mode 100644
index 0000000000..93acacf3c9
--- /dev/null
+++ b/tests/classes/constants_visibility_004.phpt
@@ -0,0 +1,28 @@
+--TEST--
+Only public and protected class constants should be inherited
+--FILE--
+<?php
+class A {
+ public const X = 1;
+ protected const Y = 2;
+ private const Z = 3;
+}
+class B extends A {
+ static public function checkConstants() {
+ var_dump(self::X);
+ var_dump(self::Y);
+ var_dump(self::Z);
+ }
+}
+
+B::checkConstants();
+?>
+--EXPECTF--
+int(1)
+int(2)
+
+Fatal error: Uncaught Error: Undefined class constant 'Z' in %s:11
+Stack trace:
+#0 %s(15): B::checkConstants()
+#1 {main}
+ thrown in %s on line 11
diff --git a/tests/classes/constants_visibility_005.phpt b/tests/classes/constants_visibility_005.phpt
new file mode 100644
index 0000000000..813009c675
--- /dev/null
+++ b/tests/classes/constants_visibility_005.phpt
@@ -0,0 +1,10 @@
+--TEST--
+Static constants are not allowed
+--FILE--
+<?php
+class A {
+ static const X = 1;
+}
+?>
+--EXPECTF--
+Fatal error: Cannot use 'static' as constant modifier in %s on line 3
diff --git a/tests/classes/constants_visibility_006.phpt b/tests/classes/constants_visibility_006.phpt
new file mode 100644
index 0000000000..537c8eac0f
--- /dev/null
+++ b/tests/classes/constants_visibility_006.phpt
@@ -0,0 +1,11 @@
+--TEST--
+Abstract constants are not allowed
+--FILE--
+<?php
+class A {
+ abstract const X = 1;
+}
+?>
+--EXPECTF--
+Fatal error: Cannot use 'abstract' as constant modifier in %s on line 3
+
diff --git a/tests/classes/constants_visibility_007.phpt b/tests/classes/constants_visibility_007.phpt
new file mode 100644
index 0000000000..f1b040c5c3
--- /dev/null
+++ b/tests/classes/constants_visibility_007.phpt
@@ -0,0 +1,10 @@
+--TEST--
+Final constants are not allowed
+--FILE--
+<?php
+class A {
+ final const X = 1;
+}
+?>
+--EXPECTF--
+Fatal error: Cannot use 'final' as constant modifier in %s on line 3
diff --git a/tests/classes/constants_visibility_error_001.phpt b/tests/classes/constants_visibility_error_001.phpt
new file mode 100644
index 0000000000..397dd24882
--- /dev/null
+++ b/tests/classes/constants_visibility_error_001.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Class private constant visibility error
+--FILE--
+<?php
+class A {
+ private const privateConst = 'privateConst';
+}
+
+var_dump(A::privateConst);
+
+?>
+--EXPECTF--
+Fatal error: Uncaught Error: Cannot access private const A::privateConst in %s:6
+Stack trace:
+#0 {main}
+ thrown in %s on line 6
diff --git a/tests/classes/constants_visibility_error_002.phpt b/tests/classes/constants_visibility_error_002.phpt
new file mode 100644
index 0000000000..2980b52c37
--- /dev/null
+++ b/tests/classes/constants_visibility_error_002.phpt
@@ -0,0 +1,16 @@
+--TEST--
+Class protected constant visibility error
+--FILE--
+<?php
+class A {
+ protected const protectedConst = 'protectedConst';
+}
+
+var_dump(A::protectedConst);
+
+?>
+--EXPECTF--
+Fatal error: Uncaught Error: Cannot access protected const A::protectedConst in %s:6
+Stack trace:
+#0 {main}
+ thrown in %s on line 6
diff --git a/tests/classes/constants_visibility_error_003.phpt b/tests/classes/constants_visibility_error_003.phpt
new file mode 100644
index 0000000000..c385bbd300
--- /dev/null
+++ b/tests/classes/constants_visibility_error_003.phpt
@@ -0,0 +1,16 @@
+--TEST--
+A redeclared class constant must have the same or higher visibility
+--FILE--
+<?php
+
+class A {
+ public const publicConst = 0;
+}
+
+class B extends A {
+ protected const publicConst = 1;
+}
+
+
+--EXPECTF--
+Fatal error: Access level to B::publicConst must be public (as in class A) in %s on line 9
diff --git a/tests/classes/constants_visibility_error_004.phpt b/tests/classes/constants_visibility_error_004.phpt
new file mode 100644
index 0000000000..fe37b0691f
--- /dev/null
+++ b/tests/classes/constants_visibility_error_004.phpt
@@ -0,0 +1,16 @@
+--TEST--
+A redeclared class constant must have the same or higher visibility
+--FILE--
+<?php
+
+class A {
+ protected const protectedConst = 0;
+}
+
+class B extends A {
+ private const protectedConst = 1;
+}
+
+
+--EXPECTF--
+Fatal error: Access level to B::protectedConst must be protected (as in class A) or weaker in %s on line 9
diff --git a/tests/classes/interface_constant_inheritance_005.phpt b/tests/classes/interface_constant_inheritance_005.phpt
new file mode 100644
index 0000000000..60bf222e85
--- /dev/null
+++ b/tests/classes/interface_constant_inheritance_005.phpt
@@ -0,0 +1,12 @@
+--TEST--
+Ensure a interface can have public constants
+--FILE--
+<?php
+interface IA {
+ public const FOO = 10;
+}
+
+echo "Done\n";
+?>
+--EXPECT--
+Done \ No newline at end of file
diff --git a/tests/classes/interface_constant_inheritance_006.phpt b/tests/classes/interface_constant_inheritance_006.phpt
new file mode 100644
index 0000000000..125326b224
--- /dev/null
+++ b/tests/classes/interface_constant_inheritance_006.phpt
@@ -0,0 +1,10 @@
+--TEST--
+Ensure a interface can not have protected constants
+
+--FILE--
+<?php
+interface A {
+ protected const FOO = 10;
+}
+--EXPECTF--
+Fatal error: Access type for interface constant A::FOO must be public in %s on line 3
diff --git a/tests/classes/interface_constant_inheritance_007.phpt b/tests/classes/interface_constant_inheritance_007.phpt
new file mode 100644
index 0000000000..52695343e1
--- /dev/null
+++ b/tests/classes/interface_constant_inheritance_007.phpt
@@ -0,0 +1,10 @@
+--TEST--
+Ensure a interface can not have private constants
+
+--FILE--
+<?php
+interface A {
+ private const FOO = 10;
+}
+--EXPECTF--
+Fatal error: Access type for interface constant A::FOO must be public in %s on line 3