From 1591fd58b7413e9ed52cb683bbfa4a8f4e9b45e9 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 11 Nov 2021 00:54:37 +0300 Subject: [PATCH 1/5] Added very bad tests, but working --- composer.json | 19 ++++++++++---- phpunit.xml | 40 ++++++++++++++++++++++++++++++ src/Services/Check.php | 11 ++++++--- tests/MainTest.php | 56 ++++++++++++++++++++++++++++++++++++++++++ tests/TestCase.php | 34 +++++++++++++++++++++++++ 5 files changed, 151 insertions(+), 9 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/MainTest.php create mode 100644 tests/TestCase.php diff --git a/composer.json b/composer.json index e65ab46..8b17dce 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..770c4de --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,40 @@ + + + + + ./src + + + + + + + + + + + + + + + + + + ./tests + + + diff --git a/src/Services/Check.php b/src/Services/Check.php index 072c7b0..573f243 100644 --- a/src/Services/Check.php +++ b/src/Services/Check.php @@ -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); } diff --git a/tests/MainTest.php b/tests/MainTest.php new file mode 100644 index 0000000..a64a1e3 --- /dev/null +++ b/tests/MainTest.php @@ -0,0 +1,56 @@ +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); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..b35a8df --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,34 @@ +setRoutes($app); + } + + protected function setRoutes($app) + { + $app['router'] + ->middleware(CheckLastModified::class) + ->get('{slug}', function (string $slug) { + return response()->json($slug); + }); + } +} From cc12791b77984205491fa29dc933380d42ad9c58 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 11 Nov 2021 00:56:26 +0300 Subject: [PATCH 2/5] Added GitHub Actions rules --- .github/workflows/phpunit.yml | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 .github/workflows/phpunit.yml diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml new file mode 100644 index 0000000..b49a031 --- /dev/null +++ b/.github/workflows/phpunit.yml @@ -0,0 +1,44 @@ +name: phpunit +on: [ push ] + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + fail-fast: true + matrix: + php: [ "7.1", "7.2", "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 From 02d528e01921092adb66f36406e89a701f3bec14 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 11 Nov 2021 00:58:17 +0300 Subject: [PATCH 3/5] Update GitHub Actions --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index b49a031..984a4d8 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: true matrix: - php: [ "7.1", "7.2", "7.3", "7.4", "8.0" ] + php: [ "7.2", "7.3", "7.4", "8.0" ] laravel: [ "6.0" ,"7.0", "8.0" ] name: php ${{ matrix.php }}, laravel ${{ matrix.laravel }} From ab5d181d054dfdf5a4ec2dc53e07b39436ffc402 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 11 Nov 2021 00:59:23 +0300 Subject: [PATCH 4/5] Update GitHub Actions --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 984a4d8..da550c9 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: true matrix: - php: [ "7.2", "7.3", "7.4", "8.0" ] + php: [ "7.3", "7.4", "8.0" ] laravel: [ "6.0" ,"7.0", "8.0" ] name: php ${{ matrix.php }}, laravel ${{ matrix.laravel }} From 1aa1dac676982bc5467374f522bbcba9dc420223 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Thu, 11 Nov 2021 01:01:44 +0300 Subject: [PATCH 5/5] Update phpunit.xml --- phpunit.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 770c4de..c86dc27 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,7 +27,6 @@ -