Skip to content

Commit

Permalink
Merge branch 'main' into #56-add-notifications-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubKermes authored Jun 27, 2024
2 parents 1973b2d + b71387e commit f5f54f8
Show file tree
Hide file tree
Showing 13 changed files with 588 additions and 207 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
"php": "^8.3",
"behat/behat": "^3.14",
"illuminate/notifications": "^10.0|^11.0",
"illuminate/console": "^10.0|^11.0",
"phpunit/phpunit": "^10.1|^11.0.1",
"symfony/css-selector": "^6.2|^7.1",
"symfony/dom-crawler": "^6.2|^7.1"
"symfony/dom-crawler": "^6.2|^7.1",
"illuminate/support": "^10.0|11.0"
},
"require-dev": {
"blumilksoftware/codestyle": "^3.1.0",
"illuminate/contracts": "^10.0|11.0",
"illuminate/http": "^10.0|11.0",
"illuminate/support": "^10.0|11.0",
"symfony/http-foundation": "^6.2|^7.1"
},
"autoload": {
Expand Down
27 changes: 22 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ BLT for PHP developers: Behat+Laravel toolbox
> Package is still under development.
### Usage
Use Composer to get package from the Packagist repository:
To start using package if you don't already have Behat initialized in your project:
```
composer require blumilksfotware/blt --dev
php artisan blt:init
```

To use the package you need to have behat installed in your project:
### Or if you want to do that manually:
Use Composer to get package from the Packagist repository:
```
php composer.phar require --dev behat/behat
composer require blumilksfotware/blt --dev
```
Create .env.behat file in your project root directory and set up your environment variables for Behat.
```
Expand All @@ -24,7 +25,7 @@ Initialize Behat in your project:
php vendor/bin/behat --init
```
To learn more about Behat visit [Behat documentation](https://docs.behat.org/en/latest/).

### To use in your tests:
Bootstrap BLT in your FeatureContext file:
```
public function __construct()
Expand All @@ -33,6 +34,22 @@ Bootstrap BLT in your FeatureContext file:
$bootstrapper->boot();
}
```
If you want to include all suite of features, you can use:
```
use Blumilk\BLT\Features\Toolbox;
class FeatureContext extends Toolbox implements Context
{...}
```
Or to use selected traits:
```
use Blumilk\BLT\Features\Traits\Eloquent;
class FeatureContext implements Context
{
use Eloquent;
...
}
```

Example usage in tests is available in the [docs](docs).
### Development
There are scripts available for package codestyle checking and testing:
Expand Down
4 changes: 4 additions & 0 deletions src/BLTServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace Blumilk\BLT;

use Blumilk\BLT\Console\Commands\BLTInit;
use Illuminate\Support\ServiceProvider;

class BLTServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->commands([
BLTInit::class,
]);
$this->publishes([
__DIR__ . "/../config/blt.php" => config_path("blt.php"),
], "config");
Expand Down
49 changes: 49 additions & 0 deletions src/Console/Commands/BLTInit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Console\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

class BLTInit extends Command
{
protected $signature = "blt:init";
protected $description = "Initialize BLT in your project";

public function handle(): void
{
$this->info("Configuring BLT package...");

if (!is_dir("features")) {
$this->initializeBehat();
}

if (!file_exists(".env.behat")) {
$this->copyEnvFile();
}

$this->info("Behat configured successfully with BLT");
}

protected function initializeBehat(): void
{
$process = new Process(["vendor/bin/behat", "--init"]);
$process->run();

if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}

$this->info($process->getOutput());
}

protected function copyEnvFile(): void
{
if (!copy(".env.example", ".env.behat")) {
$this->error("Failed to copy .env.example to .env.behat");
}
}
}
13 changes: 0 additions & 13 deletions src/Features/Http.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Features/HttpRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features;

use Behat\Behat\Context\Context;
use Blumilk\BLT\Features\Traits\HttpRequest as HttpRequestTrait;

class HttpRequest implements Context
{
use HttpRequestTrait;
}
13 changes: 13 additions & 0 deletions src/Features/HttpResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Blumilk\BLT\Features;

use Behat\Behat\Context\Context;
use Blumilk\BLT\Features\Traits\HttpResponse as HttpResponseTrait;

class HttpResponse implements Context
{
use HttpResponseTrait;
}
107 changes: 2 additions & 105 deletions src/Features/Traits/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,111 +4,8 @@

namespace Blumilk\BLT\Features\Traits;

use Behat\Gherkin\Node\TableNode;
use PHPUnit\Framework\Assert;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

trait Http
{
use Application;

protected Request $request;
protected Response $response;

/**
* @Given a user is requesting :url
* @Given a user is requesting :url using :method method
*/
public function aUserIsRequesting(string $endpoint, string $method = Request::METHOD_GET): void
{
$this->request = Request::create($endpoint, $method);
}

/**
* @When a request is sent
*/
public function aRequestIsSent(): void
{
$this->response = $this->getContainer()->handle($this->request);
$this->response->send();
}

/**
* @Given request body contains :key equal :value
*/
public function requestBodyContainsKeyValuePair(string $key, string $value): void
{
$this->request[$key] = $value;
}

/**
* @Given request body contains:
*/
public function requestBodyContains(TableNode $table): void
{
foreach ($table as $row) {
$this->requestBodyContainsKeyValuePair($row["key"], $row["value"]);
}
}

/**
* @Given request headers contains :header equal :value
*/
public function requestHeadersContainsKeyValuePair(string $header, string $value): void
{
$this->request->headers->set($header, $value);
}

/**
* @Given request headers contains:
*/
public function requestHeadersContains(TableNode $table): void
{
foreach ($table as $row) {
$this->requestHeadersContainsKeyValuePair($row["header"], $row["value"]);
}
}

/**
* @Given request form params contains:
*/
public function thereAreValuesInFormParams(TableNode $table): void
{
foreach ($table as $row) {
$this->request->request->set($row["key"], $row["value"]);
}
}

/**
* @Then a response status code should be :status
*/
public function aResponseStatusCodeShouldBe(int $status): void
{
Assert::assertEquals($status, $this->response->getStatusCode());
}

/**
* @Given a response HTML should contain :phrase phrase
*/
public function aResponseHTMLShouldContainPhrase(string $phrase): void
{
Assert::assertStringContainsString($phrase, $this->response->getContent());
}

/**
* @Given a response HTML should contain CSRF token
*/
public function aResponseHTMLShouldContainCSRFToken(): void
{
$converter = new CssSelectorConverter();
$xPath = $converter->toXPath("form input[name=\"_token\"]");

$crawler = new Crawler($this->response->getContent());
$node = $crawler->filterXPath($xPath)->first();

Assert::assertNotNull($node?->attr("value"));
}
use HttpRequest;
use HttpResponse;
}
Loading

0 comments on commit f5f54f8

Please sign in to comment.