Skip to content

Commit

Permalink
Merge 2ff64fa into 6f76217
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Oct 16, 2022
2 parents 6f76217 + 2ff64fa commit 725395b
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 78 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/phpunit.yml
@@ -0,0 +1,40 @@
name: PHPUnit
on:
push:
branches:
- master
tags:
- "*.*.*"
pull_request:
branches:
- master
schedule:
- cron: "0 8 * * 1"

jobs:
Build:
runs-on: 'ubuntu-latest'
container: 'byjg/php:${{ matrix.php-version }}-cli'
strategy:
matrix:
php-version:
- "8.1"
- "8.0"
- "7.4"
- "7.3"
- "7.2"

steps:
- uses: actions/checkout@v3
- run: composer install
- run: ./vendor/bin/phpunit

Documentation:
runs-on: 'ubuntu-latest'
needs: Build
if: github.ref == 'refs/heads/master'
env:
DOC_GITHUB_TOKEN: '${{ secrets.DOC_TOKEN }}'
steps:
- uses: actions/checkout@v3
- run: curl https://opensource.byjg.com/add-doc.sh | bash /dev/stdin php micro-orm
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -4,4 +4,5 @@ composer.lock
vendor

.idea/*
!.idea/runConfigurations
!.idea/runConfigurations
.phpunit.result.cache
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

34 changes: 16 additions & 18 deletions README.md
@@ -1,9 +1,10 @@
# MicroOrm for PHP

[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg.com-brightgreen.svg)](http://opensource.byjg.com)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/byjg/micro-orm/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byjg/micro-orm/?branch=master)
[![Build Status](https://travis-ci.org/byjg/micro-orm.svg?branch=master)](https://travis-ci.org/byjg/micro-orm)
[![Code Coverage](https://scrutinizer-ci.com/g/byjg/micro-orm/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/byjg/micro-orm/?branch=master)
[![Build Status](https://github.com/byjg/micro-orm/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/micro-orm/actions/workflows/phpunit.yml)
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/micro-orm/)
[![GitHub license](https://img.shields.io/github/license/byjg/micro-orm.svg)](https://opensource.byjg.com/opensource/licensing.html)
[![GitHub release](https://img.shields.io/github/release/byjg/micro-orm.svg)](https://github.com/byjg/micro-orm/releases/)


A micro framework for create a very simple decoupled ORM.
Expand All @@ -16,7 +17,7 @@ This library intended to be very small and very simple to use;
* A class Mapper is used for mapping the Entity and the repository
* Small and simple to use

# Examples
## Examples

For the examples below we will use the class 'Users';

Expand Down Expand Up @@ -70,7 +71,7 @@ $users->name = "New name";
$repository->save($users);
```

# Advanced uses
## Advanced uses

Get a collection using the query object:

Expand Down Expand Up @@ -108,7 +109,7 @@ $collection = $orderRepository->getByQuery(
);
```

# Using FieldAlias
## Using FieldAlias

Field alias is an alternate name for a field. This is usefull for disambiguation on join and leftjoin queries.
Imagine in the example above if both tables ITEM and ORDER have the same field called 'ID'.
Expand Down Expand Up @@ -159,7 +160,7 @@ $query = \ByJG\MicroOrm\Query::getInstance()
```


# Tables without auto increments fields
## Tables without auto increments fields

```php
<?php
Expand All @@ -174,7 +175,7 @@ $mapper = new \ByJG\MicroOrm\Mapper(
);
```

# Applying functions for Select and Update
## Applying functions for Select and Update

```php
<?php
Expand All @@ -198,7 +199,7 @@ $mapper->addFieldMap(
);
```

# Pre-defined closures for field map:
## Pre-defined closures for field map:

*Mapper::defaultClosure($value, $instance)*

Expand All @@ -210,7 +211,7 @@ If set in the update field map will make the field not updatable by the micro-or
It is usefull for fields that are pre-defined like 'Primary Key'; timestamp fields based on the
update and the creation; and others

# Before insert and update functions
## Before insert and update functions

You can also set closure to be applied before insert or update a record.
In this case will set in the Repository:
Expand All @@ -226,7 +227,7 @@ Repository::setBeforeUpdate(function ($instance) {
});
```

# Reuse Connection
## Reuse Connection

The Repository receives a DbDriverInterface instance (connection).
It is normal we create everytime a new connection.
Expand All @@ -247,7 +248,7 @@ $repo2 = new Repository($connectionManager->addConnection("uri://host"));

```

# Transaction
## Transaction

If all of DbDriver instance as created by the ConnectionManager you can create
database transactions including across different databases:
Expand All @@ -268,7 +269,7 @@ $connection->commitTransaction();
```


# Install
## Install

Just type:

Expand All @@ -282,16 +283,13 @@ composer require "byjg/micro-orm=4.0.*"
phpunit
```

# Related Projects
## Related Projects

- [Database Migration](https://github.com/byjg/migration)
- [Anydataset](https://github.com/byjg/anydataset)
- [PHP Rest Template](https://github.com/byjg/php-rest-template)
- [USDocker](https://github.com/usdocker/usdocker)
- [Serializer](https://github.com/byjg/serializer)




----
[Open source ByJG](http://opensource.byjg.com)
5 changes: 2 additions & 3 deletions composer.json
Expand Up @@ -11,11 +11,10 @@
"require": {
"php": ">=5.6.0",
"ext-json": "*",
"byjg/anydataset-db": "4.1.*",
"byjg/serializer": "1.0.*"
"byjg/anydataset-db": "4.1.3.x-dev"
},
"require-dev": {
"phpunit/phpunit": "5.7.*|7.4.*"
"phpunit/phpunit": "5.7.*|7.4.*|^9.5"
},
"suggest": {
"ext-curl": "*",
Expand Down
10 changes: 9 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -7,12 +7,20 @@ and open the template in the editor.

<!-- see http://www.phpunit.de/wiki/Documentation -->
<phpunit bootstrap="./vendor/autoload.php"
colors="false"
colors="true"
testdox="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="true"
stopOnFailure="false">

<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="error_reporting" value="E_ALL" />
</php>

<filter>
<whitelist>
<directory>./src</directory>
Expand Down
3 changes: 2 additions & 1 deletion src/Query.php
Expand Up @@ -5,6 +5,7 @@
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\MicroOrm\Exception\InvalidArgumentException;
use ByJG\Serializer\BinderObject;
use ByJG\Serializer\SerializerObject;

class Query
{
Expand Down Expand Up @@ -56,7 +57,7 @@ private function addFieldFromMapper(Mapper $mapper)
{
$entityClass = $mapper->getEntity();
$entity = new $entityClass();
$serialized = BinderObject::toArrayFrom($entity);
$serialized = SerializerObject::instance($entity)->serialize();

foreach (array_keys($serialized) as $fieldName) {
$mapField = $mapper->getFieldMap($fieldName, Mapper::FIELDMAP_FIELD);
Expand Down
11 changes: 7 additions & 4 deletions src/Repository.php
Expand Up @@ -5,6 +5,7 @@
use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException;
use ByJG\Serializer\BinderObject;
use ByJG\Serializer\SerializerObject;

class Repository
{
Expand Down Expand Up @@ -193,7 +194,7 @@ public function getByQuery(Query $query, array $mapper = [])
unset($fieldalias);
}
}
BinderObject::bindObject($data, $instance);
BinderObject::bind($data, $instance);

foreach ((array)$item->getFieldMap() as $property => $fieldmap) {
$selectMask = $fieldmap[Mapper::FIELDMAP_SELECTMASK];
Expand All @@ -204,7 +205,7 @@ public function getByQuery(Query $query, array $mapper = [])
$data[$property] = $selectMask($value, $instance);
}
if (count($item->getFieldMap()) > 0) {
BinderObject::bindObject($data, $instance);
BinderObject::bind($data, $instance);
}
$collection[] = $instance;
}
Expand Down Expand Up @@ -240,7 +241,9 @@ public function getByQueryRaw(Query $query)
public function save($instance)
{
// Get all fields
$array = BinderObject::toArrayFrom($instance, true);
$array = SerializerObject::instance($instance)
->withStopAtFirstLevel()
->serialize();
$array = $this->getMapper()->prepareField($array);

// Mapping the data
Expand Down Expand Up @@ -292,7 +295,7 @@ public function save($instance)
$this->update($updatable, $array);
}

BinderObject::bindObject($array, $instance);
BinderObject::bind($array, $instance);

return $instance;
}
Expand Down
22 changes: 8 additions & 14 deletions tests/ConnectionManagerTest.php
Expand Up @@ -3,6 +3,7 @@
namespace Test;

use ByJG\MicroOrm\ConnectionManager;
use ByJG\MicroOrm\Exception\TransactionException;
use PHPUnit\Framework\TestCase;

class ConnectionManagerTest extends TestCase
Expand All @@ -12,12 +13,12 @@ class ConnectionManagerTest extends TestCase
*/
protected $object;

public function setUp()
public function setUp(): void
{
$this->object = new ConnectionManager();
}

public function tearDown()
public function tearDown(): void
{
$this->object->destroy();
$this->object = null;
Expand Down Expand Up @@ -60,12 +61,10 @@ public function testBeginTransaction()
$this->assertEquals(2, $this->object->count());
}

/**
* @throws \ByJG\MicroOrm\Exception\TransactionException
* @expectedException \ByJG\MicroOrm\Exception\TransactionException
*/
public function testBeginTransactionError()
{
$this->expectException(TransactionException::class);

$this->object->addConnection("sqlite:///tmp/a.db");
$this->object->addConnection("sqlite:///tmp/d.db");

Expand All @@ -75,23 +74,18 @@ public function testBeginTransactionError()
$this->object->beginTransaction();
}

/**
* @throws \ByJG\MicroOrm\Exception\TransactionException
* @expectedException \ByJG\MicroOrm\Exception\TransactionException
*/
public function testRollbackTransactionError()
{
$this->expectException(TransactionException::class);

$this->object->addConnection("sqlite:///tmp/c.db");
$this->assertEquals(1, $this->object->count());
$this->object->rollbackTransaction();
}

/**
* @throws \ByJG\MicroOrm\Exception\TransactionException
* @expectedException \ByJG\MicroOrm\Exception\TransactionException
*/
public function testCommitTransactionError()
{
$this->expectException(TransactionException::class);
$this->object->addConnection("sqlite:///tmp/d.db");
$this->assertEquals(1, $this->object->count());
$this->object->commitTransaction();
Expand Down

0 comments on commit 725395b

Please sign in to comment.