Skip to content

Commit 73339c7

Browse files
committed
Starting unit tests
1 parent eb5440b commit 73339c7

File tree

7 files changed

+158
-2
lines changed

7 files changed

+158
-2
lines changed

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,17 @@
2121
"psr-4": {
2222
"Torzer\\Common\\": "src/"
2323
}
24+
},
25+
"autoload-dev": {
26+
"classmap": [
27+
"tests"
28+
]
29+
},
30+
"require": {
31+
"php": ">=5.4.0"
32+
},
33+
"require-dev": {
34+
"phpunit/phpunit" : "5.*",
35+
"laravel/laravel": "5.*"
2436
}
2537
}

phpunit.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
processIsolation="false"
9+
stopOnFailure="false"
10+
syntaxCheck="false"
11+
>
12+
<testsuites>
13+
<testsuite name="Package Test Suite">
14+
<directory suffix=".php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<php>
23+
<env name="APP_ENV" value="testing"/>
24+
<env name="CACHE_DRIVER" value="array"/>
25+
<env name="SESSION_DRIVER" value="array"/>
26+
<env name="QUEUE_DRIVER" value="sync"/>
27+
</php>
28+
</phpunit>

src/Traits/MapDateTimeMutator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ public function fromDateTimeFormatMutator($key, $value, $formats)
6464

6565
$value = $this->asDateTime($value);
6666

67-
$value = $this->setDateOnlyMongo($value, $formats);
68-
6967
$this->setDateFormat($originalDateFormat);
7068

69+
if ($this instanceof \Jenssegers\Mongodb\Eloquent\Model)
70+
{
71+
return $this->setDateOnlyMongo($value, $formats);
72+
}
73+
7174
return $value->format($formats['to']);
7275
}
7376

tests/DateTimeModel.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Database\Eloquent\Model;
4+
5+
use Torzer\Common\Traits\MapDateTimeMutator;
6+
7+
class DateTimeModel extends Model
8+
{
9+
10+
use MapDateTimeMutator;
11+
12+
protected $dates = ['ignored_at', 'started_at', 'finished_at'];
13+
14+
protected $mapDateTimeMutator = [
15+
'started_at' => [
16+
'from' => 'd/m/Y',
17+
'to' => 'Y-m-d'
18+
],
19+
];
20+
21+
}

tests/DateTimeMongoModel.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Jenssegers\Mongodb\Eloquent\Model;
4+
5+
use Torzer\Common\Traits\MapDateTimeMutator;
6+
7+
class DateTimeMongoModel extends Model
8+
{
9+
10+
use MapDateTimeMutator;
11+
12+
protected $dates = ['ignored_at', 'started_at', 'finished_at'];
13+
14+
protected $mapDateTimeMutator = [
15+
'started_at' => [
16+
'from' => 'd/m/Y',
17+
'to' => 'Y-m-d',
18+
'date-only' => true,
19+
],
20+
];
21+
22+
}

tests/MDTMTestCase.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
class MDTMTestCase extends Illuminate\Foundation\Testing\TestCase
4+
{
5+
/**
6+
* The base URL to use while testing the application.
7+
*
8+
* @var string
9+
*/
10+
protected $baseUrl = 'http://localhost';
11+
12+
/**
13+
* Creates the application.
14+
*
15+
* @return \Illuminate\Foundation\Application
16+
*/
17+
public function createApplication()
18+
{
19+
$app = require __DIR__.'/../bootstrap/app.php';
20+
21+
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
22+
23+
return $app;
24+
}
25+
}

tests/MapDateTimeMutatorTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Illuminate\Foundation\Testing\WithoutMiddleware;
4+
use Illuminate\Foundation\Testing\DatabaseMigrations;
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
7+
use Torzer\Common\Traits\MapDateTimeMutator;
8+
9+
class MapDateTimeMutatorTest extends TestCase
10+
{
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->app->setLocale('pt-BR');
17+
Config::set('app.timezone', 'America/Sao_Paulo');
18+
}
19+
20+
public function testDeafultDateTimeMutatorFromDMYtoYMD()
21+
{
22+
23+
$model = new DateTimeModel();
24+
25+
$model->started_at = '20/10/2017';
26+
$model->created_at = \Carbon\Carbon::now();
27+
28+
echo "Model in MapDateTimeMutator Test";
29+
dump($model->started_at);
30+
dump($model->created_at);
31+
32+
}
33+
34+
public function testDeafultDateTimeMutatorMongoDBFromDMYtoYMD()
35+
{
36+
37+
$model = new DateTimeMongoModel();
38+
39+
$model->started_at = '20/10/2017';
40+
41+
echo "Mongo Model in MapDateTimeMutator Test";
42+
dump($model->started_at);
43+
44+
}
45+
}

0 commit comments

Comments
 (0)