Skip to content

Commit

Permalink
Merge pull request #1 from coderfoxbrasil/hjjunior/fix-edge-cases
Browse files Browse the repository at this point in the history
Adds tests of edge cases
  • Loading branch information
hjJunior authored Nov 9, 2024
2 parents 67867a7 + e14a493 commit 2deb896
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 15 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Static Analysis

on: ['push', 'pull_request']
on: ["push", "pull_request"]

jobs:
static:
Expand All @@ -14,13 +14,15 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.2
tools: composer:v2
coverage: none

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

run: composer require composer/composer --prefer-source

- name: Types
run: composer test:types

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: Tests

on: ['push', 'pull_request']
on: ["push", "pull_request"]

jobs:
ci:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest] # (macos-latest, windows-latest) 2.x-dev is under development
php: ['8.1', '8.2']
parallel: ['', '--parallel']
php: ["8.2", "8.3", "8.4"]
parallel: ["", "--parallel"]

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

Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

> PSpec is a Pest plugin for composing multi scenarios tests with a simple API, based on RSpec let.
## Important

This plugin requires pest >= 3.5.0

### Install

```shell
composer require cfx/pspec --dev
```

### Simple usage

```php
use function Cfx\PSpec\context;
use function Cfx\PSpec\expectSubject;
Expand All @@ -25,7 +31,7 @@ context('when is admin', function () {
it('returns true', function () {
expectSubject()->is_admin->toBeTrue();
});
});
});

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

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


8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
],
"license": "MIT",
"require": {
"php": "^8.1",
"pestphp/pest": "^2.0.0",
"pestphp/pest-plugin": "^2.0.0"
"php": "^8.2",
"pestphp/pest": "^3.5.0",
"pestphp/pest-plugin": "^3.0.0"
},
"autoload": {
"psr-4": {
Expand All @@ -31,7 +31,7 @@
]
},
"require-dev": {
"pestphp/pest-dev-tools": "^2.0.0"
"pestphp/pest-dev-tools": "^3.0.0"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
14 changes: 11 additions & 3 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@
use Pest\Support\Backtrace;
use Pest\TestSuite;

function subject(Closure $subject): void
function subject(Closure $subject): BeforeEachCall
{
new SubjectTester($subject);
$filename = Backtrace::testFile();

return new BeforeEachCall(
TestSuite::getInstance(),
$filename,
fn () => new SubjectTester($subject),
);
}

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

function context(string $description, Closure $tests): DescribeCall
{
return SubjectTester::getInstance()->context($description, $tests);
return describe($description, function () use ($tests) {
return $tests();
});
}

function getSubject(): mixed
Expand Down
38 changes: 38 additions & 0 deletions tests/MultipleSubjects.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use function Cfx\PSpec\getSubject;
use function Cfx\PSpec\subject;

subject(fn () => 3);

it('uses the root subject')
->expect(getSubject(...))
->toEqual(3);

describe('subject 1', function () {
subject(fn () => 1);

it('gets subject 1')
->expect(getSubject(...))
->toEqual(1);

describe('nested block', function () {
it('gets subject 1')
->expect(getSubject(...))
->toEqual(1);
});
});

describe('subject 2', function () {
subject(fn () => 2);

it('gets subject 2')
->expect(getSubject(...))
->toEqual(2);

describe('nested block', function () {
it('gets subject 2')
->expect(getSubject(...))
->toEqual(2);
});
});
48 changes: 48 additions & 0 deletions tests/NestedBlocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use function Cfx\PSpec\get;
use function Cfx\PSpec\getSubject;
use function Cfx\PSpec\let;
use function Cfx\PSpec\subject;

describe('class_name', function () {
subject(fn () => get('variable'));

let('variable', fn () => 'from root');

it('gets the variable from root variable')
->expect(getSubject(...))
->toEqual('from root');

describe('.method_name', function () {
it('gets the variable from root variable')
->expect(getSubject(...))
->toEqual('from root');

describe('can override', function () {
let('variable', fn () => 'overrided');

it('gets the variable from local scope')
->expect(getSubject(...))
->toEqual('overrided');

describe('sub nested block', function () {
it('gets variable from parent block')
->expect(getSubject(...))
->toEqual('overrided');
});
});
});
});

describe('no direct tests block', function () {
subject(fn () => get('variable2'));

let('variable2', fn () => 'from root');

describe('nested block', function () {
it('gets the variable from root variable')
->expect(getSubject(...))
->toEqual('from root');
});
});
18 changes: 18 additions & 0 deletions tests/NoDirectTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use function Cfx\PSpec\get;
use function Cfx\PSpec\getSubject;
use function Cfx\PSpec\let;
use function Cfx\PSpec\subject;

describe('no direct tests block', function () {
subject(fn () => get('variable2'));

let('variable2', fn () => 'from root');

describe('nested block', function () {
it('gets the variable from root variable')
->expect(getSubject(...))
->toEqual('from root');
});
});

0 comments on commit 2deb896

Please sign in to comment.