Skip to content

Commit

Permalink
Merge pull request #8 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
From main - added URL column to table
  • Loading branch information
Andrey Helldar authored Nov 10, 2021
2 parents eacda0d + 1aa1dac commit 76002b8
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 9 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: phpunit
on: [ push ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [ "7.3", "7.4", "8.0" ]
laravel: [ "6.0" ,"7.0", "8.0" ]

name: php ${{ matrix.php }}, laravel ${{ matrix.laravel }}

services:
mysql:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: default
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd, redis, pdo_mysql, pdo_pgsql
tools: composer:v2
coverage: none

- name: Install dependencies
run: composer require laravel/framework:^${{ matrix.laravel }}

- name: Execute tests
run: sudo vendor/bin/phpunit
env:
MYSQL_HOST: 127.0.0.1
19 changes: 14 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,30 @@
"illuminate/support": "^6.0|^7.0|^8.0",
"nesbot/carbon": "^1.20|^2.0"
},
"require-dev": {
"ext-json": "*",
"ext-pdo_mysql": "*",
"mockery/mockery": "^1.3.1",
"orchestra/testbench": "^4.0|^5.0|^6.0",
"phpunit/phpunit": "^8.0|^9.0"
},
"autoload": {
"psr-4": {
"Helldar\\LastModified\\": "src/"
"Helldar\\LastModified\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"prefer-stable": true,
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"laravel": {
"providers": [
"Helldar\\LastModified\\ServiceProvider"
Expand Down
39 changes: 39 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/logs/coverage"/>
<text outputFile="build/logs/coverage.txt"/>
</report>
</coverage>
<php>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="APP_URL" value="http://localhost"/>
<env name="DB_USERNAME" value="root"/>
<env name="DB_PASSWORD" value="root"/>
<env name="DB_DATABASE" value="default"/>
</php>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
11 changes: 7 additions & 4 deletions src/Services/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ public function isNotModified(): bool

public function getDate(): DateTimeInterface
{
$item = $this->get();
$date = $item->updated_at ?? date('Y-m-d H:i:s');
if ($item = $this->get()) {
$date = $item->updated_at ?? date('Y-m-d H:i:s');

return Carbon::parse($date);
return Carbon::parse($date);
}

return Carbon::now();
}

public function get(): Model
public function get(): ?Model
{
return Model::find($this->key);
}
Expand Down
56 changes: 56 additions & 0 deletions tests/MainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Tests;

use Carbon\Carbon;
use Helldar\LastModified\Services\LastItem;
use Helldar\LastModified\Services\LastModified;

class MainTest extends TestCase
{
protected $table = 'last_modified';

public function testRoute()
{
$this->assertDatabaseCount($this->table, 0);

$this->get('foo')->assertStatus(200);
$this->get('foo')->assertStatus(200);

$this->assertDatabaseCount($this->table, 0);
}

public function testLastModified()
{
$this->assertDatabaseCount($this->table, 0);

$this->get('foo')->assertStatus(200);
$this->get('bar')->assertStatus(200);
$this->get('baz')->assertStatus(200);

$date = Carbon::now();

(new LastModified())
->manuals(
(new LastItem('http://localhost/foo', $date)),
(new LastItem('http://localhost/bar', $date))
)
->update();

$this->assertDatabaseCount($this->table, 2);

$this->withHeaders(['if-modified-since' => $date->format('r')])->get('foo')->assertStatus(304);
$this->withHeaders(['if-modified-since' => $date->format('r')])->get('bar')->assertStatus(304);
$this->withHeaders(['if-modified-since' => $date->format('r')])->get('baz')->assertStatus(200);

$this->get('foo')->assertStatus(304);
$this->get('foo')->assertStatus(304);
$this->get('foo')->assertStatus(304);

$this->get('bar')->assertStatus(304);
$this->get('bar')->assertStatus(304);
$this->get('bar')->assertStatus(304);
}
}
34 changes: 34 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Tests;

use Helldar\LastModified\Middlewares\CheckLastModified;
use Helldar\LastModified\ServiceProvider;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
use RefreshDatabase;

protected function getPackageProviders($app): array
{
return [ServiceProvider::class];
}

protected function getEnvironmentSetUp($app)
{
$this->setRoutes($app);
}

protected function setRoutes($app)
{
$app['router']
->middleware(CheckLastModified::class)
->get('{slug}', function (string $slug) {
return response()->json($slug);
});
}
}

0 comments on commit 76002b8

Please sign in to comment.