Skip to content

Commit 5128c33

Browse files
committed
bug fix for array replaceables and tests update
1 parent fe54ca4 commit 5128c33

File tree

4 files changed

+175
-9
lines changed

4 files changed

+175
-9
lines changed

src/StubFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function buildUseableContentFromStubContent(array $replacers) {
9595
$value = '["'.implode('", "', $value).'"]';
9696
}
9797

98-
$this->replaceInStub('"'.$key.'"', $value);
98+
$this->replaceInStub($key, $value);
9999

100100
continue;
101101
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Touhidurabir\SeedExtender\BaseTableSeeder;
6+
7+
class ExamplesTableSeeder extends BaseTableSeeder {
8+
9+
/**
10+
* Seeder table name
11+
*
12+
* @var string
13+
*/
14+
protected $table = "examples";
15+
16+
17+
/**
18+
* The table attributes/columns that will be ignored during the seeding process
19+
*
20+
* @var array
21+
*/
22+
protected $ignorables = ["id", "deleted_at"];
23+
24+
25+
/**
26+
* The table attributes/columns that will be used during the seeding process
27+
*
28+
* @var array
29+
*/
30+
protected $useables = [];
31+
32+
33+
/**
34+
* Should merge and include timestamps[created_at, updated_at] by default into the seed data
35+
*
36+
* @var boolean
37+
*/
38+
protected $includeTimestampsOnSeeding = true;
39+
40+
41+
/**
42+
* The seeding data
43+
*
44+
* @var array
45+
*/
46+
protected $data = [
47+
48+
];
49+
50+
51+
/**
52+
* Build up the seedeable data set;
53+
*
54+
* @return array
55+
*/
56+
protected function seedableDataBuilder() {
57+
58+
foreach ($this->data as $key => $value) {
59+
60+
$this->data[$key] = array_merge($value, [
61+
62+
]);
63+
}
64+
65+
return $this->data;
66+
}
67+
}

tests/StubGeneratorTest.php

+40-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Exception;
66
use FilesystemIterator;
7+
use RecursiveIteratorIterator;
8+
use RecursiveDirectoryIterator;
79
use Orchestra\Testbench\TestCase;
810
use Illuminate\Support\Facades\File;
9-
use RecursiveDirectoryIterator;
10-
use RecursiveIteratorIterator;
1111
use Touhidurabir\StubGenerator\StubGenerator;
1212
use Touhidurabir\StubGenerator\Tests\Traits\LaravelTestBootstrapping;
1313
use Touhidurabir\StubGenerator\Facades\StubGenerator as StubGeneratorFacade;
@@ -23,6 +23,7 @@ class StubGeneratorTest extends TestCase {
2323
*/
2424
protected $cleanUpExcludeFileNames = [
2525
'ExampleRepository.php',
26+
'ExamplesTableSeeder.php',
2627
];
2728

2829

@@ -47,20 +48,27 @@ protected function setUp(): void {
4748
}
4849
}
4950

51+
foreach(glob(__DIR__ . '/App/Seeders/*.*') as $fileFullPath) {
52+
53+
if ( ! in_array( last(explode('/', $fileFullPath)), $self->cleanUpExcludeFileNames ) ) {
54+
55+
File::delete($fileFullPath);
56+
}
57+
}
58+
5059
if ( File::isDirectory(__DIR__ . '/App/Repositories/Extras') ) {
5160

5261
array_map('unlink', glob(__DIR__ . '/App/Repositories/Extras/*.*'));
5362

54-
$dir = __DIR__ . '/App/Repositories/Extras';
63+
$dir = __DIR__ . '/App/Repositories/Extras';
5564
$extras = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
5665
$extras = new RecursiveIteratorIterator($extras, RecursiveIteratorIterator::CHILD_FIRST);
66+
5767
foreach($extras as $file) {
58-
if ($file->isDir()) {
59-
rmdir($file->getPathname());
60-
} else {
61-
unlink($file->getPathname());
62-
}
68+
69+
$file->isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname());
6370
}
71+
6472
rmdir($dir);
6573
}
6674
});
@@ -336,4 +344,28 @@ public function it_can_download_file_of_given_name_with_generated_content_based_
336344
$this->assertEquals($stubGenerator->getStatusCode(), 200);
337345
}
338346

347+
348+
/**
349+
* @test
350+
*/
351+
public function it_will_generate_file_with_proper_matched_contant_as_expected() {
352+
353+
$content = StubGeneratorFacade::from(__DIR__ . '/stubs/seeder.stub', true)
354+
->to(__DIR__ . '/App/Seeders')
355+
->as('TestsTableSeeder')
356+
->withReplacers([
357+
'class' => 'ExamplesTableSeeder',
358+
'table' => 'examples',
359+
'ignorables' => ['id', 'deleted_at'],
360+
'useables' => [],
361+
'timestamp' => true,
362+
])
363+
->toString();
364+
365+
$this->assertEquals(
366+
trim($content),
367+
trim(File::get(__DIR__ . '/App/Seeders/ExamplesTableSeeder.php'))
368+
);
369+
}
370+
339371
}

tests/stubs/seeder.stub

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use Touhidurabir\SeedExtender\BaseTableSeeder;
6+
7+
class {{class}} extends BaseTableSeeder {
8+
9+
/**
10+
* Seeder table name
11+
*
12+
* @var string
13+
*/
14+
protected $table = "{{table}}";
15+
16+
17+
/**
18+
* The table attributes/columns that will be ignored during the seeding process
19+
*
20+
* @var array
21+
*/
22+
protected $ignorables = {{ignorables}};
23+
24+
25+
/**
26+
* The table attributes/columns that will be used during the seeding process
27+
*
28+
* @var array
29+
*/
30+
protected $useables = {{useables}};
31+
32+
33+
/**
34+
* Should merge and include timestamps[created_at, updated_at] by default into the seed data
35+
*
36+
* @var boolean
37+
*/
38+
protected $includeTimestampsOnSeeding = {{timestamp}};
39+
40+
41+
/**
42+
* The seeding data
43+
*
44+
* @var array
45+
*/
46+
protected $data = [
47+
48+
];
49+
50+
51+
/**
52+
* Build up the seedeable data set;
53+
*
54+
* @return array
55+
*/
56+
protected function seedableDataBuilder() {
57+
58+
foreach ($this->data as $key => $value) {
59+
60+
$this->data[$key] = array_merge($value, [
61+
62+
]);
63+
}
64+
65+
return $this->data;
66+
}
67+
}

0 commit comments

Comments
 (0)