Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Tests\\Unit\\NestedTest::testCasting":4,"Tests\\Unit\\SimpleTest::testMake":4,"Tests\\Unit\\SimpleTest::testConstruct":4,"Tests\\Unit\\ToJsonTest::testToJson":4},"times":{"Tests\\Unit\\NestedTest::testCasting":0.005,"Tests\\Unit\\SimpleTest::testMake":0,"Tests\\Unit\\SimpleTest::testConstruct":0,"Tests\\Unit\\SimpleTest::testToArray":0,"Tests\\Unit\\ToJsonTest::testToJson":0}}
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
"Akbarali\\DataObject\\":"src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
},
"authors":[
{
"name":"Akbarali",
Expand All @@ -26,6 +31,7 @@
"illuminate/database":">=7"
},
"require-dev":{
"roave/security-advisories":"dev-latest"
"roave/security-advisories":"dev-latest",
"phpunit/phpunit":"^9.6"
}
}
2,532 changes: 2,178 additions & 354 deletions composer.lock

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions phpunit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
|--------------------------------------------------------------------------
| Register The Composer Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/

require __DIR__ . '/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Set The Default Timezone
|--------------------------------------------------------------------------
|
| Here we will set the default timezone for PHP. PHP is notoriously mean
| if the timezone is not explicitly set. This will be used by each of
| the PHP date and date-time functions throughout the application.
|
*/

date_default_timezone_set('Asia/Tashkent');
31 changes: 31 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="phpunit.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/logs/coverage"/>
<text outputFile="build/logs/coverage.txt"/>
</report>
</coverage>
<testsuites>
<testsuite name="DataObject Testing">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
15 changes: 15 additions & 0 deletions tests/Fixtures/Nested/Company.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace Tests\Fixtures\Nested;

use Akbarali\DataObject\DataObjectBase;

class Company extends DataObjectBase
{
public string $title;

/** @var array<\Tests\Fixtures\Nested\Project> */
public array $projects;

}
13 changes: 13 additions & 0 deletions tests/Fixtures/Nested/Developer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);

namespace Tests\Fixtures\Nested;

use Akbarali\DataObject\DataObjectBase;

class Developer extends DataObjectBase
{
public $name;

public $email;
}
23 changes: 23 additions & 0 deletions tests/Fixtures/Nested/Project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace Tests\Fixtures\Nested;

use Akbarali\DataObject\DataObjectBase;

class Project extends DataObjectBase
{
public $title;

public $domain;

/** @var array<\Tests\Fixtures\Nested\Developer> */
public $developers;

protected function castDevelopers(array $developers): array
{
return array_map(static function (array $developer) {
return Developer::fromArray($developer);
}, $developers);
}
}
30 changes: 30 additions & 0 deletions tests/Fixtures/Simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Tests\Fixtures;

use Akbarali\DataObject\DataObjectBase;

class Simple extends DataObjectBase
{
public string $foo;

protected string $bar;

private string $baz;

public function getFoo(): string
{
return $this->foo;
}

public function getBar(): string
{
return $this->bar;
}

public function getBaz(): string
{
return $this->baz;
}
}
18 changes: 18 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
protected string $foo = 'Foo';

protected string $bar = 'Bar';

protected string $baz = 'Baz';

protected string $baq = 'Baq';
}
60 changes: 60 additions & 0 deletions tests/Unit/NestedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace Tests\Unit;

use Tests\Fixtures\Nested\Company;
use Tests\TestCase;

class NestedTest extends TestCase
{
protected array $values = [
'title' => 'First Company',

'projects' => [
[
'title' => 'Project 1',
'domain' => 'https://example.com',

'developers' => [
[
'name' => 'Andrey Helldar',
'email' => 'helldar@ai-rus.com',
],
[
'name' => 'John Doe',
'email' => 'doe@example.com',
],
[
'name' => 'Luke Skywalker',
'email' => 'son@example.com',
],
],
],
],
];

public function testCasting(): void
{
$object = Company::fromArray($this->values);
die(print_r($object->projects[0]));

$this->assertSame('First Company', $object->title);

$this->assertIsArray($object->projects);

$this->assertSame('Project 1', $object->projects[0]->title);
$this->assertSame('https://example.com', $object->projects[0]->domain);

$this->assertIsArray($object->projects[0]->developers);

$this->assertSame('Andrey Helldar', $object->projects[0]->developers[0]->name);
$this->assertSame('helldar@ai-rus.com', $object->projects[0]->developers[0]->email);

$this->assertSame('John Doe', $object->projects[0]->developers[1]->name);
$this->assertSame('doe@example.com', $object->projects[0]->developers[1]->email);

$this->assertSame('Luke Skywalker', $object->projects[0]->developers[2]->name);
$this->assertSame('son@example.com', $object->projects[0]->developers[2]->email);
}
}
58 changes: 58 additions & 0 deletions tests/Unit/Simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Tests\Fixtures\Simple;
use Tests\TestCase;

class SimpleTest extends TestCase
{
public function testMake(): void
{
$object = Simple::fromArray([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$this->assertSame($this->foo, $object->foo);

$this->assertSame($this->foo, $object->getFoo());
$this->assertSame($this->bar, $object->getBar());

$this->assertNull($object->getBaz());
}

public function testConstruct(): void
{
$object = Simple::fromArray([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$this->assertSame($this->foo, $object->foo);

$this->assertSame($this->foo, $object->getFoo());
$this->assertSame($this->bar, $object->getBar());

$this->assertNull($object->getBaz());
}

public function testToArray(): void
{
$object = Simple::fromArray([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$this->assertIsArray($object->toArray());

$this->assertSame([
'foo' => $this->foo,
], $object->toArray());
}
}