1<?php 2 3/* Copyright (c) 2018 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */ 4require_once("libs/composer/vendor/autoload.php"); 5 6use ILIAS\Refinery\Factory; 7use ILIAS\Data; 8use PHPUnit\Framework\TestCase; 9 10class IsNumericConstraintTest extends TestCase 11{ 12 public function setUp() : void 13 { 14 $this->df = new Data\Factory(); 15 $this->lng = $this->createMock(\ilLanguage::class); 16 $this->f = new Factory($this->df, $this->lng); 17 18 $this->c = $this->f->numeric()->isNumeric(); 19 } 20 21 public function testAccepts1() 22 { 23 $this->assertTrue($this->c->accepts(0)); 24 } 25 26 public function testAccepts2() 27 { 28 $this->assertTrue($this->c->accepts("1")); 29 } 30 31 public function testAccepts3() 32 { 33 $this->assertTrue($this->c->accepts(1)); 34 } 35 36 public function testAccepts4() 37 { 38 $this->assertTrue($this->c->accepts(0x102)); 39 } 40 41 public function testAccepts5() 42 { 43 $this->assertTrue($this->c->accepts(0102)); 44 } 45 46 public function testAccepts6() 47 { 48 $this->assertTrue($this->c->accepts(0b101)); 49 } 50 51 public function testAccepts7() 52 { 53 $this->assertTrue($this->c->accepts(192e0)); 54 } 55 56 public function testAccepts8() 57 { 58 $this->assertTrue($this->c->accepts(9.1)); 59 } 60 61 public function testNotAccepts1() 62 { 63 $this->assertFalse($this->c->accepts(null)); 64 } 65 66 public function testNotAccepts2() 67 { 68 $this->assertFalse($this->c->accepts("foo")); 69 } 70 71 public function testCheckSucceed() 72 { 73 $this->c->check(2); 74 $this->assertTrue(true); // does not throw 75 } 76 77 public function testCheckFails() 78 { 79 $this->expectException(\UnexpectedValueException::class); 80 $this->c->check(""); 81 } 82 83 public function testNoProblemWith() 84 { 85 $this->assertNull($this->c->problemWith(2)); 86 } 87 88 public function testProblemWith() 89 { 90 $this->lng 91 ->expects($this->once()) 92 ->method("txt") 93 ->with("not_numeric") 94 ->willReturn("-%s-"); 95 96 $this->assertEquals("-aa-", $this->c->problemWith("aa")); 97 } 98 99 public function testRestrictOk() 100 { 101 $ok = $this->df->ok(2); 102 103 $res = $this->c->applyTo($ok); 104 $this->assertTrue($res->isOk()); 105 } 106 107 public function testRestrictNotOk() 108 { 109 $not_ok = $this->df->ok(""); 110 111 $this->lng 112 ->expects($this->once()) 113 ->method("txt") 114 ->with("not_numeric_empty_string") 115 ->willReturn("-%s-"); 116 117 $res = $this->c->applyTo($not_ok); 118 $this->assertFalse($res->isOk()); 119 } 120 121 public function testRestrictError() 122 { 123 $error = $this->df->error("error"); 124 125 $res = $this->c->applyTo($error); 126 $this->assertSame($error, $res); 127 } 128 129 public function testWithProblemBuilder() 130 { 131 $new_c = $this->c->withProblemBuilder(function () { 132 return "This was a fault"; 133 }); 134 $this->assertEquals("This was a fault", $new_c->problemWith("")); 135 } 136} 137