1+ <?php
2+
3+ //require_once '../../../../../vendor/autoload.php';
4+
5+ if (version_compare (phpversion (), '5.5.0 ' , '> ' )) {
6+
7+ class Base
8+ {
9+ const TEMPLATE = '034_parent.tpl ' ;
10+
11+ public $ id = null ;
12+
13+ public function __construct ($ id )
14+ {
15+ $ this ->id = $ id ;
16+ }
17+
18+ public function getHTML ()
19+ {
20+ return static ::class;
21+ }
22+ }
23+
24+ class Child extends Base
25+ {
26+ const TEMPLATE = '034_child.tpl ' ;
27+
28+ public function getText ()
29+ {
30+ return $ this ->getHTML ();
31+ }
32+ }
33+
34+ class BaseClone extends Base
35+ {
36+
37+ }
38+
39+ /*
40+ * @requires PHP 5.5
41+ */
42+
43+ class InheritanceTest extends PHPUnit_Framework_TestCase
44+ {
45+ protected $ smarty ;
46+
47+ protected function setUp ()
48+ {
49+ chdir (dirname (__FILE__ ));
50+ $ this ->smarty = new Smarty ();
51+ $ this ->smarty ->setTemplateDir ('./templates ' );
52+ $ this ->smarty ->setCompileDir ('./templates_c ' );
53+ $ this ->smarty ->setCacheDir ('./cache ' );
54+ $ this ->smarty ->caching = false ;
55+ }
56+
57+ /**
58+ * @dataProvider providerInheritance
59+ * @requires PHP5.5
60+ */
61+ public function testInheritance ($ elements , $ assertions )
62+ {
63+ foreach ($ elements as $ nr => $ element ) {
64+ $ this ->smarty ->assign ('e ' , $ element );
65+ $ this ->assertSame ($ assertions [ $ nr ], $ this ->smarty ->fetch ($ element ::TEMPLATE ));
66+ }
67+ }
68+
69+ /**
70+ * @dataProvider providerInheritance
71+ */
72+ public function testInheritanceCaching ($ elements , $ assertions )
73+ {
74+ $ this ->smarty ->caching = true ;
75+ foreach ($ elements as $ nr => $ element ) {
76+ $ this ->smarty ->assign ('e ' , $ element );
77+ $ this ->assertSame ($ assertions [ $ nr ], $ this ->smarty ->fetch ($ element ::TEMPLATE ));
78+ }
79+ }
80+
81+ /**
82+ * @dataProvider providerInheritance
83+ */
84+ public function testInheritanceCaching1 ($ elements , $ assertions )
85+ {
86+ $ this ->smarty ->caching = true ;
87+ foreach ($ elements as $ nr => $ element ) {
88+ $ this ->smarty ->assign ('e ' , $ element );
89+ $ stat = $ this ->smarty ->isCached ($ element ::TEMPLATE );
90+ $ this ->assertSame ($ assertions [ $ nr ], $ this ->smarty ->fetch ($ element ::TEMPLATE ));
91+ $ this ->assertTrue ($ stat );
92+ }
93+ }
94+
95+ public function providerInheritance ()
96+ {
97+ return [[//(#0) This test works as expected
98+ [new Base (1 ), new Base (2 ), new BaseClone (3 ),], ['1 Base ' , '2 Base ' , '3 BaseClone ' ,],],
99+ [//(#1) This test works as expected
100+ [new Child (1 ), new BaseClone (2 ), //This output is right(!)
101+ ], ['1 Child ' , '2 BaseClone ' ,],], [//(#2) This test fails
102+ [new Child (1 ), new Child (2 ), new BaseClone (3 ),],
103+ ['1 Child ' , '2 Child ' , '3 BaseClone ' ,
104+ //Here the output is "2 Child"
105+ ],], [//(#3) This test fails
106+ [new Child (1 ), new BaseClone (2 ), new Child (3 ),],
107+ ['1 Child ' , '2 BaseClone ' , '3 Child ' ,
108+ //Here the output is "2 Child"
109+ ],], [//(#4) This test fails
110+ [new Child (1 ), new Child (2 ), new BaseClone (3 ),
111+ new Child (4 ),],
112+ ['1 Child ' , '2 Child ' , '3 BaseClone ' ,
113+ //Here the output is also "2 Child"
114+ '4 Child ' ,],], [//(#5) This test fails
115+ [new BaseClone (1 ), new Child (2 ),
116+ new Child (3 ), new BaseClone (4 ),],
117+ ['1 BaseClone ' ,
118+ //This output is right(!)
119+ '2 Child ' , '3 Child ' ,
120+ '4 BaseClone ' ,
121+ //Here the output is "3 Child"
122+ ],],];
123+ }
124+ }
125+ }
0 commit comments