Skip to content

Commit 2deb896

Browse files
authored
Merge pull request #1 from coderfoxbrasil/hjjunior/fix-edge-cases
Adds tests of edge cases
2 parents 67867a7 + e14a493 commit 2deb896

File tree

8 files changed

+133
-15
lines changed

8 files changed

+133
-15
lines changed

.github/workflows/static.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Static Analysis
22

3-
on: ['push', 'pull_request']
3+
on: ["push", "pull_request"]
44

55
jobs:
66
static:
@@ -14,13 +14,15 @@ jobs:
1414
- name: Setup PHP
1515
uses: shivammathur/setup-php@v2
1616
with:
17-
php-version: 8.1
17+
php-version: 8.2
1818
tools: composer:v2
1919
coverage: none
2020

2121
- name: Install Dependencies
2222
run: composer update --prefer-stable --no-interaction --no-progress --ansi
2323

24+
run: composer require composer/composer --prefer-source
25+
2426
- name: Types
2527
run: composer test:types
2628

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
name: Tests
22

3-
on: ['push', 'pull_request']
3+
on: ["push", "pull_request"]
44

55
jobs:
66
ci:
77
runs-on: ${{ matrix.os }}
88
strategy:
99
matrix:
1010
os: [ubuntu-latest] # (macos-latest, windows-latest) 2.x-dev is under development
11-
php: ['8.1', '8.2']
12-
parallel: ['', '--parallel']
11+
php: ["8.2", "8.3", "8.4"]
12+
parallel: ["", "--parallel"]
1313

1414
name: PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.parallel }}
1515

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44

55
> PSpec is a Pest plugin for composing multi scenarios tests with a simple API, based on RSpec let.
66
7+
## Important
8+
9+
This plugin requires pest >= 3.5.0
10+
711
### Install
12+
813
```shell
914
composer require cfx/pspec --dev
1015
```
1116

1217
### Simple usage
18+
1319
```php
1420
use function Cfx\PSpec\context;
1521
use function Cfx\PSpec\expectSubject;
@@ -25,7 +31,7 @@ context('when is admin', function () {
2531
it('returns true', function () {
2632
expectSubject()->is_admin->toBeTrue();
2733
});
28-
});
34+
});
2935

3036
context('when is not admin', function () {
3137
let('is_admin', fn() => false);
@@ -56,5 +62,3 @@ context('when using high order testing', function () {
5662
```
5763

5864
[more examples](https://github.com/coderfoxbrasil/cfx-pspec/blob/master/tests/Example.php)
59-
60-

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
],
1919
"license": "MIT",
2020
"require": {
21-
"php": "^8.1",
22-
"pestphp/pest": "^2.0.0",
23-
"pestphp/pest-plugin": "^2.0.0"
21+
"php": "^8.2",
22+
"pestphp/pest": "^3.5.0",
23+
"pestphp/pest-plugin": "^3.0.0"
2424
},
2525
"autoload": {
2626
"psr-4": {
@@ -31,7 +31,7 @@
3131
]
3232
},
3333
"require-dev": {
34-
"pestphp/pest-dev-tools": "^2.0.0"
34+
"pestphp/pest-dev-tools": "^3.0.0"
3535
},
3636
"minimum-stability": "dev",
3737
"prefer-stable": true,

src/Autoload.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111
use Pest\Support\Backtrace;
1212
use Pest\TestSuite;
1313

14-
function subject(Closure $subject): void
14+
function subject(Closure $subject): BeforeEachCall
1515
{
16-
new SubjectTester($subject);
16+
$filename = Backtrace::testFile();
17+
18+
return new BeforeEachCall(
19+
TestSuite::getInstance(),
20+
$filename,
21+
fn () => new SubjectTester($subject),
22+
);
1723
}
1824

1925
function let(string $key, Closure $resolver): BeforeEachCall
@@ -34,7 +40,9 @@ function get(string $key): mixed
3440

3541
function context(string $description, Closure $tests): DescribeCall
3642
{
37-
return SubjectTester::getInstance()->context($description, $tests);
43+
return describe($description, function () use ($tests) {
44+
return $tests();
45+
});
3846
}
3947

4048
function getSubject(): mixed

tests/MultipleSubjects.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use function Cfx\PSpec\getSubject;
4+
use function Cfx\PSpec\subject;
5+
6+
subject(fn () => 3);
7+
8+
it('uses the root subject')
9+
->expect(getSubject(...))
10+
->toEqual(3);
11+
12+
describe('subject 1', function () {
13+
subject(fn () => 1);
14+
15+
it('gets subject 1')
16+
->expect(getSubject(...))
17+
->toEqual(1);
18+
19+
describe('nested block', function () {
20+
it('gets subject 1')
21+
->expect(getSubject(...))
22+
->toEqual(1);
23+
});
24+
});
25+
26+
describe('subject 2', function () {
27+
subject(fn () => 2);
28+
29+
it('gets subject 2')
30+
->expect(getSubject(...))
31+
->toEqual(2);
32+
33+
describe('nested block', function () {
34+
it('gets subject 2')
35+
->expect(getSubject(...))
36+
->toEqual(2);
37+
});
38+
});

tests/NestedBlocks.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use function Cfx\PSpec\get;
4+
use function Cfx\PSpec\getSubject;
5+
use function Cfx\PSpec\let;
6+
use function Cfx\PSpec\subject;
7+
8+
describe('class_name', function () {
9+
subject(fn () => get('variable'));
10+
11+
let('variable', fn () => 'from root');
12+
13+
it('gets the variable from root variable')
14+
->expect(getSubject(...))
15+
->toEqual('from root');
16+
17+
describe('.method_name', function () {
18+
it('gets the variable from root variable')
19+
->expect(getSubject(...))
20+
->toEqual('from root');
21+
22+
describe('can override', function () {
23+
let('variable', fn () => 'overrided');
24+
25+
it('gets the variable from local scope')
26+
->expect(getSubject(...))
27+
->toEqual('overrided');
28+
29+
describe('sub nested block', function () {
30+
it('gets variable from parent block')
31+
->expect(getSubject(...))
32+
->toEqual('overrided');
33+
});
34+
});
35+
});
36+
});
37+
38+
describe('no direct tests block', function () {
39+
subject(fn () => get('variable2'));
40+
41+
let('variable2', fn () => 'from root');
42+
43+
describe('nested block', function () {
44+
it('gets the variable from root variable')
45+
->expect(getSubject(...))
46+
->toEqual('from root');
47+
});
48+
});

tests/NoDirectTests.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use function Cfx\PSpec\get;
4+
use function Cfx\PSpec\getSubject;
5+
use function Cfx\PSpec\let;
6+
use function Cfx\PSpec\subject;
7+
8+
describe('no direct tests block', function () {
9+
subject(fn () => get('variable2'));
10+
11+
let('variable2', fn () => 'from root');
12+
13+
describe('nested block', function () {
14+
it('gets the variable from root variable')
15+
->expect(getSubject(...))
16+
->toEqual('from root');
17+
});
18+
});

0 commit comments

Comments
 (0)