Skip to content

Commit

Permalink
Added more rector rules, mostly whole test syntax migrated and workin…
Browse files Browse the repository at this point in the history
…g seperatley
  • Loading branch information
Mārtiņš Ručevskis committed Dec 4, 2023
1 parent a051931 commit e885bd5
Show file tree
Hide file tree
Showing 24 changed files with 1,055 additions and 82 deletions.
35 changes: 35 additions & 0 deletions app/Console/Commands/ShiftCodeceptionToPhpUnit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Shift\Shifts\CodeceptionToLaravelTests;
use Illuminate\Console\Command;

class ShiftCodeceptionToPhpUnit extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'shift:CodeceptionToPhpUnit';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command for automatically shifting Codeception test to Laravel tests';

/**
* Execute the console command.
*/
public function handle(): void
{
echo "I'm starting to shift, SLAYYY!".PHP_EOL;
config(['shift.command_name' => 'shift:CodeceptionToPhpUnit']);
(new CodeceptionToLaravelTests)->run(config('shift.project_path'));
}
}
20 changes: 20 additions & 0 deletions app/Shift/LaravelShiftFiles/LaravelTests/CreatesApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests;

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Application;

trait CreatesApplication
{
/**
* Creates the application.
*/
public function createApplication(): Application
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();

return $app;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests\Support\HttpMock;

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;

class HttpMock
{
/**
* @var HttpMockResponse[]
*/
private array $responses;

public function __construct(private readonly string $url)
{
}

public function url(): string
{
return $this->url;
}

public function addResponse(HttpMockResponse $response): self
{
$this->responses[] = $response;

return $this;
}

public function buildMock(): void
{
Http::fake([
$this->url => function (Request $request) {
foreach ($this->responses as $response) {
if ($response->matchesRequest($request)) {
return $response->asHttpResponse();
}
}
},
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Tests\Support\HttpMock;

use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;

class HttpMockResponse
{
private null|array|string $body = null;

private int $statusCode = 200;

private ?MockResponseConditions $responseConditions = null;

private array $headers = [];

public function when(): MockResponseConditions
{
if ($this->responseConditions === null) {
$this->responseConditions = new MockResponseConditions($this);
}

return $this->responseConditions;
}

public function respondWithBody(null|array|string $body = ''): self
{
$this->body = $body;

return $this;
}

public function respondWithStatusCode(int $statusCode): self
{
$this->statusCode = $statusCode;

return $this;
}

public function withHeaders(array $headers): self
{
$this->headers = $headers;

return $this;
}

public function matchesRequest(Request $request): bool
{
return $this->responseConditions === null || $this->responseConditions->conditionsMatchRequest($request);
}

public function asHttpResponse(): PromiseInterface
{
return Http::response($this->body, $this->statusCode, $this->headers);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Tests\Support\HttpMock;

use Illuminate\Http\Client\Request;

class MockResponseConditions
{
private ?string $bodyContains = null;

private string $requestMethod = '';

public function __construct(private readonly HttpMockResponse $response)
{
}

public function get(): self
{
$this->requestMethod = 'GET';

return $this;
}

public function patch(): self
{
$this->requestMethod = 'PATCH';

return $this;
}

public function post(): self
{
$this->requestMethod = 'POST';

return $this;
}

public function delete(): self
{
$this->requestMethod = 'DELETE';

return $this;
}

public function bodyIsContaining(string $requestBody): self
{
$this->bodyContains = $requestBody;

return $this;
}

public function then(): HttpMockResponse
{
return $this->response;
}

public function conditionsMatchRequest(Request $request): bool
{
return ($this->requestMethod === '' || $request->method() === $this->requestMethod)
&& ($this->bodyContains === null || str_contains($request->body(), $this->bodyContains));
}
}
105 changes: 105 additions & 0 deletions app/Shift/LaravelShiftFiles/LaravelTests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Tests;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Tests\Support\HttpMock\HttpMock;
use Tests\Support\HttpMock\HttpMockResponse;

use function Safe\file_get_contents;

abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use RefreshDatabase;

protected string $seeder = '';

public function httpMock(string $url = '*'): HttpMock
{
return new HttpMock($url);
}

public function jsonFileContentAsArray(string $fileName): array
{
return json_decode(file_get_contents($fileName), true);
}

public function mockResponse(
string $directory,
string $mockFilename,
string $type = null
): string {
return $this->mockFileContent(
$directory,
$mockFilename,
'Response',
$type
);
}

public function mockRequest(
string $directory,
string $mockFilename,
string $type = null
): array {
return json_decode($this->mockFileContent(
$directory,
$mockFilename,
'Request',
$type
), true);
}

public function outgoingRequest(string $url): Request
{
return $this->outgoingRequests($url)->first()[0];
}

public function outgoingRequests(string $url, string $method = null, string $body = null, bool $bodyMatchExact = false): Collection
{
return Http::recorded(function (Request $request, Response $response) use ($url, $method, $body, $bodyMatchExact) {
return str_contains($request->toPsrRequest()->getUri()->getPath(), $url)
&& (! isset($method) || $request->toPsrRequest()->getMethod() === $method)
&& (! isset($body) || (
($bodyMatchExact && $request->toPsrRequest()->getBody()->getContents() === $body)
|| (! $bodyMatchExact && str_contains($request->toPsrRequest()->getBody()->getContents(), $body))
)
);
})->values();
}

public static function httpMockResponse(): HttpMockResponse
{
return new HttpMockResponse();
}

protected function mockActiveTokenResponse(): void
{
Http::fake(
[config('services.oauth.url').'/token_info' => Http::response(
['active' => true],
200,
['Content-Type', 'application/json; charset=utf-8']
)]
);
}

private function mockFileContent(
string $directory,
string $mockFilename,
string $type = null,
string $subType = null
): string {
return file_get_contents((new Collection(
[$directory, 'Mock', $type, $subType, $mockFilename]
))->filter()->map(function ($value, $key) {
return ($key === 0 ? 'rtrim' : 'trim')($value, '/');
})->join('/'));
}
}
29 changes: 29 additions & 0 deletions app/Shift/LaravelShiftFiles/LaravelTests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
colors="true"
bootstrap="tests/_bootstrap.php">
<testsuites>
<testsuite name="Feature">
<directory>./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory>./tests/Unit</directory>
</testsuite>
</testsuites>
<coverage/>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="10"/>
<env name="CACHE_DRIVER" value="file"/>
<env name="MAIL_MAILER" value="array"/>
<env name="SESSION_DRIVER" value="file"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
Loading

0 comments on commit e885bd5

Please sign in to comment.