Skip to content

Commit c4b383e

Browse files
authored
Fix PhanUndeclaredMethod in tests directory (#7241)
This fixes all of the "PhanUndeclaredMethod" class errors in the test directory. These are mostly caused by phan not knowing that mock objects have the methods of the class being mocked, and it needs to be specifically told so with a phan type assertion. Calling methods that don't exist is a bad idea. We should unignore this error type, but first the errors need to be fixed. This PR starts with a large number of false positives caused by mock objects..
1 parent e44d739 commit c4b383e

File tree

7 files changed

+264
-110
lines changed

7 files changed

+264
-110
lines changed

test/unittests/Database_Test.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ function testUpdateEscapesHTML()
187187

188188
$stub->_PDO->expects($this->once())
189189
->method("prepare")->will($this->returnValue($stmt));
190+
191+
'@phan-var \Database $stub';
190192
$stub->update("test", ['field' => '<b>Hello</b>'], []);
191193

192194
}
@@ -213,6 +215,8 @@ function testUnsafeUpdateDoesntEscapeHTML()
213215

214216
$stub->_PDO->expects($this->once())
215217
->method("prepare")->will($this->returnValue($stmt));
218+
219+
'@phan-var \Database $stub';
216220
$stub->unsafeupdate("test", ['field' => '<b>Hello</b>'], []);
217221

218222
}
@@ -238,6 +242,8 @@ function testInsertEscapesHTML()
238242

239243
$stub->_PDO->expects($this->once())
240244
->method("prepare")->will($this->returnValue($stmt));
245+
246+
'@phan-var \Database $stub';
241247
$stub->insert("test", ['field' => '<b>Hello</b>'], []);
242248

243249
}
@@ -264,6 +270,8 @@ function testUnsafeInsertDoesntEscapeHTML()
264270

265271
$stub->_PDO->expects($this->once())->method("prepare")
266272
->will($this->returnValue($stmt));
273+
274+
'@phan-var \Database $stub';
267275
$stub->unsafeinsert("test", ['field' => '<b>Hello</b>'], []);
268276

269277
}
@@ -797,6 +805,8 @@ function testInsertOnDuplicateUpdateEscapesHTML()
797805

798806
$stub->_PDO->expects($this->once())
799807
->method("prepare")->will($this->returnValue($stmt));
808+
809+
'@phan-var \Database $stub';
800810
$stub->insertOnDuplicateUpdate(
801811
"test",
802812
['field' => '<b>Hello</b>'],
@@ -828,6 +838,8 @@ function testUnsafeInsertOnDuplicateUpdateDoesntEscapeHTML()
828838

829839
$stub->_PDO->expects($this->once())
830840
->method("prepare")->will($this->returnValue($stmt));
841+
842+
'@phan-var \Database $stub';
831843
$stub->unsafeInsertOnDuplicateUpdate(
832844
"test",
833845
['field' => '<b>Hello</b>'],
@@ -885,6 +897,8 @@ function testRun()
885897

886898
$stub->_PDO->expects($this->once())
887899
->method("exec")->with($this->equalTo("SHOW TABLES"));
900+
901+
'@phan-var \Database $stub';
888902
$stub->run("SHOW TABLES");
889903
}
890904

@@ -907,6 +921,8 @@ function testPrepare()
907921
->method("prepare")
908922
->with($this->equalTo("SHOW TABLES"))
909923
->willReturn(new PDOStatement());
924+
925+
'@phan-var \Database $stub';
910926
$stub->prepare("SHOW TABLES");
911927
}
912928

@@ -1017,13 +1033,17 @@ function testPselectCallsFunctions()
10171033
$stub = $this->getMockBuilder('FakeDatabase')
10181034
->onlyMethods($this->_getAllMethodsExcept(['pselect']))->getMock();
10191035

1036+
'@phan-var \Database $stub';
10201037
$stmt = $stub->prepare("SHOW TABLES");
10211038
$params = ['test' => 'test'];
10221039

1040+
'@phan-var \PHPUnit\Framework\MockObject\MockObject $stub';
10231041
$stub->expects($this->once())
10241042
->method("prepare")->with($this->equalTo("SHOW TABLES"));
10251043
$stub->expects($this->once())->method("execute")
10261044
->with($this->equalTo($stmt), $this->equalTo($params), []);
1045+
1046+
'@phan-var \Database $stub';
10271047
$stub->pselect("SHOW TABLES", $params);
10281048
}
10291049

@@ -1080,6 +1100,8 @@ function testPselectRowCallsPrepare()
10801100
$this->equalTo($query . " LIMIT 2"),
10811101
$params
10821102
);
1103+
1104+
'@phan-var \Database $stub';
10831105
$stub->pselectRow(
10841106
$query,
10851107
$params
@@ -1324,6 +1346,8 @@ function testInsertIgnore()
13241346

13251347
$stub->expects($this->once())
13261348
->method("_realinsert")->with($this->equalTo($table), $set, true, true);
1349+
1350+
'@phan-var \Database $stub';
13271351
$stub->insertIgnore($table, $set);
13281352
}
13291353

@@ -1589,6 +1613,8 @@ function testQuote()
15891613
$string = "Co'mpl''ex \"st'\"ring";
15901614
$stub->_PDO->expects($this->once())->method("quote")
15911615
->willReturn("Complex string");
1616+
1617+
'@phan-var \Database $stub';
15921618
$stub->quote($string);
15931619
}
15941620

@@ -1621,6 +1647,8 @@ function testInTransaction()
16211647

16221648
$stub->_PDO->expects($this->once())->method("inTransaction")
16231649
->willReturn(true);
1650+
1651+
'@phan-var \Database $stub';
16241652
$stub->inTransaction();
16251653
}
16261654

@@ -1643,6 +1671,8 @@ function testBeginTransaction()
16431671
$stub->expects($this->once())->method("inTransaction")->willReturn(false);
16441672
$stub->_PDO->expects($this->once())->method("beginTransaction")
16451673
->willReturn(true);
1674+
1675+
'@phan-var \Database $stub';
16461676
$stub->beginTransaction();
16471677
}
16481678

@@ -1663,6 +1693,8 @@ function testBeginTransactionThrowsException()
16631693

16641694
$stub->expects($this->once())->method("inTransaction")->willReturn(true);
16651695
$this->expectException("DatabaseException");
1696+
1697+
'@phan-var \Database $stub';
16661698
$stub->beginTransaction();
16671699
}
16681700

@@ -1682,6 +1714,8 @@ function testRollback()
16821714

16831715
$stub->expects($this->once())->method("inTransaction")->willReturn(true);
16841716
$stub->_PDO->expects($this->once())->method("rollBack")->willReturn(true);
1717+
1718+
'@phan-var \Database $stub';
16851719
$stub->rollBack();
16861720
}
16871721

@@ -1701,6 +1735,8 @@ function testRollbackThrowsException()
17011735

17021736
$stub->expects($this->once())->method("inTransaction")->willReturn(false);
17031737
$this->expectException("DatabaseException");
1738+
1739+
'@phan-var \Database $stub';
17041740
$stub->rollBack();
17051741
}
17061742

@@ -1720,6 +1756,8 @@ function testCommit()
17201756

17211757
$stub->expects($this->once())->method("inTransaction")->willReturn(true);
17221758
$stub->_PDO->expects($this->once())->method("commit")->willReturn(true);
1759+
1760+
'@phan-var \Database $stub';
17231761
$stub->commit();
17241762
}
17251763

@@ -1739,6 +1777,8 @@ function testCommitThrowsException()
17391777

17401778
$stub->expects($this->once())->method("inTransaction")->willReturn(false);
17411779
$this->expectException("DatabaseException");
1780+
1781+
'@phan-var \Database $stub';
17421782
$stub->commit();
17431783
}
17441784

@@ -1755,6 +1795,7 @@ function testIsConnectedNoPDO()
17551795
->onlyMethods($this->_getAllMethodsExcept(['isConnected']))
17561796
->getMock();
17571797

1798+
'@phan-var \Database $stub';
17581799
$val = $stub->isConnected();
17591800
$this->assertEquals($val, false);
17601801
}
@@ -1772,6 +1813,7 @@ function testIsConnectedWithPDO()
17721813
->onlyMethods($this->_getAllMethodsExcept(['isConnected']))
17731814
->getMock();
17741815

1816+
'@phan-var \Database $stub';
17751817
$stub->_PDO = $this->getMockBuilder('FakePDO')->getMock();
17761818
$val = $stub->isConnected();
17771819
$this->assertEquals($val, true);

0 commit comments

Comments
 (0)