Skip to content

Commit

Permalink
Merge pull request #5 from challgren/cake-3.8
Browse files Browse the repository at this point in the history
CakePHP 3.8 Support
  • Loading branch information
ADmad committed Jan 16, 2020
2 parents 91c862a + 9bad559 commit 185d36e
Show file tree
Hide file tree
Showing 19 changed files with 374 additions and 115 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,3 +1,6 @@
/composer.lock
/plugins
/vendor
/tmp
/phpcs.xml
/phpunit.phar
63 changes: 42 additions & 21 deletions .travis.yml
@@ -1,49 +1,70 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7
- 7.4

sudo: false

env:
matrix:
- DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
- DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test'
- DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'
- DB=sqlite db_dsn='sqlite:///:memory:'

global:
- DEFAULT=1

services:
- postgresql
- mysql

matrix:
fast_finish: true

include:
- php: 5.4
env: PHPCS=1 DEFAULT=0
- php: 7.1
env: DB=pgsql db_dsn='postgres://postgres@127.0.0.1/cakephp_test'

- php: 5.4
env: COVERALLS=1 DEFAULT=0 DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
- php: 7.2
env: DB=sqlite db_dsn='sqlite:///:memory:'

before_script:
- composer self-update
- composer install --prefer-dist --no-interaction
- php: 5.6
env: PREFER_LOWEST=1

- sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi"
- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi"
- php: 7.3
env: CODECOVERAGE=1 DEFAULT=0 DB=mysql db_dsn='mysql://root@127.0.0.1/cakephp_test'

- sh -c "if [ '$PHPCS' = '1' ]; then composer require cakephp/cakephp-codesniffer:dev-master; fi"
- php: 7.3
env: CHECKS=1 DEFAULT=0

before_install:
- phpenv config-rm xdebug.ini

before_script:
- if [[ $CHECKS != 1 ]]; then composer require --dev phpunit/phpunit:"^5.7.14|^6.0"; fi

- sh -c "if [ '$COVERALLS' = '1' ]; then composer require --dev satooshi/php-coveralls:dev-master; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then mkdir -p build/logs; fi"
- if [[ $PREFER_LOWEST != 1 ]]; then composer install --prefer-source --no-interaction; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --prefer-lowest --prefer-stable --prefer-dist --no-interaction; fi
- if [[ $PREFER_LOWEST == 1 ]]; then composer require --dev dereuromark/composer-prefer-lowest:dev-master; fi

- if [[ $DB == 'mysql' ]]; then mysql -u root -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi

script:
- sh -c "if [ '$DEFAULT' = '1' ]; then phpunit --stderr; fi"
- sh -c "if [ '$PHPCS' = '1' ]; then ./vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then phpunit --stderr --coverage-clover build/logs/clover.xml; fi"
- sh -c "if [ '$COVERALLS' = '1' ]; then php vendor/bin/coveralls -c .coveralls.yml -v; fi"
- if [[ $DEFAULT == 1 ]]; then vendor/bin/phpunit; fi
- if [[ $PREFER_LOWEST == 1 ]]; then vendor/bin/validate-prefer-lowest; fi

- if [[ $CHECKS == 1 ]]; then composer phpstan-setup && composer phpstan; fi
- if [[ $CHECKS == 1 ]]; then composer cs-check; fi

- if [[ $CODECOVERAGE == 1 ]]; then phpdbg -qrr vendor/bin/phpunit --stderr --coverage-clover build/logs/clover.xml; fi

after_success:
- if [[ $CODECOVERAGE == 1 ]]; then php vendor/bin/php-coveralls -c .coveralls.yml -v; fi

cache:
directories:
- $HOME/.composer/cache

notifications:
email: false
26 changes: 10 additions & 16 deletions README.md
Expand Up @@ -7,8 +7,8 @@

Single Table Inheritance for CakePHP 3 ORM.

> [...] a way to emulate object-oriented inheritance in a relational database. When mapping from a database
> table to an object in an object-oriented language, a field in the database identifies what class in the
> [...] a way to emulate object-oriented inheritance in a relational database. When mapping from a database
> table to an object in an object-oriented language, a field in the database identifies what class in the
> hierarchy the object belongs to.
(source: [Wikipedia][1])
Expand All @@ -27,12 +27,6 @@ You then need to load the plugin. You can use the shell command:
bin/cake plugin load Muffin/Sti
```

or by manually adding statement shown below to `bootstrap.php`:

```php
Plugin::load('Muffin/Sti');
```

## Usage

```php
Expand All @@ -56,7 +50,7 @@ class CooksTable extends Table {

// Optionally, set the default type. If none is defined, the
// first one (i.e. `chef`) will be used.
$this->entityClass('App\Model\Entity\AssistantChef');
$this->setEntityClass('App\Model\Entity\AssistantChef');
}
}
```
Expand Down Expand Up @@ -88,7 +82,7 @@ Optionally, you can create classes for your tables that extend the parent table
<?php // src/Model/Table/ChefsTable.php
namespace App\Model\Table;

class ChefsTable extends CooksTable
class ChefsTable extends CooksTable
{
// ...
}
Expand All @@ -107,25 +101,25 @@ public function validationChefs(Validator $validator)
```

### New entities
The behavior will automatically add helper methods for creating entities of different types

The behavior will automatically add helper methods for creating entities of different types
(i.e. `newChef()`). There are different ways of creating new entities, all are valid and depending
on the cases, you might need one or the other:

```php
// using the parent table
$cooks->newChef([...]);

// or, using the parent table again
$cooks->newEntity(['type' => 'chef', ...]);

// or, using the child table
$chefs->newEntity([...]);
```

### Note

For the above examples to work using (*chef), you need to add a custom rule to the `Inflector`:
For the above examples to work using (*chef), you need to add a custom rule to the `Inflector`:

```php
Cake\Utility\Inflector::rules('plural', ['/chef$/i' => '\1Chefs']);
Expand Down
15 changes: 12 additions & 3 deletions composer.json
Expand Up @@ -30,11 +30,12 @@
"source": "https://github.com/usemuffin/sti"
},
"require": {
"php": ">=5.6",
"cakephp/cakephp": "^3.8"
},
"require-dev": {
"cakephp/cakephp": "~3.0",
"cakephp/cakephp-codesniffer": "2.*",
"phpunit/phpunit": "4.1.*"
"cakephp/cakephp-codesniffer": "^3.0",
"php-coveralls/php-coveralls": "^2.2"
},
"autoload": {
"psr-4": {
Expand All @@ -47,6 +48,14 @@
"Muffin\\Sti\\TestApp\\": "tests/test_app"
}
},
"scripts": {
"phpstan": "phpstan analyse -c tests/phpstan.neon -l 5 src/",
"phpstan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan-shim:^0.11 && mv composer.backup composer.json",
"test": "php phpunit.phar",
"test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit-6.5.13.phar && mv phpunit-6.5.13.phar phpunit.phar || true",
"cs-check": "phpcs -p --ignore=/config/Migrations/ --extensions=php src/ tests/",
"cs-fix": "phpcbf -v --ignore=/config/Migrations/ --extensions=php src/ tests/"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
Expand Down
12 changes: 12 additions & 0 deletions phpcs.xml.dist
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<ruleset name="CakePHP Core">
<rule ref="./vendor/cakephp/cakephp-codesniffer/CakePHP/ruleset.xml">
<!-- Exclude unwanted sniffs -->
<exclude name="Generic.Commenting.Todo.TaskFound"/> <!-- Excluded during 3.next development -->
</rule>

<!-- Necessary for class aliases used for backwards compat -->
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<severity>0</severity>
</rule>
</ruleset>
31 changes: 31 additions & 0 deletions phpunit.xml
@@ -0,0 +1,31 @@
<phpunit
bootstrap="tests/bootstrap.php"
colors="true"
>
<php>
<!-- E_ALL => 32767 -->
<!-- E_ALL & ~E_USER_DEPRECATED => 16383 -->
<ini name="error_reporting" value="16383"/>
</php>
<testsuites>
<testsuite name="sti">
<directory>tests/</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="Cake\TestSuite\Fixture\FixtureInjector">
<arguments>
<object class="Cake\TestSuite\Fixture\FixtureManager"/>
</arguments>
</listener>
</listeners>

<!-- Prevent coverage reports from looking in tests, vendors, config folders -->
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>

</phpunit>

0 comments on commit 185d36e

Please sign in to comment.