Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Nov 26, 2020
0 parents commit a699cc5
Show file tree
Hide file tree
Showing 12 changed files with 873 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.github export-ignore
/tests export-ignore
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/composer.lock
/phpunit.xml
/vendor/
/build/
/.php_cs.cache
/.phpunit.result.cache
47 changes: 47 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in([__DIR__.'/src', __DIR__.'/tests'])
->notName('*.tpl.php')
;

return PhpCsFixer\Config::create()
->setRules(array(
'@Symfony' => true,
'@Symfony:risky' => true,
'@DoctrineAnnotation' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHPUnit75Migration:risky' => true,
'multiline_whitespace_before_semicolons' => ['strategy' => 'new_line_for_chained_calls'],
'multiline_comment_opening_closing' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => [
'imports_order' => ['const', 'class', 'function'],
],
'ordered_class_elements' => true,
'native_function_invocation' => true,
'explicit_indirect_variable' => true,
'explicit_string_variable' => true,
'escape_implicit_backslashes' => true,
'mb_str_functions' => true,
'logical_operators' => true,
'php_unit_method_casing' => ['case' => 'snake_case'],
'php_unit_test_annotation' => ['style' => 'annotation'],
'no_unreachable_default_argument_value' => true,
'declare_strict_types' => false,
'void_return' => false,
'single_trait_insert_per_statement' => false,
'simple_to_complex_string_variable' => true,
'no_superfluous_phpdoc_tags' => [
'allow_mixed' => true,
'allow_unused_params' => true,
'remove_inheritdoc' => true,
],
'phpdoc_to_comment' => false,
'function_declaration' => ['closure_function_spacing' => 'none'],
'nullable_type_declaration_for_default_null_value' => true,
))
->setRiskyAllowed(true)
->setFinder($finder)
;
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Kevin Bond

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "zenstruck/browser",
"description": "A fluent interface for your Symfony functional tests.",
"homepage": "https://github.com/zenstruck/browser",
"type": "library",
"license": "MIT",
"keywords": ["test", "symfony"],
"authors": [
{
"name": "Kevin Bond",
"email": "[email protected]"
}
],
"require": {
"php": ">=7.4",
"behat/mink": "^1.8",
"behat/mink-browserkit-driver": "^1.3",
"symfony/framework-bundle": "^4.4|^5.0"
},
"require-dev": {
"symfony/mailer": "^4.4|^5.0",
"symfony/phpunit-bridge": "^5.1"
},
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"autoload": {
"psr-4": { "Zenstruck\\": "src/" }
}
}
82 changes: 82 additions & 0 deletions src/Browser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Zenstruck;

use Behat\Mink\Driver\BrowserKitDriver;
use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Mink;
use Behat\Mink\Session;
use Behat\Mink\WebAssert;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Zenstruck\Browser\Actions;
use Zenstruck\Browser\Assertions;

/**
* @author Kevin Bond <[email protected]>
*/
class Browser
{
use Actions, Assertions;

private const SESSION = 'app';

private AbstractBrowser $browser;
private Mink $mink;

final public function __construct(AbstractBrowser $browser)
{
$this->browser = $browser;
$this->mink = new Mink([self::SESSION => new Session(new BrowserKitDriver($this->browser))]);
}

final public function browser(): AbstractBrowser
{
return $this->browser;
}

final public function minkSession(): Session
{
return $this->mink->getSession(self::SESSION);
}

final public function webAssert(): WebAssert
{
return $this->mink->assertSession(self::SESSION);
}

final public function documentElement(): DocumentElement
{
return $this->minkSession()->getPage();
}

final public function interceptRedirects(): self
{
$this->browser()->followRedirects(false);

return $this;
}

final public function with(callable $callback): self
{
$callback($this);

return $this;
}

final public function dump(?string $selector = null): self
{
$context = 'URL: '.$this->minkSession()->getCurrentUrl().', STATUS: '.$this->minkSession()->getStatusCode();

dump($context);
dump($selector ? $this->documentElement()->find('css', $selector)->getText() : $this->documentElement()->getText());
dump($context);

return $this;
}

final public function dd(?string $selector = null): void
{
$this->dump($selector);
exit(1);
}
}
102 changes: 102 additions & 0 deletions src/Browser/Actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Zenstruck\Browser;

use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Session;
use Symfony\Component\BrowserKit\AbstractBrowser;

/**
* @author Kevin Bond <[email protected]>
*
* @method AbstractBrowser browser()
* @method Session minkSession()
* @method DocumentElement documentElement()
*/
trait Actions
{
final public function visit(string $uri): self
{
$this->minkSession()->visit($uri);

return $this;
}

final public function get(string $url, array $parameters = [], array $files = [], array $server = []): self
{
$this->browser()->request('GET', $url, $parameters, $files, $server);

return $this;
}

final public function post(string $url, array $parameters = [], array $files = [], array $server = []): self
{
$this->browser()->request('POST', $url, $parameters, $files, $server);

return $this;
}

final public function delete(string $url, array $parameters = []): self
{
$this->browser()->request('DELETE', $url, $parameters);

return $this;
}

final public function follow(string $link): self
{
$this->documentElement()->clickLink($link);

return $this;
}

final public function fillField(string $selector, string $value): self
{
$this->documentElement()->fillField($selector, $value);

return $this;
}

public function checkField(string $selector): self
{
$this->documentElement()->checkField($selector);

return $this;
}

public function uncheckField(string $selector): self
{
$this->documentElement()->uncheckField($selector);

return $this;
}

public function selectFieldOption(string $selector, string $value): self
{
$this->documentElement()->selectFieldOption($selector, $value);

return $this;
}

final public function attachFile(string $selector, string $path): self
{
$this->documentElement()->attachFileToField($selector, $path);

return $this;
}

final public function press(string $selector): self
{
$this->documentElement()->pressButton($selector);

return $this;
}

final public function followRedirect(): self
{
$this->browser()->followRedirect();
$this->browser()->followRedirects(true);

return $this;
}
}
Loading

0 comments on commit a699cc5

Please sign in to comment.