From f5e166d936d186f3c04845abd6b64a739d89534d Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Wed, 21 Feb 2024 10:44:07 -0500 Subject: [PATCH] feat: remove `behat/mink` requirement and add new selector API (#125) --- .github/workflows/ci.yml | 21 +- .gitignore | 1 + README.md | 132 +++- attachment.zip | Bin 182 -> 0 bytes composer.json | 26 +- phpstan-baseline.neon | 142 ---- phpstan.neon | 3 - src/Browser.php | 336 ++++---- src/Browser/Assertion/SameUrlAssertion.php | 34 +- src/Browser/Dumper.php | 29 + src/Browser/HttpOptions.php | 2 +- src/Browser/Json.php | 19 + src/Browser/KernelBrowser.php | 178 ++++- src/Browser/PantherBrowser.php | 96 ++- src/Browser/Session.php | 176 +---- src/Browser/Session/Assert.php | 51 -- src/Browser/Session/Driver.php | 72 -- .../Session/Driver/BrowserKitDriver.php | 729 ------------------ src/Browser/Session/Driver/PantherDriver.php | 389 ---------- src/Browser/Session/KernelSession.php | 258 +++++++ src/Browser/Session/PantherSession.php | 162 ++++ tests/BrowserTests.php | 56 +- tests/Fixture/files/page1.html | 39 +- tests/KernelBrowserTests.php | 27 +- 24 files changed, 1168 insertions(+), 1810 deletions(-) delete mode 100644 attachment.zip delete mode 100644 phpstan-baseline.neon create mode 100644 src/Browser/Dumper.php delete mode 100644 src/Browser/Session/Assert.php delete mode 100644 src/Browser/Session/Driver.php delete mode 100644 src/Browser/Session/Driver/BrowserKitDriver.php delete mode 100644 src/Browser/Session/Driver/PantherDriver.php create mode 100644 src/Browser/Session/KernelSession.php create mode 100644 src/Browser/Session/PantherSession.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4175fe..3cc234a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,26 @@ on: jobs: test: - uses: zenstruck/.github/.github/workflows/php-test-symfony.yml@main + name: PHP ${{ matrix.php }}, SF ${{ matrix.symfony }} - ${{ matrix.deps }} + runs-on: ubuntu-latest + strategy: + matrix: + php: [8.1, 8.2, 8.3] + deps: [highest] + symfony: [6.4.*, 7.0.*] + include: + - php: 8.1 + deps: lowest + symfony: '*' + exclude: + - php: 8.1 + symfony: 7.0.* + steps: + - uses: zenstruck/.github/actions/php-test-symfony@main + with: + php: ${{ matrix.php }} + symfony: ${{ matrix.symfony }} + deps: ${{ matrix.deps }} code-coverage: uses: zenstruck/.github/.github/workflows/php-coverage-codecov.yml@main diff --git a/.gitignore b/.gitignore index aa58d4a..e942df1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ /build/ /var/ /drivers/ +/attachment.zip /.php-cs-fixer.cache /.phpunit.result.cache diff --git a/README.md b/README.md index 9bc9a0d..cdd1b7a 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,6 @@ previous request didn't perform any security-related operations. Possible soluti for all requests removing the need to ever call `->withProfiling()` but can slow down your tests. - #### HTTP Requests The _KernelBrowser_ can be used for testing API endpoints. The following http methods are available: @@ -411,7 +410,7 @@ $browser Make assertions about json responses using [JMESPath expressions](https://jmespath.org/) See the [JMESPath Tutorials](https://jmespath.org/tutorial.html) to learn more. -> **Note** +> [!NOTE] > `mtdowling/jmespath.php` is required: `composer require --dev mtdowling/jmespath.php`. ```php @@ -457,7 +456,7 @@ $json = $browser ; ``` -> **Note** +> [!NOTE] > See the [full `zenstruck/assert` expectation API documentation](https://github.com/zenstruck/assert#expectation-api) > to see all the methods available on `Zenstruck\Browser\Json`. @@ -543,6 +542,126 @@ class MyTest extends PantherTestCase } ``` +### Advanced DOM Selectors + +Any browser method that accepts a `$selector` argument can be one of the following types: + +1. `string` - [_auto selector_](#auto-string-selector) +2. `Zenstruck\Dom\Selector` - [_selector object_](#selector-object) +3. `callable` - [_callable selector_](#callable-selector) + +> [!NOTE] +> Most [`PantherBrowser`](#pantherbrowser) specific methods that accept a `$selector` +> argument only accept a string that's specific to the Panther client. + +#### Auto (`string`) Selector + +A `string` is considered an auto selector and tries to find the DOM node using the following +strategies in order (first strategy to find a node wins): + +1. **CSS**: Try to find _any node_ using `$selector` as CSS selector string. +2. **Button**: Try to find ` + + diff --git a/tests/KernelBrowserTests.php b/tests/KernelBrowserTests.php index 7a6bd4f..1a1bbef 100644 --- a/tests/KernelBrowserTests.php +++ b/tests/KernelBrowserTests.php @@ -19,6 +19,7 @@ use Zenstruck\Browser\Json; use Zenstruck\Browser\KernelBrowser; use Zenstruck\Browser\Tests\Fixture\CustomHttpOptions; +use Zenstruck\Dom; /** * @author Kevin Bond @@ -27,6 +28,26 @@ trait KernelBrowserTests { use BrowserTests; + /** + * @test + */ + public function click_with_complex_callback_filter(): void + { + $this->browser() + ->visit('/page1') + ->click(function(Dom $dom) { + return $dom + ->find('input7') + ?->ensure(Dom\Node\Form\Field::class) + ->form() + ?->siblings('#link') + ->first('a link') + ; + }) + ->assertOn('/page2') + ; + } + /** * @test */ @@ -322,10 +343,10 @@ public function can_set_default_http_options(): void $this->browser() ->setDefaultHttpOptions(['headers' => ['x-foo' => 'bar']]) ->post('/http-method') - ->assertContains('"x-foo":["Bar"]') + ->assertContains('"x-foo":["bar"]') ->post('/http-method', ['headers' => ['x-bar' => 'foo']]) - ->assertContains('"x-bar":["Foo"]') - ->assertContains('"x-foo":["Bar"]') + ->assertContains('"x-bar":["foo"]') + ->assertContains('"x-foo":["bar"]') ; }