Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public final function writeFile(): bool
$fileContents = $this->generateFileContents();

$filePath = $this->writeDir;
if (substr($filePath, -1) !== '/') {
$filePath .= '/';
if (substr($filePath, -1) !== DIRECTORY_SEPARATOR) {
$filePath .= DIRECTORY_SEPARATOR;
}
$filePath .= $this->fileName . '.php';

Expand Down Expand Up @@ -138,6 +138,6 @@ public function changeWriteDir(string $writeDir)
*/
public function getWritePath(): string
{
return $this->writeDir . "/$this->fileName.php";
return $this->writeDir . DIRECTORY_SEPARATOR . $this->fileName . ".php";
}
}
2 changes: 1 addition & 1 deletion src/SchemaGenerator/SchemaClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private function setWriteDir(): void
$currentDir = dirname($currentDir);
}

$this->writeDir = $currentDir . '/schema_object';
$this->writeDir = $currentDir . DIRECTORY_SEPARATOR .'schema_object';
}

/**
Expand Down
55 changes: 43 additions & 12 deletions tests/AbstractCodeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AbstractCodeFileTest extends CodeFileTestCase
*/
protected static function getExpectedFilesDir()
{
return parent::getExpectedFilesDir() . '/abstract_code_files';
return parent::getExpectedFilesDir() . DIRECTORY_SEPARATOR . 'abstract_code_files';
}

/**
Expand All @@ -39,7 +39,7 @@ protected function setUp(): void
AbstractCodeFile::class,
[static::getGeneratedFilesDir(), $this->fileName]
);
$this->codeFile->method('generateFileContents')->willReturn("<?php\n");
$this->codeFile->method('generateFileContents')->willReturn("<?php" . PHP_EOL);
}

/**
Expand All @@ -53,9 +53,9 @@ public function testInvalidWriteDirInConstructor()
$this->expectException(\Exception::class);
$mock = $this->getMockForAbstractClass(
AbstractCodeFile::class,
[static::getGeneratedFilesDir() . '/invalid', $this->fileName]
[static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . 'invalid', $this->fileName]
);
$mock->method('generateFileContents')->willReturn("<?php\n");
$mock->method('generateFileContents')->willReturn("<?php" . PHP_EOL);
}

/**
Expand All @@ -66,16 +66,27 @@ public function testInvalidWriteDirInConstructor()
*/
public function testUnwritableDirInConstructor()
{
$testDir = static::getGeneratedFilesDir() . '/unwritable-constructor';

$uname = strtolower(php_uname());

if (strpos($uname, "darwin") !== false) {
// It's OSX
}
elseif (strpos($uname, "win") !== false) {
$this->markTestSkipped('Skipped on WIN');
}

$testDir = static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . 'unwritable-constructor';
mkdir($testDir);
chmod($testDir, 0444);

$this->expectException(\Exception::class);

$mock = $this->getMockForAbstractClass(
AbstractCodeFile::class,
[$testDir, $this->fileName]
);
$mock->method('generateFileContents')->willReturn("<?php\n");
$mock->method('generateFileContents')->willReturn("<?php" . PHP_EOL);
}

/**
Expand All @@ -86,8 +97,9 @@ public function testUnwritableDirInConstructor()
*/
public function testInvalidWriteDir()
{

$this->expectException(\Exception::class);
$this->codeFile->changeWriteDir(static::getGeneratedFilesDir() . '/invalid');
$this->codeFile->changeWriteDir(static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . 'invalid');
}

/**
Expand All @@ -98,7 +110,16 @@ public function testInvalidWriteDir()
*/
public function testUnwritableDir()
{
$testDir = static::getGeneratedFilesDir() . '/unwritable';
$uname = strtolower(php_uname());

if (strpos($uname, "darwin") !== false) {
// It's OSX
}
elseif (strpos($uname, "win") !== false) {
$this->markTestSkipped('Skipped on WIN');
}

$testDir = static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR .'unwritable';
mkdir($testDir);
chmod($testDir, 0444);

Expand All @@ -111,7 +132,7 @@ public function testUnwritableDir()
*/
public function testWritePathGetter()
{
$this->assertEquals(static::getGeneratedFilesDir() . "/$this->fileName.php", $this->codeFile->getWritePath());
$this->assertEquals(static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . $this->fileName . ".php", $this->codeFile->getWritePath());
}

/**
Expand All @@ -124,7 +145,13 @@ public function testWritePathGetter()
public function testFileWritingWorks()
{
$this->codeFile->writeFile();
$this->assertFileEquals(static::getExpectedFilesDir() . "/$this->fileName.php", $this->codeFile->getWritePath());

$this->assertTrue(
$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $this->fileName . ".php", $this->codeFile->getWritePath()
)
);

}

/**
Expand All @@ -137,10 +164,14 @@ public function testFileWritingWorks()
public function testFileWritingWorksWithTrailingSlash()
{
$this->fileName = 'EmptyCodeFileWithSlash';
$this->codeFile->changeWriteDir($this->codeFile->getWriteDir() . '/');
$this->codeFile->changeWriteDir($this->codeFile->getWriteDir() . DIRECTORY_SEPARATOR);
$this->codeFile->changeFileName($this->fileName);
$this->codeFile->writeFile();

$this->assertFileEquals(static::getExpectedFilesDir() . "/$this->fileName.php", $this->codeFile->getWritePath());
$this->assertTrue(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we should just call the assertion method. Not call an assertion inside an assertion.

$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $this->fileName . ".php", $this->codeFile->getWritePath()
)
);
}
}
50 changes: 31 additions & 19 deletions tests/ArgumentsObjectClassBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ArgumentsObjectClassBuilderTest extends CodeFileTestCase
*/
protected static function getExpectedFilesDir()
{
return parent::getExpectedFilesDir() . '/arguments_objects';
return parent::getExpectedFilesDir() . DIRECTORY_SEPARATOR . 'arguments_objects';
}

/**
Expand All @@ -38,9 +38,11 @@ public function testAddScalarArgument()
$classBuilder->addScalarArgument('scalarProperty');
$classBuilder->build();

$this->assertFileEquals(
static::getExpectedFilesDir() . "/$objectName.php",
static::getGeneratedFilesDir() . "/$objectName.php"
$this->assertTrue(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same in all instances that have the same issue, we should remove the assertTrue

$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php",
static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php"
)
);
}

Expand All @@ -61,9 +63,11 @@ public function testAddMultipleScalarArguments()
$classBuilder->addScalarArgument('another_scalar_property');
$classBuilder->build();

$this->assertFileEquals(
static::getExpectedFilesDir() . "/$objectName.php",
static::getGeneratedFilesDir() . "/$objectName.php"
$this->assertTrue(
$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php",
static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php"
)
);
}

Expand All @@ -83,9 +87,11 @@ public function testAddListArgument()
$classBuilder->addListArgument('listProperty', 'string');
$classBuilder->build();

$this->assertFileEquals(
static::getExpectedFilesDir() . "/$objectName.php",
static::getGeneratedFilesDir() . "/$objectName.php"
$this->assertTrue(
$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php",
static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php"
)
);
}

Expand All @@ -106,9 +112,11 @@ public function testAddMultipleListArguments()
$classBuilder->addListArgument('another_list_property', 'string');
$classBuilder->build();

$this->assertFileEquals(
static::getExpectedFilesDir() . "/$objectName.php",
static::getGeneratedFilesDir() . "/$objectName.php"
$this->assertTrue(
$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php",
static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php"
)
);
}

Expand All @@ -128,9 +136,11 @@ public function testAddInputObjectArgument()
$classBuilder->addInputObjectArgument('objectProperty', 'Some');
$classBuilder->build();

$this->assertFileEquals(
static::getExpectedFilesDir() . "/$objectName.php",
static::getGeneratedFilesDir() . "/$objectName.php"
$this->assertTrue(
$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php",
static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php"
)
);
}

Expand All @@ -151,9 +161,11 @@ public function testAddMultipleInputObjectArguments()
$classBuilder->addInputObjectArgument('another_object_property', 'Another');
$classBuilder->build();

$this->assertFileEquals(
static::getExpectedFilesDir() . "/$objectName.php",
static::getGeneratedFilesDir() . "/$objectName.php"
$this->assertTrue(
$this->assertFileEqualsIgnoreWhitespace(
static::getExpectedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php",
static::getGeneratedFilesDir() . DIRECTORY_SEPARATOR . $objectName . ".php"
)
);
}
}
Loading