Skip to content

Commit d7cf443

Browse files
committed
Verify 2.1
* Added new expect-toBe and expect-notTo BDD Syntax.
1 parent 66b075b commit d7cf443

31 files changed

Lines changed: 2662 additions & 140 deletions

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 2.1
4+
5+
* Added new expect-toBe and expect-notTo BDD Syntax.
6+
* Added full documentation about Expectations.
7+
* Fixed minor bugs.
8+
* Deleted RoboFile and VERSION file.
9+
* **BC:** `expect` function now works with expectations instead of verifiers.
10+
311
## 2.0
412

513
* Support for Chained Verifiers.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2013-2015 Codeception PHP Testing Framework
3+
Copyright (c) 2013-2020 Codeception PHP Testing Framework
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,19 @@ Verify::Callable($callback)
8383
8484
## Alternative Syntax
8585

86-
If you follow TDD/BDD you'd rather use `expect` or `verify_that` instead of `verify`. Which are just an alias function:
86+
If you follow TDD/BDD you'd rather use `expect` instead of `verify`:
8787

8888
```php
89-
expect($user->getNumPosts())->equals(5, 'user have 5 posts');
89+
expect($user->getNumPosts())
90+
->notToBeNull()
91+
->toBeInt()
92+
->toEqual(5, 'user have 5 posts');
93+
```
94+
> :page_facing_up: **See Expectations full list [here.][7]**
95+
>
96+
Or `verify_that` which is just an alias function:
9097

98+
```php
9199
verify_that($user->getRate())->equals(7, 'first user rate is 7');
92100
```
93101

@@ -117,7 +125,7 @@ class MyVerify extends Verify {
117125
```
118126

119127
And use it!
120-
128+
121129
```php
122130
$myVerify = new MyVerify;
123131

RoboFile.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/supported_expectations.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
## Expectations List
2+
3+
`expect()` supports all the expectations listed here! :rocket:
4+
5+
### Array
6+
```
7+
notToContain
8+
notToContainEqual
9+
notToContainOnly
10+
notToHaveCount
11+
notToHaveKey
12+
notToHaveSameSizeAs
13+
toContain
14+
toContainEqual
15+
toContainOnly
16+
toContainOnlyInstancesOf
17+
toHaveCount
18+
toHaveKey
19+
toHaveSameSizeAs
20+
```
21+
22+
### BaseObject
23+
```
24+
notToHaveAttribute
25+
toHaveAttribute
26+
```
27+
28+
### Callable
29+
```
30+
notToThrow
31+
toThrow
32+
```
33+
34+
### Class
35+
```
36+
notToHaveAttribute
37+
notToHaveStaticAttribute
38+
toHaveAttribute
39+
toHaveStaticAttribute
40+
```
41+
42+
### Directory
43+
```
44+
notToBeReadable
45+
notToBeWritable
46+
notToExist
47+
toBeReadable
48+
toBeWritable
49+
toExist
50+
toExistAndNotToBeReadable
51+
toExistAndNotToBeWritable
52+
toExistAndToBeReadable
53+
toExistAndToBeWritable
54+
```
55+
56+
### File
57+
```
58+
notToBeReadable
59+
notToBeWritable
60+
notToExist
61+
toBeEqual
62+
toBeEqualCanonicalizing
63+
toBeEqualIgnoringCase
64+
toBeReadable
65+
toBeWritable
66+
toExist
67+
toExistAndNotToBeReadable
68+
toExistAndNotToBeWritable
69+
toExistAndToBeReadable
70+
toExistAndToBeWritable
71+
toNotEqual
72+
toNotEqualCanonicalizing
73+
toNotEqualIgnoringCase
74+
```
75+
76+
### JsonFile
77+
```
78+
notToEqualJsonFile
79+
toEqualJsonFile
80+
```
81+
82+
### JsonString
83+
```
84+
notToEqualJsonFile
85+
notToEqualJsonString
86+
toEqualJsonFile
87+
toEqualJsonString
88+
```
89+
90+
### Mixed
91+
```
92+
notToBe
93+
notToBeArray
94+
notToBeBool
95+
notToBeCallable
96+
notToBeClosedResource
97+
notToBeEmpty
98+
notToBeFalse
99+
notToBeFloat
100+
notToBeInstanceOf
101+
notToBeInt
102+
notToBeIterable
103+
notToBeNull
104+
notToBeNumeric
105+
notToBeObject
106+
notToBeResource
107+
notToBeScalar
108+
notToBeString
109+
notToBeTrue
110+
notToEqual
111+
notToEqualCanonicalizing
112+
notToEqualIgnoringCase
113+
notToEqualWithDelta
114+
toBe
115+
toBeArray
116+
toBeBool
117+
toBeCallable
118+
toBeClosedResource
119+
toBeEmpty
120+
toBeFalse
121+
toBeFinite
122+
toBeFloat
123+
toBeGreaterThan
124+
toBeGreaterThanOrEqualTo
125+
toBeInfinite
126+
toBeInstanceOf
127+
toBeInt
128+
toBeIterable
129+
toBeLessThan
130+
toBeLessThanOrEqualTo
131+
toBeNan
132+
toBeNull
133+
toBeNumeric
134+
toBeObject
135+
toBeResource
136+
toBeScalar
137+
toBeString
138+
toBeTrue
139+
toEqual
140+
toEqualCanonicalizing
141+
toEqualIgnoringCase
142+
toEqualWithDelta
143+
```
144+
145+
### String
146+
```
147+
notToContainString
148+
notToContainStringIgnoringCase
149+
notToEndWith
150+
notToEqualFile
151+
notToEqualFileCanonicalizing
152+
notToEqualFileIgnoringCase
153+
notToMatchFormat
154+
notToMatchFormatFile
155+
notToMatchRegExp
156+
notToStartWith
157+
toBeJson
158+
toContainString
159+
toContainStringIgnoringCase
160+
toEndWith
161+
toEqualFile
162+
toEqualFileCanonicalizing
163+
toEqualFileIgnoringCase
164+
toMatchFormat
165+
toMatchFormatFile
166+
toMatchRegExp
167+
toStartWith
168+
```
169+
170+
### XmlFile
171+
```
172+
notToEqualXmlFile
173+
toEqualXmlFile
174+
```
175+
176+
### XmlString
177+
```
178+
notToEqualXmlFile
179+
notToEqualXmlString
180+
toEqualXmlFile
181+
toEqualXmlString
182+
```

docs/supported_verifiers.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ notHasStaticAttribute
4343
```
4444
doesNotExist
4545
exists
46+
existsAndIsNotReadable
47+
existsAndIsNotWritable
48+
existsAndIsReadable
49+
existsAndIsWritable
4650
isNotReadable
4751
isNotWritable
4852
isReadable
@@ -56,6 +60,10 @@ equals
5660
equalsCanonicalizing
5761
equalsIgnoringCase
5862
exists
63+
existsAndIsNotReadable
64+
existsAndIsNotWritable
65+
existsAndIsReadable
66+
existsAndIsWritable
5967
isNotReadable
6068
isNotWritable
6169
isReadable
@@ -131,7 +139,6 @@ notSame
131139
notTrue
132140
null
133141
same
134-
that
135142
true
136143
```
137144

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Codeception\Verify\Asserts;
4+
5+
use Exception;
6+
use PHPUnit\Framework\Assert;
7+
use PHPUnit\Framework\ExpectationFailedException;
8+
use Throwable;
9+
10+
trait AssertThrows
11+
{
12+
public function assertThrows($throws = null, $message = false): self
13+
{
14+
if ($throws instanceof Exception) {
15+
$message = $throws->getMessage();
16+
$throws = get_class($throws);
17+
}
18+
19+
try {
20+
call_user_func($this->actual);
21+
} catch (Throwable $exception) {
22+
if (!$throws) {
23+
return $this; // it throws
24+
}
25+
26+
$actualThrows = get_class($exception);
27+
$actualMessage = $exception->getMessage();
28+
29+
Assert::assertSame($throws, $actualThrows, sprintf('exception \'%s\' was expected, but \'%s\' was thrown', $throws, $actualThrows));
30+
31+
if ($message) {
32+
Assert::assertSame($message, $actualMessage, sprintf('exception message \'%s\' was expected, but \'%s\' was received', $message, $actualMessage));
33+
}
34+
}
35+
36+
if (!isset($exception)) {
37+
throw new ExpectationFailedException(sprintf('exception \'%s\' was not thrown as expected', $throws));
38+
}
39+
40+
return $this;
41+
}
42+
43+
public function assertDoesNotThrow($throws = null, $message = false): self
44+
{
45+
if ($throws instanceof Exception) {
46+
$message = $throws->getMessage();
47+
$throws = get_class($throws);
48+
}
49+
50+
try {
51+
call_user_func($this->actual);
52+
} catch (Throwable $exception) {
53+
if (!$throws) {
54+
throw new ExpectationFailedException('exception was not expected to be thrown');
55+
}
56+
57+
$actualThrows = get_class($exception);
58+
$actualMessage = $exception->getMessage();
59+
60+
if ($throws !== $actualThrows) {
61+
return $this;
62+
}
63+
64+
if (!$message) {
65+
throw new ExpectationFailedException(sprintf('exception \'%s\' was not expected to be thrown', $throws));
66+
}
67+
68+
if ($message === $actualMessage) {
69+
throw new ExpectationFailedException(sprintf('exception \'%s\' with message \'%s\' was not expected to be thrown', $throws, $message));
70+
}
71+
}
72+
73+
return $this;
74+
}
75+
}

0 commit comments

Comments
 (0)