<commit_before><?php

use League\FactoryMuffin\Exceptions\NoDefinedFactoryException;
use League\FactoryMuffin\Facade as FactoryMuffin;

/**
 * @group main
 */
class FactoryMuffinTest extends AbstractTestCase
{
    public function testDefaultingToFaker()
    {
        $obj = FactoryMuffin::instance('FakerDefaultingModelStub');
        $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);

        $this->assertEquals('http://lorempixel.com/400/600/', $obj->image);
        $this->assertNotEquals('unique::text', $obj->unique_text);
        $this->assertNotEquals('optional::text', $obj->optional_text);
    }

    public function testGetFaker()
    {
        $original = FactoryMuffin::getGeneratorFactory()->getFaker();
        $new = FactoryMuffin::getGeneratorFactory()->setFakerLocale('en_GB')->getFaker();

        $this->assertInstanceOf('Faker\Generator', $original);
        $this->assertInstanceOf('Faker\Generator', $new);

        $this->assertFalse($original === $new);
    }

    public function testShouldGetAttributesFor()
    {
        $object = new MainModelStub();
        $attr = FactoryMuffin::attributesFor($object);
        $this->assertInternalType('string', $attr['text_closure']);
    }

    public function testGetIds()
    {
        $obj = FactoryMuffin::instance('IdTestModelStub');

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

    public function testShouldMakeSimpleCalls()
    {
        $obj = FactoryMuffin::instance('ComplexModelStub');

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

        $this->assertEquals($expected, $obj->future);
    }



    public function testFakerDefaultBoolean()
    {
        $obj = FactoryMuffin::instance('MainModelStub');

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

    public function testFakerDefaultLatitude()
    {
        $obj = FactoryMuffin::instance('MainModelStub');

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

    public function testFakerDefaultLongitude()
    {
        $obj = FactoryMuffin::instance('MainModelStub');

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

    /**
     * @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
     */
    public function testShouldThrowExceptionWhenNoDefinedFactoryException()
    {
        try {
            FactoryMuffin::instance($model = 'ModelWithNoFactoryClassStub');
        } catch (NoDefinedFactoryException $e) {
            $this->assertEquals("No factory definition(s) were defined for the model of type: '$model'.", $e->getMessage());
            $this->assertEquals($model, $e->getModel());
            throw $e;
        }
    }

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

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

        $this->assertEquals('just a string', $obj->string);
        $this->assertInstanceOf('ModelWithStaticMethodFactory', $obj->data['object']);
        $this->assertFalse($obj->data['saved']);
    }

    /**
     * @expectedException \PHPUnit_Framework_Error_Warning
     */
    public function testShouldThrowExceptionWhenInvalidStaticMethod()
    {
        try {
            FactoryMuffin::create($model = 'ModelWithMissingStaticMethod');
        } catch (PHPUnit_Framework_Error_Warning $e) {
            $this->assertEquals("call_user_func_array() expects parameter 1 to be a valid callback, class 'Faker\Generator' is not a subclass of 'ModelWithMissingStaticMethod'", $e->getMessage());
            throw $e;
        }
    }
}

class MainModelStub
{
    public function save()
    {
        $this->id = date('U');<commit_msg>Loosen test since we don't care about the message<commit_after><?php

use League\FactoryMuffin\Exceptions\NoDefinedFactoryException;
use League\FactoryMuffin\Facade as FactoryMuffin;

/**
 * @group main
 */
class FactoryMuffinTest extends AbstractTestCase
{
    public function testDefaultingToFaker()
    {
        $obj = FactoryMuffin::instance('FakerDefaultingModelStub');
        $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);

        $this->assertEquals('http://lorempixel.com/400/600/', $obj->image);
        $this->assertNotEquals('unique::text', $obj->unique_text);
        $this->assertNotEquals('optional::text', $obj->optional_text);
    }

    public function testGetFaker()
    {
        $original = FactoryMuffin::getGeneratorFactory()->getFaker();
        $new = FactoryMuffin::getGeneratorFactory()->setFakerLocale('en_GB')->getFaker();

        $this->assertInstanceOf('Faker\Generator', $original);
        $this->assertInstanceOf('Faker\Generator', $new);

        $this->assertFalse($original === $new);
    }

    public function testShouldGetAttributesFor()
    {
        $object = new MainModelStub();
        $attr = FactoryMuffin::attributesFor($object);
        $this->assertInternalType('string', $attr['text_closure']);
    }

    public function testGetIds()
    {
        $obj = FactoryMuffin::instance('IdTestModelStub');

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

    public function testShouldMakeSimpleCalls()
    {
        $obj = FactoryMuffin::instance('ComplexModelStub');

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

        $this->assertEquals($expected, $obj->future);
    }



    public function testFakerDefaultBoolean()
    {
        $obj = FactoryMuffin::instance('MainModelStub');

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

    public function testFakerDefaultLatitude()
    {
        $obj = FactoryMuffin::instance('MainModelStub');

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

    public function testFakerDefaultLongitude()
    {
        $obj = FactoryMuffin::instance('MainModelStub');

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

    /**
     * @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
     */
    public function testShouldThrowExceptionWhenNoDefinedFactoryException()
    {
        try {
            FactoryMuffin::instance($model = 'ModelWithNoFactoryClassStub');
        } catch (NoDefinedFactoryException $e) {
            $this->assertEquals("No factory definition(s) were defined for the model of type: '$model'.", $e->getMessage());
            $this->assertEquals($model, $e->getModel());
            throw $e;
        }
    }

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

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

        $this->assertEquals('just a string', $obj->string);
        $this->assertInstanceOf('ModelWithStaticMethodFactory', $obj->data['object']);
        $this->assertFalse($obj->data['saved']);
    }

    /**
     * @expectedException \PHPUnit_Framework_Error_Warning
     */
    public function testShouldThrowExceptionWhenInvalidStaticMethod()
    {
        FactoryMuffin::create('ModelWithMissingStaticMethod');
    }
}

class MainModelStub
{
    public function save()
    {
        $this->id = date('U');
{'fraction_non_alphanumeric': 0.14774476145377058, 'fraction_numerical': 0.005090564697525749, 'mean_word_length': 3.3636363636363638, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 119, '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': '2616774', 'n_tokens_mistral': 2465, 'n_tokens_neox': 2415, 'n_words': 374}