3232use PHPStan \Rules \IdentifierRuleError ;
3333use PHPStan \Rules \NonIgnorableRuleError ;
3434use PHPStan \Rules \RuleError ;
35+ use PHPUnit \Framework \Attributes \DataProvider ;
3536use PHPUnit \Framework \MockObject \MockObject ;
3637use PHPUnit \Framework \TestCase ;
3738
4041 */
4142class BannedNodesRuleTest extends TestCase
4243{
43- /**
44- * @var BannedNodesRule
45- */
46- private $ rule ;
47-
48- /**
49- * @var Scope|MockObject
50- */
51- private $ scope ;
44+ private BannedNodesRule $ rule ;
45+
46+ private Scope &MockObject $ scope ;
5247
5348 /**
5449 * {@inheritdoc}
@@ -77,18 +72,6 @@ public function testGetNodeType(): void
7772 $ this ->assertSame (Node::class, $ this ->rule ->getNodeType ());
7873 }
7974
80- /**
81- * Tests processNode with unhandled nodes.
82- *
83- * @param Expr $node
84- *
85- * @dataProvider getUnhandledNodes
86- */
87- public function testProcessNodeWithUnhandledType (Expr $ node ): void
88- {
89- $ this ->assertCount (0 , $ this ->rule ->processNode ($ node , $ this ->scope ));
90- }
91-
9275 public function testProcessNodeWithBannedFunctions (): void
9376 {
9477 $ ruleWithoutLeadingSlashes = new BannedNodesRule (
@@ -126,20 +109,6 @@ public function testProcessNodeWithBannedFunctions(): void
126109 $ this ->assertNodeTriggersError ($ ruleWithLeadingSlashes , $ namespacedFunction );
127110 }
128111
129- protected function assertNodeTriggersError (BannedNodesRule $ rule , Node $ node ): void
130- {
131- $ errors = $ rule ->processNode ($ node , $ this ->scope );
132- $ this ->assertCount (1 , $ errors );
133- $ error = $ errors [0 ];
134- $ this ->assertStringStartsWith ('ekinoBannedCode. ' , $ error ->getIdentifier ());
135- $ this ->assertInstanceOf (NonIgnorableRuleError::class, $ error );
136- }
137-
138- protected function assertNodePasses (BannedNodesRule $ rule , Node $ node ): void
139- {
140- $ this ->assertCount (0 , $ rule ->processNode ($ node , $ this ->scope ));
141- }
142-
143112 public function testProcessNodeWithAllowedFunctions (): void
144113 {
145114 $ rootFunction = new FuncCall (new Name ('allowed ' ));
@@ -169,12 +138,22 @@ public function testProcessNodeWithArrayDimFetch(): void
169138 }
170139
171140 /**
172- * Tests processNode with handled nodes.
141+ * Tests processNode with unhandled nodes.
173142 *
174143 * @param Expr $node
144+ */
145+ #[DataProvider('getUnhandledNodes ' )]
146+ public function testProcessNodeWithUnhandledType (Expr $ node ): void
147+ {
148+ $ this ->assertCount (0 , $ this ->rule ->processNode ($ node , $ this ->scope ));
149+ }
150+
151+ /**
152+ * Tests processNode with handled nodes.
175153 *
176- * @dataProvider getHandledNodes
154+ * @param Expr $node
177155 */
156+ #[DataProvider('getHandledNodes ' )]
178157 public function testProcessNodeWithHandledTypes (Expr $ node ): void
179158 {
180159 $ this ->assertNodeTriggersError ($ this ->rule , $ node );
@@ -183,19 +162,35 @@ public function testProcessNodeWithHandledTypes(Expr $node): void
183162 /**
184163 * @return \Generator<array<Include_>>
185164 */
186- public function getUnhandledNodes (): \Generator
165+ public static function getUnhandledNodes (): \Generator
187166 {
188- yield [new Include_ ($ this -> createMock (Expr::class ), Include_::TYPE_INCLUDE )];
167+ yield [new Include_ (new String_ ( ' test ' ), Include_::TYPE_INCLUDE )];
189168 }
190169
191170 /**
192171 * @return \Generator<array<mixed>>
193172 */
194- public function getHandledNodes (): \Generator
173+ public static function getHandledNodes (): \Generator
195174 {
196- yield [new Eval_ ($ this ->createMock (Expr::class))];
175+ $ stringExpr = new String_ ('test ' );
176+
177+ yield [new Eval_ ($ stringExpr )];
197178 yield [new Exit_ ()];
198- yield [new Print_ ($ this ->createMock (Expr::class))];
199- yield [new ShellExec ([new String_ ('' )])];
179+ yield [new Print_ ($ stringExpr )];
180+ yield [new ShellExec ([$ stringExpr ])];
181+ }
182+
183+ protected function assertNodeTriggersError (BannedNodesRule $ rule , Node $ node ): void
184+ {
185+ $ errors = $ rule ->processNode ($ node , $ this ->scope );
186+ $ this ->assertCount (1 , $ errors );
187+ $ error = $ errors [0 ];
188+ $ this ->assertStringStartsWith ('ekinoBannedCode. ' , $ error ->getIdentifier ());
189+ $ this ->assertInstanceOf (NonIgnorableRuleError::class, $ error );
190+ }
191+
192+ protected function assertNodePasses (BannedNodesRule $ rule , Node $ node ): void
193+ {
194+ $ this ->assertCount (0 , $ rule ->processNode ($ node , $ this ->scope ));
200195 }
201196}
0 commit comments