Completed
Push — master ( 98a062...441031 )
by Ryan
10:10
created

WriteEntityTestCases::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Stream\Console\Command;
2
3
use Anomaly\Streams\Platform\Addon\Addon;
4
use Anomaly\Streams\Platform\Support\Parser;
5
use Illuminate\Filesystem\Filesystem;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
/**
9
 * Class WriteEntityTestCases
10
 *
11
 * @link   http://pyrocms.com/
12
 * @author PyroCMS, Inc. <[email protected]>
13
 * @author Ryan Thompson <[email protected]>
14
 */
15
class WriteEntityTestCases
16
{
17
18
    /**
19
     * The entity slug.
20
     *
21
     * @var string
22
     */
23
    private $slug;
24
25
    /**
26
     * The addon instance.
27
     *
28
     * @var Addon
29
     */
30
    private $addon;
31
32
    /**
33
     * The entity stream namespace.
34
     *
35
     * @var string
36
     */
37
    private $namespace;
38
39
40
    /**
41
     * Create a new WriteEntityTestCases instance.
42
     *
43
     * @param Addon $addon
44
     * @param       $slug
45
     * @param       $namespace
46
     */
47
    public function __construct(Addon $addon, $slug, $namespace)
48
    {
49
        $this->slug      = $slug;
50
        $this->addon     = $addon;
51
        $this->namespace = $namespace;
52
    }
53
54
    /**
55
     * Handle the command.
56
     *
57
     * @param Parser     $parser
58
     * @param Filesystem $filesystem
59
     */
60
    public function handle(Parser $parser, Filesystem $filesystem)
61
    {
62
        $suffix = ucfirst(camel_case($this->slug));
63
        $entity = str_singular($suffix);
64
65
        $addon     = ucfirst(camel_case($this->addon->getSlug() . '_' . $this->addon->getType()));
66
        $base      = $this->addon->getTransformedClass("Test\\{$addon}TestCase");
67
        $namespace = $this->addon->getTransformedClass("Test\\Unit\\{$entity}");
68
        $extends   = "{$addon}TestCase";
69
70
        $template = $filesystem->get(
71
            base_path("vendor/anomaly/streams-platform/resources/stubs/entity/test.stub")
72
        );
73
74
        /* @var SplFileInfo $file */
75
        foreach ($filesystem->allFiles($this->addon->getPath("src/{$entity}")) as $file) {
76
77
            // Skip interfaces.
78
            if (str_contains($file->getFilename(), 'Interface')) {
79
                continue;
80
            }
81
82
            $prefix   = str_replace($this->addon->getPath('src/'), '', $file->getPath());
83
            $basename = basename($file->getRealPath(), '.php');
84
            $class    = "{$basename}Test";
85
86
            $path = $this->addon->getPath("tests/Unit/{$prefix}/{$basename}Test.php");
87
88
            $filesystem->makeDirectory(dirname($path), 0755, true, true);
89
90
            $filesystem->put($path, $parser->parse($template, compact('class', 'namespace', 'base', 'extends')));
91
        }
92
    }
93
}
94