Skip to content

Commit bf2cb1f

Browse files
author
Christian Blank
committed
Write more specs
1 parent f3a18df commit bf2cb1f

10 files changed

+126
-12
lines changed

spec/Type/AnyTypeSpec.php

+24
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,28 @@ function it_is_initializable()
1212
{
1313
$this->shouldHaveType(AnyType::class);
1414
}
15+
16+
function it_should_return_valid_for_null()
17+
{
18+
$this->check(null)->isValid()->shouldBe(true);
19+
}
20+
21+
function it_should_return_empty_errors_for_null()
22+
{
23+
$this->check(null)->getErrors()->shouldHaveCount(0);
24+
}
25+
26+
function it_should_return_valid_for_all_values()
27+
{
28+
$this->check(true)->isValid()->shouldBe(true);
29+
$this->check("foo")->isValid()->shouldBe(true);
30+
$this->check(13)->isValid()->shouldBe(true);
31+
}
32+
33+
function it_should_return_empty_errors_for_all_values()
34+
{
35+
$this->check(true)->getErrors()->shouldHaveCount(0);
36+
$this->check("foo")->getErrors()->shouldHaveCount(0);
37+
$this->check(13)->getErrors()->shouldHaveCount(0);
38+
}
1539
}

spec/Type/BoolTypeSpec.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,29 @@
22

33
namespace spec\StructureCheck\Type;
44

5-
use StructureCheck\Type\BoolType;
65
use PhpSpec\ObjectBehavior;
7-
use Prophecy\Argument;
6+
use StructureCheck\Type\BoolType;
87

98
class BoolTypeSpec extends ObjectBehavior
109
{
10+
1111
function it_is_initializable()
1212
{
1313
$this->shouldHaveType(BoolType::class);
1414
}
15+
16+
function it_should_return_valid_for_bool()
17+
{
18+
$this->check(true)->isValid()->shouldBe(true);
19+
$this->check(false)->isValid()->shouldBe(true);
20+
}
21+
22+
function it_should_return_invalid_for_others()
23+
{
24+
$this->check(null)->isValid()->shouldBe(false);
25+
$this->check("foo")->isValid()->shouldBe(false);
26+
$this->check([])->isValid()->shouldBe(false);
27+
$this->check(1)->isValid()->shouldBe(false);
28+
$this->check(1.0)->isValid()->shouldBe(false);
29+
}
1530
}

spec/Type/FloatTypeSpec.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@
44

55
use StructureCheck\Type\FloatType;
66
use PhpSpec\ObjectBehavior;
7-
use Prophecy\Argument;
87

98
class FloatTypeSpec extends ObjectBehavior
109
{
1110
function it_is_initializable()
1211
{
1312
$this->shouldHaveType(FloatType::class);
1413
}
14+
15+
function it_should_return_valid_for_floats()
16+
{
17+
$this->check(0.0)->isValid()->shouldBe(true);
18+
$this->check(1.1)->isValid()->shouldBe(true);
19+
$this->check(2.0)->isValid()->shouldBe(true);
20+
$this->check(-144.2)->isValid()->shouldBe(true);
21+
}
22+
23+
function it_should_return_invalid_for_others()
24+
{
25+
$this->check(null)->isValid()->shouldBe(false);
26+
$this->check("foo")->isValid()->shouldBe(false);
27+
$this->check([])->isValid()->shouldBe(false);
28+
$this->check(1)->isValid()->shouldBe(false);
29+
$this->check(true)->isValid()->shouldBe(false);
30+
$this->check(false)->isValid()->shouldBe(false);
31+
}
1532
}

spec/Type/IntTypeSpec.php

+18
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,22 @@ function it_is_initializable()
1212
{
1313
$this->shouldHaveType(IntType::class);
1414
}
15+
16+
function it_should_return_valid_for_integers()
17+
{
18+
$this->check(0)->isValid()->shouldBe(true);
19+
$this->check(1)->isValid()->shouldBe(true);
20+
$this->check(20)->isValid()->shouldBe(true);
21+
$this->check(-144)->isValid()->shouldBe(true);
22+
}
23+
24+
function it_should_return_invalid_for_others()
25+
{
26+
$this->check(null)->isValid()->shouldBe(false);
27+
$this->check("foo")->isValid()->shouldBe(false);
28+
$this->check([])->isValid()->shouldBe(false);
29+
$this->check(1.234)->isValid()->shouldBe(false);
30+
$this->check(true)->isValid()->shouldBe(false);
31+
$this->check(false)->isValid()->shouldBe(false);
32+
}
1533
}

spec/Type/NullableTypeSpec.php

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace spec\StructureCheck\Type;
44

5+
use StructureCheck\Result;
56
use StructureCheck\Type\NullableType;
67
use PhpSpec\ObjectBehavior;
7-
use Prophecy\Argument;
88
use StructureCheck\Type\TypeInterface;
99

1010
class NullableTypeSpec extends ObjectBehavior
@@ -14,4 +14,15 @@ function it_is_initializable(TypeInterface $childType)
1414
$this->beConstructedWith($childType);
1515
$this->shouldHaveType(NullableType::class);
1616
}
17+
18+
function it_should_return_valid_for_null(TypeInterface $childType) {
19+
$this->beConstructedWith($childType);
20+
$this->check(null)->isValid()->shouldBe(true);
21+
}
22+
23+
function it_should_return_the_value_from_the_child(TypeInterface $childType) {
24+
$this->beConstructedWith($childType);
25+
$childType->check(false)->willReturn(new Result(false, []));
26+
$this->check(false)->isValid()->shouldBe(false);
27+
}
1728
}

spec/Type/StringTypeSpec.php

+16
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,20 @@ function it_is_initializable()
1212
{
1313
$this->shouldHaveType(StringType::class);
1414
}
15+
16+
function it_should_return_valid_for_strings()
17+
{
18+
$this->check("")->isValid()->shouldBe(true);
19+
$this->check("fooo")->isValid()->shouldBe(true);
20+
$this->check('adadsad asd a')->isValid()->shouldBe(true);
21+
}
22+
23+
function it_should_return_invalid_for_others()
24+
{
25+
$this->check(null)->isValid()->shouldBe(false);
26+
$this->check(12.3)->isValid()->shouldBe(false);
27+
$this->check([])->isValid()->shouldBe(false);
28+
$this->check(-1)->isValid()->shouldBe(false);
29+
$this->check(true)->isValid()->shouldBe(false);
30+
}
1531
}

src/Result.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function __construct($valid, array $errors)
3636
*/
3737
public function isValid()
3838
{
39-
return true;
39+
return $this->valid;
4040
}
4141

4242
/**
4343
* @inheritdoc
4444
*/
4545
public function getErrors()
4646
{
47-
return [];
47+
return $this->errors;
4848
}
4949
}

src/Type/AnyType.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
namespace StructureCheck\Type;
44

5-
class AnyType
5+
use StructureCheck\Result;
6+
7+
/**
8+
* Class AnyType
9+
* @package StructureCheck\Type
10+
*/
11+
class AnyType implements TypeInterface
612
{
7-
}
13+
/**
14+
* @inheritdoc
15+
*/
16+
public function check($value)
17+
{
18+
return new Result(true, []);
19+
}
20+
}

src/Type/BoolType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class BoolType implements TypeInterface
2020
*/
2121
public function check($value)
2222
{
23-
$checkResult = is_string($value);
23+
$checkResult = is_bool($value);
2424

2525
return new Result(
2626
$checkResult,

src/Type/FloatType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace StructureCheck\Type;
44

5+
use StructureCheck\Result;
6+
57
class FloatType implements TypeInterface
68
{
79
private static $errorMessage = 'The value %s is not a float.';
810

911
/**
10-
* @param mixed $value
11-
*
12-
* @return ResultInterface
12+
* @inheritdoc
1313
*/
1414
public function check($value)
1515
{

0 commit comments

Comments
 (0)