Skip to content

Commit

Permalink
Feature/more tests (#3)
Browse files Browse the repository at this point in the history
* Tests for Entity and FindRequest
* FindRequest 100% coverage
* coverage 100%
* labels from scrutinizer
  • Loading branch information
somework committed Oct 28, 2019
1 parent 7274757 commit c2e18b1
Show file tree
Hide file tree
Showing 20 changed files with 649 additions and 42 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/somework/minjust/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/somework/minjust/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/somework/minjust/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/somework/minjust/?branch=master)
[![Build Status](https://scrutinizer-ci.com/g/somework/minjust/badges/build.png?b=master)](https://scrutinizer-ci.com/g/somework/minjust/build-status/master)

Example

```$xslt
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"friendsofphp/php-cs-fixer": "^2.14",
"phpunit/phpunit": "^8.4",
"http-interop/http-factory-guzzle": "^1.0",
"divineomega/psr-18-guzzle-adapter": "^1.0"
"divineomega/psr-18-guzzle-adapter": "^1.0",
"rector/rector": "^0.5.19"
}
}
17 changes: 17 additions & 0 deletions rector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
parameters:
autoload_paths:
- 'vendor/autoload.php'
exclude_paths:
- 'tests/data/*'

imports:
- { resource: 'vendor/rector/rector/config/set/coding-style/*.yaml' }
- { resource: 'vendor/rector/rector/config/set/code-quality/*.yaml' }
- { resource: 'vendor/rector/rector/config/set/dead-code/*.yaml' }
- { resource: 'vendor/rector/rector/config/set/php/php72.yaml' }
- { resource: 'vendor/rector/rector/config/set/phpunit/phpunit80.yaml' }
- { resource: 'vendor/rector/rector/config/set/phpunit/phpunit-code-quality.yaml' }
- { resource: 'vendor/rector/rector/config/set/phpunit/phpunit-exception.yaml' }
- { resource: 'vendor/rector/rector/config/set/phpunit/phpunit-mock.yaml' }
- { resource: 'vendor/rector/rector/config/set/phpunit/phpunit-yield-data-provider.yaml' }
# - { resource: 'vendor/rector/rector/config/set/phpunit/phpunit-specific-method.yaml' }
3 changes: 3 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

/**
* @see \SomeWork\Minjust\Tests\Unit\ClientTest
*/
class Client
{
/**
Expand Down
12 changes: 8 additions & 4 deletions src/Entity/DetailLawyer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@

namespace SomeWork\Minjust\Entity;

/**
* @see \SomeWork\Minjust\Tests\Unit\Entity\DetailLawyerTest
*/
class DetailLawyer extends Lawyer
{
/**
* @var string
*/
protected $chamberOfLaw;
protected $chamberOfLaw = '';

/**
* @var LawFormation|null
*/
protected $lawFormation;

public static function init(Lawyer $lawyer): DetailLawyer
public function __construct(?Lawyer $lawyer = null)
{
return (new self())
->loadFromLawyer($lawyer);
if ($lawyer !== null) {
$this->loadFromLawyer($lawyer);
}
}

public function loadFromLawyer(Lawyer $lawyer): self
Expand Down
13 changes: 8 additions & 5 deletions src/Entity/LawFormation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,35 @@

namespace SomeWork\Minjust\Entity;

/**
* @see \SomeWork\Minjust\Tests\Unit\Entity\LawFormationTest
*/
class LawFormation
{
/**
* @var string
*/
protected $organizationalForm;
protected $organizationalForm = '';

/**
* @var string
*/
protected $name;
protected $name = '';

/**
* @var string
*/
protected $address;
protected $address = '';

/**
* @var string
*/
protected $phone;
protected $phone = '';

/**
* @var string
*/
protected $email;
protected $email = '';

/**
* @return string
Expand Down
15 changes: 9 additions & 6 deletions src/Entity/Lawyer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@

namespace SomeWork\Minjust\Entity;

/**
* @see \SomeWork\Minjust\Tests\Unit\Entity\LawyerTest
*/
class Lawyer
{
/**
* @var string
*/
protected $fullName;
protected $fullName = '';

/**
* @var string
*/
protected $registerNumber;
protected $registerNumber = '';

/**
* @var string
*/
protected $certificateNumber;
protected $certificateNumber = '';

/**
* @var string
*/
protected $status;
protected $status = '';

/**
* @var string
*/
protected $territorialSubject;
protected $territorialSubject = '';

/**
* @var string
*/
protected $url;
protected $url = '';

/**
* @return string
Expand Down
20 changes: 18 additions & 2 deletions src/FindRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use InvalidArgumentException;

/**
* @see \SomeWork\Minjust\Tests\Unit\FindRequestTest
*/
class FindRequest
{
/**
Expand Down Expand Up @@ -49,7 +52,7 @@ class FindRequest
/**
* @var int
*/
private const MAX_VALUE_MAX = 100;
public const MAX_VALUE_MAX = 100;

/**
* @var string
Expand Down Expand Up @@ -81,12 +84,15 @@ class FindRequest
*/
protected $territorialSubject;

/**
* @var bool
*/
protected $fullData = false;

/**
* @var int
*/
protected $max = 50;
protected $max = FindRequest::MAX_VALUE_MAX;

/**
* @var int
Expand Down Expand Up @@ -242,6 +248,11 @@ public function getMax(): int
*/
public function setMax(int $max): FindRequest
{
if ($max < 1) {
throw new InvalidArgumentException(
sprintf('Minimum value for "%s" is %s', static::MAX, 1)
);
}
if ($max > static::MAX_VALUE_MAX) {
throw new InvalidArgumentException(
sprintf('Maximum value for "%s" is %s', static::MAX, static::MAX_VALUE_MAX)
Expand All @@ -267,6 +278,11 @@ public function getOffset(): int
*/
public function setOffset(int $offset): FindRequest
{
if ($offset < 0) {
throw new InvalidArgumentException(
sprintf('Minimum value for "%s" is %s', static::OFFSET, 0)
);
}
$this->offset = $offset;

return $this;
Expand Down
3 changes: 3 additions & 0 deletions src/FindResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use SomeWork\Minjust\Entity\DetailLawyer;
use SomeWork\Minjust\Entity\Lawyer;

/**
* @see \SomeWork\Minjust\Tests\Unit\FindResponseTest
*/
class FindResponse
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Parser/DomParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use SomeWork\Minjust\Entity\Lawyer;
use SomeWork\Minjust\FindResponse;

/**
* @see \SomeWork\Minjust\Tests\Unit\DomParserTest
*/
class DomParser implements ParserInterface
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use SomeWork\Minjust\Entity\Lawyer;
use SomeWork\Minjust\Parser\ParserInterface;

/**
* @see \SomeWork\Minjust\Tests\Unit\ServiceTest
*/
class Service
{
/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ClientTest extends TestCase
{
/**
* @covers ::__construct
* @throws \ReflectionException
* @throws ReflectionException
*/
public function testConstruct(): void
{
Expand Down
115 changes: 115 additions & 0 deletions tests/Unit/Entity/DetailLawyerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace SomeWork\Minjust\Tests\Unit\Entity;

use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionObject;
use SomeWork\Minjust\Entity\DetailLawyer;
use SomeWork\Minjust\Entity\LawFormation;
use SomeWork\Minjust\Entity\Lawyer;

/**
* @coversDefaultClass \SomeWork\Minjust\Entity\DetailLawyer
* @covers \SomeWork\Minjust\Entity\DetailLawyer
*/
class DetailLawyerTest extends TestCase
{
public function testEmpty(): DetailLawyer
{
$lawyer = new DetailLawyer();
$this->assertIsString($this->getPropertyValue($lawyer, 'chamberOfLaw'));
$this->assertNull($this->getPropertyValue($lawyer, 'lawFormation'));

return $lawyer;
}

protected function getPropertyValue(object $object, string $property)
{
$ref = new ReflectionObject($object);
$property = $ref->getProperty($property);
$property->setAccessible(true);

return $property->getValue($object);
}

/**
* @depends testEmpty
*
* @param \SomeWork\Minjust\Entity\DetailLawyer $lawyer
*
* @return \SomeWork\Minjust\Entity\DetailLawyer
*/
public function testSet(DetailLawyer $lawyer): DetailLawyer
{
$lawFormation = (new LawFormation())->setName('testLawFormation');

$lawyer
->setChamberOfLaw('testChamberOfLaw')
->setLawFormation($lawFormation);

$this->assertEquals('testChamberOfLaw', $this->getPropertyValue($lawyer, 'chamberOfLaw'));
$this->assertEquals($lawFormation, $this->getPropertyValue($lawyer, 'lawFormation'));

return $lawyer;
}

/**
* @depends testSet
*
* @param \SomeWork\Minjust\Entity\DetailLawyer $lawyer
*/
public function testGet(DetailLawyer $lawyer): void
{
$lawFormation = $this->getPropertyValue($lawyer, 'lawFormation');

$this->assertEquals('testChamberOfLaw', $lawyer->getChamberOfLaw());
$this->assertEquals($lawFormation, $lawyer->getLawFormation());
}

public function testLoadFromLawyer(): void
{
$lawyer = (new Lawyer())
->setUrl('testUrl')
->setFullName('testFullName')
->setStatus('testStatus')
->setRegisterNumber('testRegisterNumber')
->setCertificateNumber('testCertificateNumber')
->setTerritorialSubject('testTerritorialSubject');

$detailLawyer = (new DetailLawyer())->loadFromLawyer($lawyer);

$this->assertEquals($lawyer->getUrl(), $detailLawyer->getUrl());
$this->assertEquals($lawyer->getFullName(), $detailLawyer->getFullName());
$this->assertEquals($lawyer->getStatus(), $detailLawyer->getStatus());
$this->assertEquals($lawyer->getRegisterNumber(), $detailLawyer->getRegisterNumber());
$this->assertEquals($lawyer->getCertificateNumber(), $detailLawyer->getCertificateNumber());
$this->assertEquals($lawyer->getTerritorialSubject(), $detailLawyer->getTerritorialSubject());
}

/**
* @covers ::__construct
*/
public function testConstruct(): void
{
$lawyer = (new Lawyer())
->setUrl('testUrl')
->setFullName('testFullName')
->setStatus('testStatus')
->setRegisterNumber('testRegisterNumber')
->setCertificateNumber('testCertificateNumber')
->setTerritorialSubject('testTerritorialSubject');

$detailLawyer = $this->createPartialMock(DetailLawyer::class, ['loadFromLawyer']);

$detailLawyer
->expects($this->once())
->method('loadFromLawyer')
->with($lawyer)
->willReturn($detailLawyer);

$reflectedClass = new ReflectionClass(DetailLawyer::class);
$constructor = $reflectedClass->getConstructor();
$constructor->invoke($detailLawyer, $lawyer);
}
}

0 comments on commit c2e18b1

Please sign in to comment.