<commit_before><?php

namespace League\FactoryMuffin\Test;

use League\FactoryMuffin\Facade\FactoryMuffin;

class FactoryMuffinTest extends TestCase
{
    public function testDefaultingToFaker()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelB');
        $this->assertInternalType('array', $obj->card);
        $this->assertArrayHasKey('type', $obj->card);
        $this->assertArrayHasKey('number', $obj->card);
        $this->assertArrayHasKey('name', $obj->card);
        $this->assertArrayHasKey('expirationDate', $obj->card);
    }

    public function testShouldGetAttributesFor()
    {
        $attr = FactoryMuffin::attributesFor('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(4, strlen($attr['string_4']));
    }

    public function testDateKind()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $dateTime = \DateTime::createFromFormat('Y-m-d', $obj->created);
        $this->assertEquals($obj->created, $dateTime->format('Y-m-d'));
    }

    public function testInteger()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(9, strlen($obj->number));
    }

    public function testName()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertTrue(strlen($obj->full_name) > 0);
    }

    public function testString()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(4, strlen($obj->string_4));
    }

    public function testText()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(4, strlen($obj->text_4));
    }

    public function testTextDefault()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(100, strlen($obj->text_100));
    }

    public function testShouldCreate()
    {
        $obj = FactoryMuffin::create('League\FactoryMuffin\Test\SampleModelA');
        $this->assertTrue(is_numeric($obj->id));
    }

    public function testGetIds()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelF');

        $this->assertEquals(1, $obj->modelGetKey);
        $this->assertEquals(1, $obj->modelPk);
        $this->assertEquals(1, $obj->model_id);
        $this->assertNull($obj->model_null);
    }
    /**
     * @expectedException \League\FactoryMuffin\Exception\SaveFailed
     * @expectedExceptionMessage We could not save the model of type: 'League\FactoryMuffin\Test\SampleModelC'.
     */
    public function testShouldThrowExceptionOnModelSaveFailure()
    {
        FactoryMuffin::create('League\FactoryMuffin\Test\SampleModelC');
    }

    public function testShouldMakeSimpleCalls()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelD');

        $expected = gmdate('Y-m-d', strtotime('+40 days'));

        $this->assertEquals($expected, $obj->future);
    }
    public function testShouldPassSimpleArgumentsToCalls()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelD');

        $this->assertRegExp('|^[a-z0-9-]+$|', $obj->slug);
    }
    public function testShouldPassFactoryModelsToCalls()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelD');

        $this->assertRegExp("|^[a-z0-9.']+$|", $obj->munged_model);
    }

    public function testFakerDefaultBoolean()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');

        $this->assertInternalType('boolean', $obj->boolean, "Asserting {$obj->boolean} is a boolean");
    }

    public function testFakerDefaultLatitude()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');

        $this->assertGreaterThanOrEqual(-90, $obj->lat);
        $this->assertLessThanOrEqual(90, $obj->lat);
    }

    public function testFakerDefaultLongitude()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');

        $this->assertGreaterThanOrEqual(-180, $obj->lon);
        $this->assertLessThanOrEqual(180, $obj->lon);
    }

    /**
     * @expectedException \League\FactoryMuffin\Exception\NoDefinedFactory
     * @expectedExceptionMessage No factory class was defined for the model of type: 'League\FactoryMuffin\Test\SampleModelE'.
     */
    public function testShouldThrowExceptionWhenNoDefinedFactory()
    {
        FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelE');
    }

    public function testShouldAcceptClosureAsAttributeFactory()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals('just a string', $obj->text_closure);
    }

    public function testCanCreateFromStaticMethod()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\ModelWithStaticMethodFactory');

        $this->assertEquals('just a string', $obj->string);
        $this->assertEquals(4, $obj->four);
    }

    /**
     * @expectedException \League\FactoryMuffin\Exception\MethodNotFound
     * @expectedExceptionMessage The static method 'doesNotExist' was not found on the model of type: 'League\FactoryMuffin\Test\ModelWithMissingStaticMethod'.
     */
    public function testThrowExceptionWhenInvalidStaticMethod()
    {
        $obj = FactoryMuffin::create('League\FactoryMuffin\Test\ModelWithMissingStaticMethod');
        $obj->does_not_exist;
    }

    /**
     * @expectedException \League\FactoryMuffin\Exception\SaveFailed
     * @expectedExceptionMessage Failed to save. We could not save the model of type: 'League\FactoryMuffin\Test\SampleModelWithValidationErrors'.
     */
    public function testWithValidationErrors()
    {
        FactoryMuffin::create('League\FactoryMuffin\Test\SampleModelWithValidationErrors');
    }
}

class SampleModelWithValidationErrors
{
    public $validationErrors = 'Failed to save.';

    public function save()
    {

    }

    /**
     * @todo This can be removed once we resolve #97
     */
    public function delete()
    {
        return true;
    }
}
<commit_msg>More exception assertions<commit_after><?php

namespace League\FactoryMuffin\Test;

use DateTime;
use League\FactoryMuffin\Exception\NoDefinedFactory;
use League\FactoryMuffin\Exception\MethodNotFound;
use League\FactoryMuffin\Exception\SaveFailed;
use League\FactoryMuffin\Facade\FactoryMuffin;

class FactoryMuffinTest extends TestCase
{
    public function testDefaultingToFaker()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelB');
        $this->assertInternalType('array', $obj->card);
        $this->assertArrayHasKey('type', $obj->card);
        $this->assertArrayHasKey('number', $obj->card);
        $this->assertArrayHasKey('name', $obj->card);
        $this->assertArrayHasKey('expirationDate', $obj->card);
    }

    public function testShouldGetAttributesFor()
    {
        $attr = FactoryMuffin::attributesFor('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(4, strlen($attr['string_4']));
    }

    public function testDateKind()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $dateTime = DateTime::createFromFormat('Y-m-d', $obj->created);
        $this->assertEquals($obj->created, $dateTime->format('Y-m-d'));
    }

    public function testInteger()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(9, strlen($obj->number));
    }

    public function testName()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertTrue(strlen($obj->full_name) > 0);
    }

    public function testString()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(4, strlen($obj->string_4));
    }

    public function testText()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(4, strlen($obj->text_4));
    }

    public function testTextDefault()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals(100, strlen($obj->text_100));
    }

    public function testShouldCreate()
    {
        $obj = FactoryMuffin::create('League\FactoryMuffin\Test\SampleModelA');
        $this->assertTrue(is_numeric($obj->id));
    }

    public function testGetIds()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelF');

        $this->assertEquals(1, $obj->modelGetKey);
        $this->assertEquals(1, $obj->modelPk);
        $this->assertEquals(1, $obj->model_id);
        $this->assertNull($obj->model_null);
    }

    public function testShouldThrowExceptionOnModelSaveFailure()
    {
        try {
            FactoryMuffin::create($model = 'League\FactoryMuffin\Test\SampleModelC');
        } catch (SaveFailed $e) {
            $this->assertEquals("We could not save the model of type: '$model'.", $e->getMessage());
            $this->assertEquals($model, $e->getModel());
            $this->assertNull($e->getErrors());
        }
    }

    public function testShouldMakeSimpleCalls()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelD');

        $expected = gmdate('Y-m-d', strtotime('+40 days'));

        $this->assertEquals($expected, $obj->future);
    }
    public function testShouldPassSimpleArgumentsToCalls()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelD');

        $this->assertRegExp('|^[a-z0-9-]+$|', $obj->slug);
    }
    public function testShouldPassFactoryModelsToCalls()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelD');

        $this->assertRegExp("|^[a-z0-9.']+$|", $obj->munged_model);
    }

    public function testFakerDefaultBoolean()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');

        $this->assertInternalType('boolean', $obj->boolean, "Asserting {$obj->boolean} is a boolean");
    }

    public function testFakerDefaultLatitude()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');

        $this->assertGreaterThanOrEqual(-90, $obj->lat);
        $this->assertLessThanOrEqual(90, $obj->lat);
    }

    public function testFakerDefaultLongitude()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');

        $this->assertGreaterThanOrEqual(-180, $obj->lon);
        $this->assertLessThanOrEqual(180, $obj->lon);
    }

    public function testShouldThrowExceptionWhenNoDefinedFactory()
    {
        try {
            FactoryMuffin::instance($model = 'League\FactoryMuffin\Test\SampleModelE');
        } catch (NoDefinedFactory $e) {
            $this->assertEquals("No factory class was defined for the model of type: '$model'.", $e->getMessage());
            $this->assertEquals($model, $e->getModel());
        }
    }

    public function testShouldAcceptClosureAsAttributeFactory()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\SampleModelA');
        $this->assertEquals('just a string', $obj->text_closure);
    }

    public function testCanCreateFromStaticMethod()
    {
        $obj = FactoryMuffin::instance('League\FactoryMuffin\Test\ModelWithStaticMethodFactory');

        $this->assertEquals('just a string', $obj->string);
        $this->assertEquals(4, $obj->four);
    }

    public function testThrowExceptionWhenInvalidStaticMethod()
    {
        try {
            $obj = FactoryMuffin::create($model = 'League\FactoryMuffin\Test\ModelWithMissingStaticMethod');
        } catch (MethodNotFound $e) {
            $this->assertEquals("The static method 'doesNotExist' was not found on the model of type: '$model'.", $e->getMessage());
            $this->assertEquals($model, $e->getModel());
            $this->assertEquals('doesNotExist', $e->getMethod());
        }
    }

    public function testWithValidationErrors()
    {
        try {
            FactoryMuffin::create($model = 'League\FactoryMuffin\Test\SampleModelWithValidationErrors');
        } catch (SaveFailed $e) {
            $this->assertEquals("Failed to save. We could not save the model of type: '$model'.", $e->getMessage());
            $this->assertEquals($model, $e->getModel());
            $this->assertEquals('Failed to save.', $e->getErrors());
        }
    }
}

class SampleModelWithValidationErrors
{
    public $validationErrors = 'Failed to save.';

    public function save()
    {

    }

    /**
     * @todo This can be removed once we resolve #97
     */
    public function delete()
    {
        return true;
    }
}

{'fraction_non_alphanumeric': 0.1429344883595019, 'fraction_numerical': 0.005568876169850723, 'mean_word_length': 3.3186372745490984, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 144, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1655283', 'n_tokens_mistral': 3930, 'n_tokens_neox': 3796, 'n_words': 572}