Skip to content

Commit

Permalink
Allow to run command with a boot file
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jul 12, 2019
1 parent 3aec0eb commit 2e5cc16
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 9 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml export-ignore
/tests export-ignore

* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.phar
composer.lock
/coverage/
/vendor/
/.idea/
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"nesbot/carbon": "^2.20"
},
"require-dev": {
"phpunit/phpunit": "^8"
"phpunit/phpunit": "^7.5 || ^8.2"
},
"autoload": {
"psr-4": {
Expand All @@ -26,7 +26,7 @@
},
"autoload-dev": {
"psr-4": {
"Carbon\\Tests\\": "src/Carbon/Tests/"
"Carbon\\Tests\\": "tests/Carbon/"
}
}
}
25 changes: 25 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
forceCoversAnnotation="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>

<filter>
<whitelist>
<directory>src/Carbon</directory>
</whitelist>
</filter>

<testsuites>
<testsuite name="carbon">
<directory suffix="Test.php">tests/Carbon</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 4 additions & 2 deletions src/Carbon/Command/Macro.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ public function run(SimpleCli $cli): bool
{
/* @var Cli $cli */

$path = realpath($this->sourcePath);
$generator = new Generator();
$generator->writeHelpers($this->class, realpath($this->sourcePath), $this->filePrefix);
$generator->writeHelpers($this->class, $path, $this->filePrefix);

$cli->write('ok');
$cli->writeLine("{$path}_static.php created with static macros.");
$cli->writeLine("{$path}_instantiated.php created with instantiated macros.");

return true;
}
Expand Down
16 changes: 13 additions & 3 deletions src/Carbon/Types/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ class Generator
*/
protected function getMethods($boot)
{
$boot instanceof Closure
? call_user_func($boot)
: Carbon::mixin(new $boot());
if (is_string($boot)) {
if (class_exists($className = $boot)) {
$boot = function () use ($className) {
Carbon::mixin(new $className());
};
} elseif (file_exists($file = $boot)) {
$boot = function () use ($file) {
include $file;
};
}
}

call_user_func($boot);

$c = new ReflectionClass(Carbon::now());
$macros = $c->getProperty('globalMacros');
Expand Down
23 changes: 23 additions & 0 deletions tests/Carbon/CliTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Carbon\Tests;

use Carbon\Cli;
use Carbon\Command\Macro;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \Carbon\Cli
*/
class CliTest extends TestCase
{
/**
* @covers ::getCommands
*/
public function testGetCommands()
{
$this->assertSame([
'macro' => Macro::class,
], (new Cli())->getCommands());
}
}
71 changes: 71 additions & 0 deletions tests/Carbon/Command/MacroTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Carbon\Tests\Command;

use Carbon\Cli;
use Carbon\Tests\DummyMixin;
use Carbon\Tests\TestCase;

/**
* @coversDefaultClass \Carbon\Command\Macro
*/
class MacroTest extends TestCase
{
/**
* @covers ::run
* @covers \Carbon\Types\Generator::getMethods
* @covers \Carbon\Types\Generator::getMethodsDefinitions
* @covers \Carbon\Types\Generator::dumpParameter
* @covers \Carbon\Types\Generator::dumpValue
* @covers \Carbon\Types\Generator::writeHelpers
*/
public function testRun()
{
$dir = sys_get_temp_dir().'/macro-test-'.mt_rand(0, 999999);
@mkdir($dir);
chdir($dir);
$cli = new Cli();
$cli->mute();
$cli('carbon', 'macro', DummyMixin::class, '--source-path', __DIR__.'/..');

$this->assertSame([
'.',
'..',
'types',
], scandir($dir));
$this->assertSame([
'.',
'..',
'_ide_carbon_mixin_instantiated.php',
'_ide_carbon_mixin_static.php',
], scandir("$dir/types"));
$this->assertFileEquals(__DIR__.'/_ide_carbon_mixin_instantiated.php', "$dir/types/_ide_carbon_mixin_instantiated.php");
$this->assertFileEquals(__DIR__.'/_ide_carbon_mixin_static.php', "$dir/types/_ide_carbon_mixin_static.php");

$this->removeDirectory($dir);
}

/**
* @covers ::run
* @covers \Carbon\Types\Generator::getMethods
* @covers \Carbon\Types\Generator::getMethodsDefinitions
* @covers \Carbon\Types\Generator::dumpParameter
* @covers \Carbon\Types\Generator::dumpValue
* @covers \Carbon\Types\Generator::writeHelpers
*/
public function testRunWithFile()
{
$dir = sys_get_temp_dir().'/macro-test-'.mt_rand(0, 999999);
@mkdir($dir);
chdir($dir);
file_put_contents('test.php', '<?php \Carbon\Carbon::macro(\'foo\', function () { return 42; });');
$cli = new Cli();
$cli->mute();
$cli('carbon', 'macro', 'test.php');

$contents = file_get_contents("$dir/types/_ide_carbon_mixin_instantiated.php");
$this->assertStringContainsString('public function foo()', $contents);

$this->removeDirectory($dir);
}
}
65 changes: 65 additions & 0 deletions tests/Carbon/Command/_ide_carbon_mixin_instantiated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Carbon
{
class Carbon
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}

namespace Carbon
{
class CarbonImmutable
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}

namespace Illuminate\Support
{
class Carbon
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}

namespace Illuminate\Support\Facades
{
class Date
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}
65 changes: 65 additions & 0 deletions tests/Carbon/Command/_ide_carbon_mixin_static.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Carbon
{
class Carbon
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public static function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}

namespace Carbon
{
class CarbonImmutable
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public static function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}

namespace Illuminate\Support
{
class Carbon
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public static function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}

namespace Illuminate\Support\Facades
{
class Date
{
/**
* @see \DummyMixin::sayHi
*
* Say "Hi!" to a given person name.
*/
public static function sayHi(string $name)
{
// Content, see src/DummyMixin.php:15
}
}
}
4 changes: 2 additions & 2 deletions src/Carbon/Test.php → tests/Carbon/DummyMixin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Carbon;
namespace Carbon\Tests;

class Test
class DummyMixin
{
/**
* @return \Closure
Expand Down
40 changes: 40 additions & 0 deletions tests/Carbon/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Carbon\Tests;

use PHPUnit\Framework\TestCase as FrameworkTestCase;

class TestCase extends FrameworkTestCase
{
/**
* Remove a directory and all sub-directories and files inside.
*
* @param string $directory
*
* @return void
*/
protected function removeDirectory($directory)
{
if (!($dir = @opendir($directory))) {
return;
}

while (false !== ($file = readdir($dir))) {
if ($file === '.' || $file === '..') {
continue;
}

if (is_dir($directory.'/'.$file)) {
$this->removeDirectory($directory.'/'.$file);

continue;
}

unlink($directory.'/'.$file);
}

closedir($dir);

@rmdir($directory);
}
}

0 comments on commit 2e5cc16

Please sign in to comment.