@@ -45,7 +45,22 @@ public function it_generates_method_for_empty_class(): void
4545 $ ast = $ this ->parser ->parse ('' );
4646
4747 $ classFactory = ClassBuilder::fromScratch ('TestClass ' , 'My \\Awesome \\Service ' );
48- $ classFactory ->setMethods (ClassMethodBuilder::fromScratch ('setActive ' )->setReturnType ('void ' ));
48+ $ classFactory ->setMethods (
49+ ClassMethodBuilder::fromScratch ('setActive ' )->setReturnType ('void ' ),
50+ ClassMethodBuilder::fromScratch ('doSomething ' )->setReturnType ('void ' )->setFinal (true )
51+ );
52+
53+ $ methods = $ classFactory ->getMethods ();
54+
55+ $ this ->assertCount (2 , $ methods );
56+
57+ $ this ->assertSame ('setActive ' , $ methods [0 ]->getName ());
58+ $ this ->assertFalse ($ methods [0 ]->isAbstract ());
59+ $ this ->assertFalse ($ methods [0 ]->isFinal ());
60+
61+ $ this ->assertSame ('doSomething ' , $ methods [1 ]->getName ());
62+ $ this ->assertFalse ($ methods [1 ]->isAbstract ());
63+ $ this ->assertTrue ($ methods [1 ]->isFinal ());
4964
5065 $ nodeTraverser = new NodeTraverser ();
5166 $ classFactory ->injectVisitors ($ nodeTraverser , $ this ->parser );
@@ -61,6 +76,45 @@ class TestClass
6176 public function setActive() : void
6277 {
6378 }
79+ public final function doSomething() : void
80+ {
81+ }
82+ }
83+ EOF;
84+
85+ $ this ->assertSame ($ expected , $ this ->printer ->prettyPrintFile ($ nodeTraverser ->traverse ($ ast )));
86+ }
87+
88+ /**
89+ * @test
90+ */
91+ public function it_generates_abstract_method_for_empty_class (): void
92+ {
93+ $ ast = $ this ->parser ->parse ('' );
94+
95+ $ classFactory = ClassBuilder::fromScratch ('TestClass ' , 'My \\Awesome \\Service ' );
96+ $ classFactory ->setMethods (ClassMethodBuilder::fromScratch ('setActive ' )->setReturnType ('void ' )->setAbstract (true ));
97+
98+ $ methods = $ classFactory ->getMethods ();
99+
100+ $ this ->assertCount (1 , $ methods );
101+
102+ $ this ->assertSame ('setActive ' , $ methods [0 ]->getName ());
103+ $ this ->assertTrue ($ methods [0 ]->isAbstract ());
104+ $ this ->assertFalse ($ methods [0 ]->isFinal ());
105+
106+ $ nodeTraverser = new NodeTraverser ();
107+ $ classFactory ->injectVisitors ($ nodeTraverser , $ this ->parser );
108+
109+ $ expected = <<<'EOF'
110+ <?php
111+
112+ declare (strict_types=1);
113+ namespace My\Awesome\Service;
114+
115+ class TestClass
116+ {
117+ public abstract function setActive() : void;
64118}
65119EOF;
66120
@@ -83,6 +137,48 @@ class TestClass
83137 public function setActive() : void
84138 {
85139 }
140+ public final function doSomething() : void
141+ {
142+ }
143+ }
144+ EOF;
145+
146+ $ ast = $ this ->parser ->parse ($ expected );
147+
148+ $ classFactory = ClassBuilder::fromNodes (...$ ast );
149+
150+ $ methods = $ classFactory ->getMethods ();
151+
152+ $ this ->assertCount (2 , $ methods );
153+
154+ $ this ->assertSame ('setActive ' , $ methods [0 ]->getName ());
155+ $ this ->assertFalse ($ methods [0 ]->isAbstract ());
156+ $ this ->assertFalse ($ methods [0 ]->isFinal ());
157+
158+ $ this ->assertSame ('doSomething ' , $ methods [1 ]->getName ());
159+ $ this ->assertFalse ($ methods [1 ]->isAbstract ());
160+ $ this ->assertTrue ($ methods [1 ]->isFinal ());
161+
162+ $ nodeTraverser = new NodeTraverser ();
163+ $ classFactory ->injectVisitors ($ nodeTraverser , $ this ->parser );
164+
165+ $ this ->assertSame ($ expected , $ this ->printer ->prettyPrintFile ($ nodeTraverser ->traverse ($ this ->parser ->parse ('' ))));
166+ }
167+
168+ /**
169+ * @test
170+ */
171+ public function it_generates_abstract_method_for_empty_class_from_template (): void
172+ {
173+ $ expected = <<<'EOF'
174+ <?php
175+
176+ declare (strict_types=1);
177+ namespace My\Awesome\Service;
178+
179+ class TestClass
180+ {
181+ public abstract function setActive() : void;
86182}
87183EOF;
88184
@@ -95,6 +191,8 @@ public function setActive() : void
95191 $ this ->assertCount (1 , $ methods );
96192
97193 $ this ->assertSame ('setActive ' , $ methods [0 ]->getName ());
194+ $ this ->assertTrue ($ methods [0 ]->isAbstract ());
195+ $ this ->assertFalse ($ methods [0 ]->isFinal ());
98196
99197 $ nodeTraverser = new NodeTraverser ();
100198 $ classFactory ->injectVisitors ($ nodeTraverser , $ this ->parser );
0 commit comments