VerifyTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. include __DIR__.'/../src/Codeception/function.php';
  3. include __DIR__.'/../vendor/autoload.php';
  4. class VerifyTest extends PHPUnit_Framework_TestCase {
  5. protected $xml;
  6. protected function setUp()
  7. {
  8. $this->xml = new DomDocument;
  9. $this->xml->loadXML('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
  10. }
  11. public function testEquals()
  12. {
  13. verify(5)->equals(5);
  14. verify("hello")->equals("hello");
  15. verify("user have 5 posts", 5)->equals(5);
  16. verify(3)->notEquals(5);
  17. verify_file(__FILE__)->equals(__FILE__);
  18. verify_file(__FILE__)->notEquals(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'composer.json');
  19. }
  20. public function testContains()
  21. {
  22. verify(array(3, 2))->contains(3);
  23. verify("user have 5 posts", array(3, 2))->notContains(5);
  24. }
  25. public function testGreaterLowerThan()
  26. {
  27. verify(7)->greaterThan(5);
  28. verify(7)->lessThan(10);
  29. verify(7)->lessOrEquals(7);
  30. verify(7)->lessOrEquals(8);
  31. verify(7)->greaterOrEquals(7);
  32. verify(7)->greaterOrEquals(5);
  33. }
  34. public function testTrueFalseNull()
  35. {
  36. verify(true)->true();
  37. verify(false)->false();
  38. verify(null)->null();
  39. verify(true)->notNull();
  40. verify('something should be false', false)->false();
  41. verify('something should be true', true)->true();
  42. }
  43. public function testEmptyNotEmpty()
  44. {
  45. verify(array('3', '5'))->notEmpty();
  46. verify(array())->isEmpty();
  47. }
  48. public function testVerifyThat()
  49. {
  50. verify_that(12);
  51. verify_that('hello world');
  52. verify_that(array('hello'));
  53. }
  54. public function testVerifyNot()
  55. {
  56. verify_not(false);
  57. verify_not(null);
  58. verify_not(array());
  59. }
  60. public function testExpectFunctions()
  61. {
  62. expect(12)->equals(12);
  63. expect_that(true);
  64. expect_not(false);
  65. }
  66. public function testArrayHasKey()
  67. {
  68. $errors = array('title' => 'You should add title');
  69. expect($errors)->hasKey('title');
  70. expect($errors)->hasntKey('body');
  71. }
  72. public function testIsInstanceOf()
  73. {
  74. $testClass = new DateTime();
  75. expect($testClass)->isInstanceOf('DateTime');
  76. expect($testClass)->isNotInstanceOf('DateTimeZone');
  77. }
  78. public function testInternalType()
  79. {
  80. $testVar = array();
  81. expect($testVar)->internalType('array');
  82. expect($testVar)->notInternalType('boolean');
  83. }
  84. public function testHasAttribute()
  85. {
  86. expect('Exception')->hasAttribute('message');
  87. expect('Exception')->notHasAttribute('fakeproperty');
  88. $testObject = (object) array('existingAttribute' => true);
  89. expect($testObject)->hasAttribute('existingAttribute');
  90. expect($testObject)->notHasAttribute('fakeproperty');
  91. }
  92. public function testHasStaticAttribute()
  93. {
  94. expect('FakeClassForTesting')->hasStaticAttribute('staticProperty');
  95. expect('FakeClassForTesting')->notHasStaticAttribute('fakeProperty');
  96. }
  97. public function testContainsOnly()
  98. {
  99. expect(array('1', '2', '3'))->containsOnly('string');
  100. expect(array('1', '2', 3))->notContainsOnly('string');
  101. }
  102. public function testContainsOnlyInstancesOf()
  103. {
  104. expect(array(new FakeClassForTesting(), new FakeClassForTesting(), new FakeClassForTesting()))
  105. ->containsOnlyInstancesOf('FakeClassForTesting');
  106. }
  107. public function testCount()
  108. {
  109. expect(array(1,2,3))->count(3);
  110. expect(array(1,2,3))->notCount(2);
  111. }
  112. public function testEqualXMLStructure()
  113. {
  114. $expected = new DOMElement('foo');
  115. $actual = new DOMElement('foo');
  116. expect($expected)->equalXMLStructure($actual);
  117. }
  118. public function testFileExists()
  119. {
  120. expect_file(__FILE__)->exists();
  121. expect_file('completelyrandomfilename.txt')->notExists();
  122. }
  123. public function testEqualsJsonFile()
  124. {
  125. expect_file(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'json-test-file.json')
  126. ->equalsJsonFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'equal-json-test-file.json');
  127. expect('{"some" : "data"}')->equalsJsonFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'equal-json-test-file.json');
  128. }
  129. public function testEqualsJsonString()
  130. {
  131. expect('{"some" : "data"}')->equalsJsonString('{"some" : "data"}');
  132. }
  133. public function testRegExp()
  134. {
  135. expect('somestring')->regExp('/string/');
  136. }
  137. public function testMatchesFormat()
  138. {
  139. expect('somestring')->matchesFormat('%s');
  140. expect('somestring')->notMatchesFormat('%i');
  141. }
  142. public function testMatchesFormatFile()
  143. {
  144. expect('23')->matchesFormatFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  145. expect('asdfas')->notMatchesFormatFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  146. }
  147. public function testSame()
  148. {
  149. expect(1)->same(0+1);
  150. expect(1)->notSame(true);
  151. }
  152. public function testEndsWith()
  153. {
  154. expect('A completely not funny string')->endsWith('ny string');
  155. expect('A completely not funny string')->notEndsWith('A completely');
  156. }
  157. public function testEqualsFile()
  158. {
  159. expect('%i')->equalsFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  160. expect('Another string')->notEqualsFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'format-file.txt');
  161. }
  162. public function testStartsWith()
  163. {
  164. expect('A completely not funny string')->startsWith('A completely');
  165. expect('A completely not funny string')->notStartsWith('string');
  166. }
  167. public function testEqualsXmlFile()
  168. {
  169. expect_file(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'xml-test-file.xml')
  170. ->equalsXmlFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'xml-test-file.xml');
  171. expect('<foo><bar>Baz</bar><bar>Baz</bar></foo>')
  172. ->equalsXmlFile(__DIR__ . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'xml-test-file.xml');
  173. }
  174. public function testEqualsXmlString()
  175. {
  176. expect('<foo><bar>Baz</bar><bar>Baz</bar></foo>')
  177. ->equalsXmlString('<foo><bar>Baz</bar><bar>Baz</bar></foo>');
  178. }
  179. }
  180. class FakeClassForTesting
  181. {
  182. static $staticProperty;
  183. }