SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/validityTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\Validity;
  15. use Sprout\Exceptions\ValidationException;
  16.  
  17.  
  18. class validityTest extends PHPUnit_Framework_TestCase
  19. {
  20.  
  21. public function dataEmail() {
  22. return [
  23. ['aa', false],
  24. ['test@example.com', true],
  25. ['@example.com', false],
  26. ['test@', false],
  27. ];
  28. }
  29.  
  30. /**
  31.   * @dataProvider dataEmail
  32.   */
  33. public function testEmail($value, $success)
  34. {
  35. try {
  36. Validity::email($value);
  37. if (!$success) $this->assertFalse(true);
  38. } catch (ValidationException $ex) {
  39. if ($success) $this->assertFalse(true);
  40. }
  41. }
  42.  
  43.  
  44.  
  45. public function dataPositiveInt() {
  46. return [
  47. ['0', false],
  48. ['1', true],
  49. ['1000', true],
  50. ['-1', false],
  51. ['test', false],
  52. ];
  53. }
  54.  
  55. /**
  56.   * @dataProvider dataPositiveInt
  57.   */
  58. public function testPositiveInt($value, $success)
  59. {
  60. try {
  61. Validity::positiveInt($value);
  62. if (!$success) $this->assertFalse(true);
  63. } catch (ValidationException $ex) {
  64. if ($success) $this->assertFalse(true);
  65. }
  66. }
  67.  
  68.  
  69.  
  70. public function dataBinary() {
  71. return [
  72. ['0', true],
  73. ['1', true],
  74. [0, true],
  75. [1, true],
  76. // Anything else is invalid
  77. ["\0", false],
  78. ['01', false],
  79. [array(), false],
  80. ['1000', false],
  81. ['-1', false],
  82. ['test', false],
  83. [true, false],
  84. [false, false],
  85. ['false', false],
  86. ['true', false],
  87. ];
  88. }
  89.  
  90. /**
  91.   * @dataProvider dataBinary
  92.   */
  93. public function testBinary($value, $success)
  94. {
  95. try {
  96. Validity::binary($value);
  97. if (!$success) $this->assertFalse(true);
  98. } catch (ValidationException $ex) {
  99. if ($success) $this->assertFalse(true);
  100. }
  101. }
  102.  
  103.  
  104.  
  105. public function dataDateMySQL() {
  106. return [
  107. ['0000-00-00', false],
  108. ['1980-01-01', true],
  109. ['1970-01-01', true],
  110. ['1900-01-01', true],
  111. ['2100-01-01', true],
  112. ['', false],
  113. ['1899-01-01', false],
  114. ['2101-01-01', false],
  115. ['1970-00-01', false],
  116. ['1970-13-01', false],
  117. ['1970-01-00', false],
  118. ['1970-01-32', false],
  119. ];
  120. }
  121.  
  122. /**
  123.   * @dataProvider dataDateMySQL
  124.   */
  125. public function testDateMySQL($value, $success)
  126. {
  127. try {
  128. Validity::dateMySQL($value);
  129. if (!$success) $this->assertFalse(true);
  130. } catch (ValidationException $ex) {
  131. if ($success) $this->assertFalse(true);
  132. }
  133. }
  134. }
  135.