-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAssert.php
115 lines (108 loc) · 2.99 KB
/
Assert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/*
* Copyright (c) Ouzo contributors, https://github.com/letsdrink/ouzo
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Tests;
use Ouzo\Model;
/**
* Class Assert
* @package Ouzo\Tests
*/
class Assert
{
/**
* Fluent custom array assertion to simplify your tests.
*
* Sample usage:
* <code>
* $animals = array('cat', 'dog', 'pig');
* Assert::thatArray($animals)->hasSize(3)->contains('cat');
* Assert::thatArray($animals)->containsOnly('pig', 'dog', 'cat');
* Assert::thatArray($animals)->containsExactly('cat', 'dog', 'pig');
* Assert::thatArray(array('id' => 123, 'name' => 'john'))->containsKeyAndValue(array('id' => 123));
* </code>
*
* @param array|null $actual
* @return ArrayAssert
*/
public static function thatArray(?array $actual): ArrayAssert
{
return ArrayAssert::that($actual);
}
/**
* Fluent custom model assertion to simplify your tests.
*
* Sample usage:
* <code>
* Assert::thatModel(new User(['name' => 'bob']))->hasSameAttributesAs(new User(['name' => 'bob']));
* </code>
*
* @param Model|null $actual
* @return ModelAssert
*/
public static function thatModel(?Model $actual): ModelAssert
{
return ModelAssert::that($actual);
}
/**
* Fluent custom session assertion to simplify your tests.
*
* Sample usage:
* <code>
* Session::push('key1', 'key2', 'value1');
* Assert::thatSession()->hasSize(1)->contains('value1');
* </code>
*
* @return ArrayAssert
*/
public static function thatSession(): ArrayAssert
{
return ArrayAssert::that(isset($_SESSION) ? $_SESSION : []);
}
/**
* Fluent custom string assertion to simplify your tests.
*
* Sample usage:
* <code>
* Assert::thatString("Frodo")->startsWith("Fro")->endsWith("do")->contains("rod")->doesNotContain("fro")->hasSize(5)->matches('/Fro\w+/');
* </code>
*
* @param ?string $string
* @return StringAssert
*/
public static function thatString(?string $string): StringAssert
{
return StringAssert::that($string);
}
/**
* Fluent custom boolean assertion to simplify your tests.
*
* Sample usage:
* <code>
* Assert::thatBool(isCool())->isTrue();
* </code>
*
* @param ?bool $string
* @return BooleanAssert
*/
public static function thatBool(?bool $string): BooleanAssert
{
return BooleanAssert::that($string);
}
/**
* Fluent custom primitives' and objects' assertion to simplify your tests.
*
* Sample usage:
* <code>
* Assert::that($object)->isInstanceOf(Controller::class);
* </code>
*
* @param mixed $subject
* @return GeneralAssert
*/
public static function that(mixed $subject): GeneralAssert
{
return GeneralAssert::that($subject);
}
}