PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Skajdo/TestSuite/Assert.php

https://bitbucket.org/skajdo/test-suite
PHP | 567 lines | 365 code | 51 blank | 151 comment | 46 complexity | 297b8eb74d0ec8a0dcd2f80d6a5750cf MD5 | raw file
  1. <?php
  2. namespace Skajdo\TestSuite;
  3. use \Skajdo\TestSuite\Exception\TestFailed;
  4. use \Skajdo\TestSuite\Exception\TestSkipped;
  5. use \Enhance\TextFactory;
  6. /**
  7. * Assertion object
  8. * Perform checks for given objects.
  9. */
  10. class Assert
  11. {
  12. /**
  13. * @var Assert
  14. */
  15. protected static $instance;
  16. /**
  17. * @var \Enhance\TextFactory
  18. */
  19. protected $text;
  20. /**
  21. * @return Assert
  22. */
  23. public static function getInstance()
  24. {
  25. if (!self::$instance) {
  26. self::$instance = new self();
  27. }
  28. return self::$instance;
  29. }
  30. /**
  31. * @return \Skajdo\TestSuite\Assert
  32. */
  33. protected function __construct()
  34. {
  35. $this->text = TextFactory::getLanguageText('En');
  36. }
  37. /**
  38. * Tell if given entities are identical.
  39. *
  40. * Objects must reference the same class instance to be identical.
  41. * Multidimensional arrays are handled properly.
  42. *
  43. * @param $expected
  44. * @param $actual
  45. * @throws TestFailed
  46. */
  47. public function isIdentical($expected, $actual)
  48. {
  49. if (is_float($expected)) {
  50. $expected = (string) $expected;
  51. $actual = (string) $actual;
  52. }
  53. if ($expected !== $actual) {
  54. self::testFailed(array(Message::IS_NOT_IDENTICAL, $actual, $expected));
  55. }
  56. }
  57. /**
  58. * Tell if current entities are NOT identical.
  59. *
  60. * Objects are considered not identical event if they are different instanced of the same class.
  61. * Multidimensional arrays are handled properly.
  62. *
  63. * @param $expected
  64. * @param $actual
  65. * @throws TestFailed
  66. */
  67. public function isNotIdentical($expected, $actual)
  68. {
  69. if (is_float($expected)) {
  70. $expected = (string) $expected;
  71. $actual = (string) $actual;
  72. }
  73. if ($expected === $actual) {
  74. self::testFailed(Message::IS_IDENTICAL, array($actual, $expected));
  75. }
  76. }
  77. /**
  78. * @deprecated use Assert::isIdentical()
  79. * @param $expected
  80. * @param $actual
  81. * @throws TestFailed
  82. */
  83. public function areIdentical($expected, $actual)
  84. {
  85. $this->isIdentical($expected, $actual);
  86. }
  87. /**
  88. * @deprecated use Assert::isNotIdentical()
  89. * @param $expected
  90. * @param $actual
  91. * @throws TestFailed
  92. */
  93. public function areNotIdentical($expected, $actual)
  94. {
  95. $this->isNotIdentical($expected, $actual);
  96. }
  97. /**
  98. * Tell if current entitiy equals boolean TRUE
  99. *
  100. * @param $actual
  101. * @throws TestFailed
  102. */
  103. public function isTrue($actual)
  104. {
  105. if ($actual !== true) {
  106. $this->testFailed(Message::EXPECTED_VALUE, array(true, $actual));
  107. }
  108. }
  109. /**
  110. * Tell if current entity is not equal to boolean TRUE
  111. *
  112. * @param $actual
  113. */
  114. public function isNotTrue($actual)
  115. {
  116. if ($actual === true) {
  117. $this->testFailed(Message::NOT_EXPECTED_VALUE, array(true));
  118. }
  119. }
  120. /**
  121. * Tell if current entitiy equals boolean FALSE
  122. *
  123. * @param $actual
  124. * @throws TestFailed
  125. */
  126. public function isFalse($actual)
  127. {
  128. if ($actual !== false) {
  129. $this->testFailed(Message::EXPECTED_VALUE, array(false, $actual));
  130. }
  131. }
  132. /**
  133. * Tell if given string ($actual) contains another string ($expected).
  134. *
  135. * @param $neddle
  136. * @param $haystack
  137. * @param bool $caseSensitive
  138. * @param string $encoding
  139. * @throws TestFailed
  140. */
  141. public function contains($neddle, $haystack, $caseSensitive = true, $encoding = 'utf8')
  142. {
  143. if ($caseSensitive !== true) {
  144. $neddle = mb_strtolower($neddle, $encoding);
  145. $haystack = mb_strtolower($haystack, $encoding);
  146. }
  147. $result = mb_strpos($haystack, $neddle);
  148. if ($result === false) {
  149. $this->testFailed(array(Message::SHOULD_CONTAIN_VALUE, $neddle, $haystack));
  150. }
  151. }
  152. /**
  153. * Make sure that $haystack does not contain $neddle
  154. *
  155. * @param $neddle
  156. * @param $haystack
  157. * @param bool $caseSensitive
  158. * @param string $encoding
  159. * @throws TestFailed
  160. */
  161. public function notContains($neddle, $haystack, $caseSensitive = true, $encoding = 'utf8')
  162. {
  163. if ($caseSensitive !== true) {
  164. $neddle = mb_strtolower($neddle, $encoding);
  165. $haystack = mb_strtolower($haystack, $encoding);
  166. }
  167. $result = mb_strpos($haystack, $neddle);
  168. if ($result !== false) {
  169. $this->testFailed(array(Message::SHOULD_NOT_CONTAIN_VALUE, $neddle, $haystack));
  170. }
  171. }
  172. /**
  173. * Tell if given value is NULL
  174. *
  175. * @param $actual
  176. * @throws TestFailed
  177. */
  178. public function isNull($actual)
  179. {
  180. if ($actual !== null) {
  181. $this->testFailed(Message::EXPECTED_VALUE, array('NULL', $actual));
  182. }
  183. }
  184. /**
  185. * Tell if given value is NOT NULL
  186. *
  187. * @param $actual
  188. * @throws TestFailed
  189. */
  190. public function isNotNull($actual)
  191. {
  192. if ($actual === null) {
  193. $this->testFailed(Message::NOT_EXPECTED_VALUE, array('NULL'));
  194. }
  195. }
  196. /**
  197. * @param $actual
  198. */
  199. public function isArray($actual)
  200. {
  201. if (!is_array($actual)) {
  202. $this->testFailed(Message::EXPECTED_VALUE, array('array', $actual));
  203. }
  204. }
  205. /**
  206. * @param $actual
  207. */
  208. public function isNotArray($actual)
  209. {
  210. if (is_array($actual)) {
  211. if ($actual === null) {
  212. $this->testFailed(Message::NOT_EXPECTED_VALUE, array('array'));
  213. }
  214. }
  215. }
  216. public function isBool($actual)
  217. {
  218. if (!is_bool($actual)) {
  219. throw new TestFailed(
  220. str_replace(
  221. '{0}',
  222. 'null',
  223. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  224. ), 0);
  225. }
  226. }
  227. public function isNotBool($actual)
  228. {
  229. if (is_bool($actual)) {
  230. throw new TestFailed(
  231. str_replace(
  232. '{0}',
  233. 'null',
  234. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  235. ), 0);
  236. }
  237. }
  238. public function isFloat($actual)
  239. {
  240. if (!is_float($actual)) {
  241. throw new TestFailed(
  242. str_replace(
  243. '{0}',
  244. 'null',
  245. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  246. ), 0);
  247. }
  248. }
  249. public function isNotFloat($actual)
  250. {
  251. if (is_float($actual)) {
  252. throw new TestFailed(
  253. str_replace(
  254. '{0}',
  255. 'null',
  256. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  257. ), 0);
  258. }
  259. }
  260. public function isInt($actual)
  261. {
  262. if (!is_int($actual)) {
  263. throw new TestFailed(
  264. str_replace(
  265. '{0}',
  266. 'null',
  267. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  268. ), 0);
  269. }
  270. }
  271. public function isNotInt($actual)
  272. {
  273. if (is_int($actual)) {
  274. throw new TestFailed(
  275. str_replace(
  276. '{0}',
  277. 'null',
  278. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  279. ), 0);
  280. }
  281. }
  282. public function isNumeric($actual)
  283. {
  284. if (!is_numeric($actual)) {
  285. throw new TestFailed(
  286. str_replace(
  287. '{0}',
  288. 'null',
  289. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  290. ), 0);
  291. }
  292. }
  293. public function isNotNumeric($actual)
  294. {
  295. if (is_numeric($actual)) {
  296. throw new TestFailed(
  297. str_replace(
  298. '{0}',
  299. 'null',
  300. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  301. ), 0);
  302. }
  303. }
  304. public function isObject($actual)
  305. {
  306. if (!is_object($actual)) {
  307. throw new TestFailed(
  308. str_replace(
  309. '{0}',
  310. 'null',
  311. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  312. ), 0);
  313. }
  314. }
  315. public function isNotObject($actual)
  316. {
  317. if (is_object($actual)) {
  318. throw new TestFailed(
  319. str_replace(
  320. '{0}',
  321. 'null',
  322. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  323. ), 0);
  324. }
  325. }
  326. public function isResource($actual)
  327. {
  328. if (!is_resource($actual)) {
  329. throw new TestFailed(
  330. str_replace(
  331. '{0}',
  332. 'null',
  333. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  334. ), 0);
  335. }
  336. }
  337. public function isNotResource($actual)
  338. {
  339. if (is_resource($actual)) {
  340. throw new TestFailed(
  341. str_replace(
  342. '{0}',
  343. 'null',
  344. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  345. ), 0);
  346. }
  347. }
  348. public function isScalar($actual)
  349. {
  350. if (!is_scalar($actual)) {
  351. throw new TestFailed(
  352. str_replace(
  353. '{0}',
  354. 'null',
  355. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  356. ), 0);
  357. }
  358. }
  359. public function isNotScalar($actual)
  360. {
  361. if (is_scalar($actual)) {
  362. throw new TestFailed(
  363. str_replace(
  364. '{0}',
  365. 'null',
  366. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  367. ), 0);
  368. }
  369. }
  370. public function isString($actual)
  371. {
  372. if (!is_string($actual)) {
  373. throw new TestFailed(
  374. str_replace(
  375. '{0}',
  376. 'null',
  377. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedButWas)
  378. ), 0);
  379. }
  380. }
  381. public function isNotString($actual)
  382. {
  383. if (is_string($actual)) {
  384. throw new TestFailed(
  385. str_replace(
  386. '{0}',
  387. 'null',
  388. str_replace('{1}', $this->getDescription($actual), $this->text->FormatForExpectedNotButWas)
  389. ), 0);
  390. }
  391. }
  392. /**
  393. * @param string $message
  394. * @throws TestFailed
  395. */
  396. public function fail($message = null)
  397. {
  398. throw new TestFailed(Message::format(Message::TestFailed, array($message)));
  399. }
  400. /**
  401. * @deprecated use skip() instead
  402. * @throws TestSkipped
  403. */
  404. public function inconclusive()
  405. {
  406. $this->skip();
  407. }
  408. /**
  409. * Skip given test
  410. *
  411. * @throws TestSkipped
  412. */
  413. public function skip($message = null)
  414. {
  415. if($message === null){
  416. $message = Message::TEST_SKIPPED;
  417. }
  418. throw new TestSkipped($message);
  419. }
  420. /**
  421. * Object must be an instance of expected class
  422. */
  423. public function isInstanceOfClass($expected, $actual)
  424. {
  425. $actualType = get_class($actual);
  426. if ($expected !== $actualType) {
  427. throw new TestFailed($actualType .' is not an instance of class ' . $expected);
  428. }
  429. }
  430. /**
  431. * Tell if current object is an instance of given type
  432. *
  433. * @deprecated
  434. * @param string $expected
  435. * @param mixed $actual
  436. * @throws Exception\TestFailed
  437. */
  438. public function isInstanceOfType($expected, $actual)
  439. {
  440. $this->isInstanceOf($expected, $actual);
  441. }
  442. /**
  443. * @depreacated
  444. * @param $expected
  445. * @param $actual
  446. * @throws Exception\TestFailed
  447. */
  448. public function isNotInstanceOfType($expected, $actual)
  449. {
  450. $this->isNotInstanceOf($expected, $actual);
  451. }
  452. /**
  453. * Tell if current object is an instance of given type
  454. *
  455. * @param string $expected
  456. * @param mixed $actual
  457. * @throws Exception\TestFailed
  458. */
  459. public function isInstanceOf($expected, $actual)
  460. {
  461. if(!is_object($actual) || !is_a($actual, $expected)){
  462. $this->testFailed(Message::IS_NOT_INSTANCEOF, array($actual, $expected));
  463. }
  464. }
  465. /**
  466. * Tell if current object is NOT an instance of given type
  467. *
  468. * @param string $expected
  469. * @param mixed $actual
  470. * @throws Exception\TestFailed
  471. */
  472. public function isNotInstanceOf($expected, $actual)
  473. {
  474. if(!is_object($actual) || !is_a($actual, $expected)){
  475. $this->testFailed(Message::IS_INSTANCEOF, array($actual, $expected));
  476. }
  477. }
  478. public function throws($class, $methodName, $args = null)
  479. {
  480. $exception = false;
  481. try {
  482. if ($args !== null) {
  483. /** @noinspection PhpParamsInspection */
  484. call_user_func_array(array($class, $methodName), $args);
  485. } else {
  486. $class->{$methodName}();
  487. }
  488. } catch (\Exception $e) {
  489. $exception = true;
  490. }
  491. if (!$exception) {
  492. throw new TestFailed($this->text->ExpectedExceptionNotThrown, 0);
  493. }
  494. }
  495. private static function getDescription($mixed)
  496. {
  497. if (is_object($mixed)) {
  498. return get_class($mixed);
  499. } else {
  500. if (is_bool($mixed)) {
  501. return $mixed ? 'true' : 'false';
  502. } else {
  503. return (string)$mixed;
  504. }
  505. }
  506. }
  507. /**
  508. * @param $message
  509. * @param array $arguments
  510. * @throws Exception\TestFailed
  511. */
  512. protected static function testFailed($message, array $arguments = array())
  513. {
  514. throw new TestFailed(Message::format($message, $arguments), 0);
  515. }
  516. }