VerifyTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. include_once __DIR__.'/../src/Codeception/function.php';
  3. class VerifyTest extends \Codeception\PHPUnit\TestCase {
  4. protected $xml;
  5. protected function _setUp()
  6. {
  7. $this->xml = new DomDocument;
  8. $this->xml->loadXML('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
  9. }
  10. public function testEquals()
  11. {
  12. verify(5)->equals(5);
  13. verify("hello")->equals("hello");
  14. verify("user have 5 posts", 5)->equals(5);
  15. verify(3.251)->equals(3.25, 0.01);
  16. verify("respects delta", 3.251)->equals(3.25, 0.01);
  17. verify_file(__FILE__)->equals(__FILE__);
  18. }
  19. public function testNotEquals()
  20. {
  21. verify(3)->notEquals(5);
  22. verify(3.252)->notEquals(3.25, 0.001);
  23. verify("respects delta", 3.252, 0.001);
  24. verify_file(__FILE__)->notEquals(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'composer.json');
  25. }
  26. public function testContains()
  27. {
  28. verify(array(3, 2))->contains(3);
  29. verify("user have 5 posts", array(3, 2))->notContains(5);
  30. }
  31. public function testGreaterLowerThan()
  32. {
  33. verify(7)->greaterThan(5);
  34. verify(7)->lessThan(10);
  35. verify(7)->lessOrEquals(7);
  36. verify(7)->lessOrEquals(8);
  37. verify(7)->greaterOrEquals(7);
  38. verify(7)->greaterOrEquals(5);
  39. }
  40. public function testTrueFalseNull()
  41. {
  42. verify(true)->true();
  43. verify(false)->false();
  44. verify(null)->null();
  45. verify(true)->notNull();
  46. verify('something should be false', false)->false();
  47. verify('something should be true', true)->true();
  48. }
  49. public function testEmptyNotEmpty()
  50. {
  51. verify(array('3', '5'))->notEmpty();
  52. verify(array())->isEmpty();
  53. }
  54. public function testVerifyThat()
  55. {
  56. verify_that(12);
  57. verify_that('hello world');
  58. verify_that(array('hello'));
  59. }
  60. public function testVerifyNot()
  61. {
  62. verify_not(false);
  63. verify_not(null);
  64. verify_not(array());
  65. }
  66. public function testExpectFunctions()
  67. {
  68. expect(12)->equals(12);
  69. expect_that(true);
  70. expect_not(false);
  71. }
  72. public function testArrayHasKey()
  73. {
  74. $errors = array('title' => 'You should add title');
  75. expect($errors)->hasKey('title');
  76. expect($errors)->hasntKey('body');
  77. }
  78. public function testIsInstanceOf()
  79. {
  80. $testClass = new DateTime();
  81. expect($testClass)->isInstanceOf('DateTime');
  82. expect($testClass)->isNotInstanceOf('DateTimeZone');
  83. }
  84. public function testInternalType()
  85. {
  86. $testVar = array();
  87. expect($testVar)->internalType('array');
  88. expect($testVar)->notInternalType('boolean');
  89. }
  90. public function testHasAttribute()
  91. {
  92. expect('Exception')->hasAttribute('message');
  93. expect('Exception')->notHasAttribute('fakeproperty');
  94. $testObject = (object) array('existingAttribute' => true);
  95. expect($testObject)->hasAttribute('existingAttribute');
  96. expect($testObject)->notHasAttribute('fakeproperty');
  97. }
  98. public function testHasStaticAttribute()
  99. {
  100. expect('FakeClassForTesting')->hasStaticAttribute('staticProperty');
  101. expect('FakeClassForTesting')->notHasStaticAttribute('fakeProperty');
  102. }
  103. public function testContainsOnly()
  104. {
  105. expect(array('1', '2', '3'))->containsOnly('string');
  106. expect(array('1', '2', 3))->notContainsOnly('string');
  107. }
  108. public function testContainsOnlyInstancesOf()
  109. {
  110. expect(array(new FakeClassForTesting(), new FakeClassForTesting(), new FakeClassForTesting()))
  111. ->containsOnlyInstancesOf('FakeClassForTesting');
  112. }
  113. public function testCount()
  114. {
  115. expect(array(1,2,3))->count(3);
  116. expect(array(1,2,3))->notCount(2);
  117. }
  118. public function testEqualXMLStructure()
  119. {
  120. $expected = new DOMElement('foo');
  121. $actual = new DOMElement('foo');
  122. expect($expected)->equalXMLStructure($actual);
  123. }
  124. public function testFileExists()
  125. {
  126. expect_file(__FILE__)->exists();
  127. expect_file('completelyrandomfilename.txt')->notExists();
  128. }
  129. public function testEqualsJsonFile()
  130. {
  131. expect_file(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'json-test-file.json')
  132. ->equalsJsonFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'equal-json-test-file.json');
  133. expect('{"some" : "data"}')->equalsJsonFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'equal-json-test-file.json');
  134. }
  135. public function testEqualsJsonString()
  136. {
  137. expect('{"some" : "data"}')->equalsJsonString('{"some" : "data"}');
  138. }
  139. public function testRegExp()
  140. {
  141. expect('somestring')->regExp('/string/');
  142. }
  143. public function testMatchesFormat()
  144. {
  145. expect('somestring')->matchesFormat('%s');
  146. expect('somestring')->notMatchesFormat('%i');
  147. }
  148. public function testMatchesFormatFile()
  149. {
  150. expect('23')->matchesFormatFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  151. expect('asdfas')->notMatchesFormatFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  152. }
  153. public function testSame()
  154. {
  155. expect(1)->same(0+1);
  156. expect(1)->notSame(true);
  157. }
  158. public function testEndsWith()
  159. {
  160. expect('A completely not funny string')->endsWith('ny string');
  161. expect('A completely not funny string')->notEndsWith('A completely');
  162. }
  163. public function testEqualsFile()
  164. {
  165. expect('%i')->equalsFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  166. expect('Another string')->notEqualsFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  167. }
  168. public function testStartsWith()
  169. {
  170. expect('A completely not funny string')->startsWith('A completely');
  171. expect('A completely not funny string')->notStartsWith('string');
  172. }
  173. public function testEqualsXmlFile()
  174. {
  175. expect_file(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'xml-test-file.xml')
  176. ->equalsXmlFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'xml-test-file.xml');
  177. expect('<foo><bar>Baz</bar><bar>Baz</bar></foo>')
  178. ->equalsXmlFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'xml-test-file.xml');
  179. }
  180. public function testEqualsXmlString()
  181. {
  182. expect('<foo><bar>Baz</bar><bar>Baz</bar></foo>')
  183. ->equalsXmlString('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
  184. }
  185. public function testStringContainsString()
  186. {
  187. verify('foo bar')->stringContainsString('o b');
  188. verify('foo bar')->stringNotContainsString('BAR');
  189. }
  190. public function testStringContainsStringIgnoringCase()
  191. {
  192. verify('foo bar')->stringContainsStringIgnoringCase('O b');
  193. verify('foo bar')->stringNotContainsStringIgnoringCase('baz');
  194. }
  195. public function testIsString()
  196. {
  197. verify('foo bar')->string();
  198. verify(false)->notString();
  199. }
  200. public function testIsArray()
  201. {
  202. verify([1,2,3])->array();
  203. verify(false)->notArray();
  204. }
  205. public function testIsBool()
  206. {
  207. verify(false)->bool();
  208. verify([1,2,3])->notBool();
  209. }
  210. public function testIsFloat()
  211. {
  212. verify(1.5)->float();
  213. verify(1)->notFloat();
  214. }
  215. public function testIsInt()
  216. {
  217. verify(5)->int();
  218. verify(1.5)->notInt();
  219. }
  220. public function testIsNumeric()
  221. {
  222. verify('1.5')->numeric();
  223. verify('foo bar')->notNumeric();
  224. }
  225. public function testIsObject()
  226. {
  227. verify(new stdClass)->object();
  228. verify(false)->notObject();
  229. }
  230. public function testIsResource()
  231. {
  232. verify(fopen(__FILE__, 'r'))->resource();
  233. verify(false)->notResource();
  234. }
  235. public function testIsScalar()
  236. {
  237. verify('foo bar')->scalar();
  238. verify([1,2,3])->notScalar();
  239. }
  240. public function testIsCallable()
  241. {
  242. verify(function() {})->callable();
  243. verify(false)->notCallable();
  244. }
  245. }
  246. class FakeClassForTesting
  247. {
  248. static $staticProperty;
  249. }