Skip to content

Commit

Permalink
Increase test coverage too 100%
Browse files Browse the repository at this point in the history
- Add new tests for RepositoriesGenerateCommand, although some
  tests for the SelectEntries builder in there is still missing
- Some slight restructuring and correction of phpcs ruleset
  • Loading branch information
iquito committed Apr 29, 2019
1 parent 81bb0df commit 01fded3
Show file tree
Hide file tree
Showing 20 changed files with 745 additions and 283 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ before_script:
script:
- vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml
- vendor/bin/phpstan analyse src bin --level=7
- vendor/bin/phpcs --standard=ruleset.xml --extensions=php src bin tests
- vendor/bin/phpcs --standard=ruleset.xml --extensions=php --ignore=tests/TestEntities src bin tests
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT; fi

notifications:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Squirrel Entities Component

[![Build Status](https://img.shields.io/travis/com/squirrelphp/entities.svg)](https://travis-ci.com/squirrelphp/entities) [![Test Coverage](https://api.codeclimate.com/v1/badges/36a9f5a3b4abbaf7901c/test_coverage)](https://codeclimate.com/github/squirrelphp/entities/test_coverage) ![PHPStan](https://img.shields.io/badge/style-level%207-success.svg?style=flat-round&label=phpstan) [![Packagist Version](https://img.shields.io/packagist/v/squirrelphp/entities.svg?style=flat-round)](https://packagist.org/packages/squirrelphp/entities) [![PHP Version](https://img.shields.io/packagist/php-v/squirrelphp/entities.svg)](https://packagist.org/packages/squirrelphp/entities) [![Software License](https://img.shields.io/badge/license-MIT-success.svg?style=flat-round)](LICENSE)

Simple, safe and flexible implementation of handling SQL entities and repositories as well as multi-table SQL queries while staying lightweight and easy to understand and use.
Simple & safe implementation of handling SQL entities and repositories as well as multi-table SQL queries while staying lightweight and easy to understand and use. Offers rapid application development by generating repositories (which should not be added to VCS) for entities, and [squirrelphp/entities-bundle](https://github.com/squirrelphp/entities-bundle) offers automatic integration of these repositories into Symfony.

This package is still not 100% finished - some tests are missing (although the most important parts have test coverage) and the documentation will be a challenge. For now it is released so it can be used as-is and improved over time.
This package is still not 100% finished - mainly documentation is lacking and needs to be added, and some slight API changes are still possible, although unlikely at this point. Some refactoring would also be good.
5 changes: 4 additions & 1 deletion bin/squirrel_repositories_generate
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ $input = new ArgvInput(null, $inputDefinition);
$srcDirectories = $input->getOption('source-dir');

// Execute command to generate repositories
$cmd = new \Squirrel\Entities\Generate\RepositoriesGenerateCommand($srcDirectories);
$cmd = new \Squirrel\Entities\Generate\RepositoriesGenerateCommand(
$srcDirectories,
new \Squirrel\Entities\Generate\PHPFilesInDirectoryGetContents()
);
$log = $cmd();

// Add summary of processed entities
Expand Down
2 changes: 1 addition & 1 deletion captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"conditions": []
},
{
"action": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src bin tests",
"action": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache --ignore=tests/TestEntities src bin tests",
"options": [],
"conditions": []
}
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"scripts": {
"phpstan": "vendor/bin/phpstan analyse src bin --level=7",
"phpunit": "vendor/bin/phpunit --colors=always",
"phpcs": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache src bin tests",
"phpcs": "vendor/bin/phpcs --standard=ruleset.xml --extensions=php --cache --ignore=tests/TestEntities src bin tests",
"codecoverage": "vendor/bin/phpunit --coverage-html tests/_reports"
}
}
7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
bootstrap="tests/autoload.php"
>
<testsuites>
<testsuite name="Unit Tests">
Expand All @@ -15,7 +15,10 @@

<filter>
<whitelist>
<directory>src</directory>
<directory suffix=".php">src</directory>
<exclude>
<directory>src/Generate/PHPFilesInDirectoryGetContents.php</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
1 change: 0 additions & 1 deletion ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<rule ref="SlevomatCodingStandard.ControlStructures.AssignmentInCondition"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowContinueWithoutIntegerOperandInSwitch"/>
<rule ref="SlevomatCodingStandard.ControlStructures.DisallowEmpty"/>
<rule ref="SlevomatCodingStandard.Classes.UnusedPrivateElements"/>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true"/>
Expand Down
68 changes: 2 additions & 66 deletions src/Action/MultiSelectIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,85 +10,21 @@
*/
class MultiSelectIterator implements \Iterator, ActionInterface
{
use SelectIteratorTrait;

/**
* @var MultiRepositoryReadOnlyInterface
*/
private $repository;

/**
* @var array SELECT query to execute
*/
private $query = [];

/**
* @var MultiRepositorySelectQueryInterface|null
*/
private $selectReference;

/**
* @var int
*/
private $position = -1;

/**
* @var array|null
*/
private $lastResult;

public function __construct(MultiRepositoryReadOnlyInterface $repository, array $query)
{
$this->repository = $repository;
$this->query = $query;
}

/**
* @return array|null
*/
public function current()
{
return $this->lastResult;
}

public function next()
{
if (isset($this->selectReference)) {
$this->lastResult = $this->repository->fetch($this->selectReference);
$this->position++;
}
}

/**
* @return int
*/
public function key()
{
return $this->position;
}

/**
* @return bool
*/
public function valid()
{
return ( $this->lastResult === null ? false : true );
}

public function rewind()
{
$this->clear();

$this->selectReference = $this->repository->select($this->query);

$this->next();
}

public function clear()
{
if (isset($this->selectReference)) {
$this->repository->clear($this->selectReference);
}
$this->position = -1;
$this->selectReference = null;
$this->lastResult = null;
}
}
68 changes: 2 additions & 66 deletions src/Action/SelectIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,85 +10,21 @@
*/
class SelectIterator implements \Iterator, ActionInterface
{
use SelectIteratorTrait;

/**
* @var RepositoryReadOnlyInterface
*/
private $repository;

/**
* @var array SELECT query to execute
*/
private $query = [];

/**
* @var RepositorySelectQueryInterface|null
*/
private $selectReference;

/**
* @var int
*/
private $position = -1;

/**
* @var object|null
*/
private $lastResult;

public function __construct(RepositoryReadOnlyInterface $repository, array $query)
{
$this->repository = $repository;
$this->query = $query;
}

/**
* @return object|null
*/
public function current()
{
return $this->lastResult;
}

public function next()
{
if (isset($this->selectReference)) {
$this->lastResult = $this->repository->fetch($this->selectReference);
$this->position++;
}
}

/**
* @return int
*/
public function key()
{
return $this->position;
}

/**
* @return bool
*/
public function valid()
{
return ( $this->lastResult === null ? false : true );
}

public function rewind()
{
$this->clear();

$this->selectReference = $this->repository->select($this->query);

$this->next();
}

public function clear()
{
if (isset($this->selectReference)) {
$this->repository->clear($this->selectReference);
}
$this->position = -1;
$this->selectReference = null;
$this->lastResult = null;
}
}
85 changes: 85 additions & 0 deletions src/Action/SelectIteratorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Squirrel\Entities\Action;

/**
* Iterator basis for SelectEntries and MultiSelectEntries to be used in a foreach loop
*/
trait SelectIteratorTrait
{
/**
* @var object
*/
private $repository;

/**
* @var array SELECT query to execute
*/
private $query = [];

/**
* @var object|null
*/
private $selectReference;

/**
* @var int
*/
private $position = -1;

/**
* @var object|array|null
*/
private $lastResult;

/**
* @return object|array|null
*/
public function current()
{
return $this->lastResult;
}

public function next()
{
if (isset($this->selectReference)) {
$this->lastResult = $this->repository->fetch($this->selectReference);
$this->position++;
}
}

/**
* @return int
*/
public function key()
{
return $this->position;
}

/**
* @return bool
*/
public function valid()
{
return ( $this->lastResult === null ? false : true );
}

public function rewind()
{
$this->clear();

$this->selectReference = $this->repository->select($this->query);

$this->next();
}

public function clear()
{
if (isset($this->selectReference)) {
$this->repository->clear($this->selectReference);
}
$this->position = -1;
$this->selectReference = null;
$this->lastResult = null;
}
}
5 changes: 4 additions & 1 deletion src/Generate/FindClassesWithAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function __invoke(string $fileContents)
$namespaceStarted = false;

// We have found the SQLMapper annotation - so there can be entities in this file
if ($importClassName === 'Squirrel\\Entities\\Annotation') {
if ($importClassName === 'Squirrel\\Entities\\Annotation'
|| $importClassName === 'Squirrel\\Entities\\Annotation\\Entity'
|| $importClassName === 'Squirrel\\Entities\\Annotation\\Field'
) {
$annotationUseFound = true;
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/Generate/PHPFilesInDirectoryGetContents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Squirrel\Entities\Generate;

use Symfony\Component\Finder\Finder;

class PHPFilesInDirectoryGetContents
{
public function __invoke(string $directory)
{
$sourceFinder = new Finder();
$sourceFinder->in($directory)->files()->name('*.php')->sortByName();

// Go through files which were found
foreach ($sourceFinder as $file) {
// Safety check because Finder can return false if the file was not found
if ($file->getRealPath() === false) {
throw new \InvalidArgumentException('File in source directory not found');
}

// Get file contents
$fileContents = \file_get_contents($file->getRealPath());

// Another safety check because file_get_contents can return false if the file was not found
if ($fileContents===false) {
throw new \InvalidArgumentException('File in source directory could not be retrieved');
}

yield [
'filename' => $file->getFilename(),
'path' => $file->getPath(),
'contents' => $fileContents,
];
}
}
}
Loading

0 comments on commit 01fded3

Please sign in to comment.